unstable_cache
doesn't serialize non-primitive data types
#75083
Labels
Runtime
Related to Node.js or Edge Runtime with Next.js.
Link to the code that reproduces this issue
https://github.com/devjmetivier/next-unstable-cache-args-serialization
To Reproduce
Current vs. Expected behavior
Cache doesn't work (as expected) because passing arguments to
unstable_cache
of any non-JSON serializable data type (Map, in this example) results in the same deterministic output.Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 24.1.0: Thu Nov 14 18:19:02 PST 2024; root:xnu-11215.41.3~13/RELEASE_ARM64_T8132 Available memory (MB): 32768 Available CPU cores: 10 Binaries: Node: 22.12.0 npm: 10.9.0 Yarn: 1.22.22 pnpm: 9.15.3 Relevant Packages: next: 15.2.0-canary.16 // Latest available version is detected (15.2.0-canary.16). eslint-config-next: N/A react: 19.0.0 react-dom: 19.0.0 typescript: 5.7.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Runtime, Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local), Vercel (Deployed)
Additional context
I understand that
unstable_cache
is intended to handle any work inside the function, but I'm encountering an issue when passing iterables like Maps and Sets. These data structures aren't serializable, so their contents can't be cached, forcing me to either change the data type or manually serialize/deserialize the values. It would be helpful if unstable_cache could support these types (iterables) more seamlessly.Here's where I can see these arguments being dealt with in Next.js:
next.js/packages/next/src/server/web/spec-extension/unstable-cache.ts
Lines 138 to 144 in 3f3f4bc
The text was updated successfully, but these errors were encountered: