Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unstable_cache doesn't serialize non-primitive data types #75083

Open
devjmetivier opened this issue Jan 19, 2025 · 0 comments
Open

unstable_cache doesn't serialize non-primitive data types #75083

devjmetivier opened this issue Jan 19, 2025 · 0 comments
Labels
Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@devjmetivier
Copy link

Link to the code that reproduces this issue

https://github.com/devjmetivier/next-unstable-cache-args-serialization

To Reproduce

  1. Open homepage
  2. Change sort order with input
  3. See request cache behavior

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.

Image

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:

// Construct the complete cache key for this function invocation
// @TODO stringify is likely not safe here. We will coerce undefined to null which will make
// the keyspace smaller than the execution space
const invocationKey = `${fixedKey}-${JSON.stringify(args)}`
const cacheKey = await incrementalCache.generateCacheKey(invocationKey)
// $urlWithPath,$sortedQueryStringKeys,$hashOfEveryThingElse
const fetchUrl = `unstable_cache ${pathname}${sortedSearch.length ? '?' : ''}${sortedSearch} ${cb.name ? ` ${cb.name}` : cacheKey}`

@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

1 participant