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 loads Object instance instead of Map instance from cache. #64812

Open
taylorjohnwood opened this issue Apr 20, 2024 · 3 comments
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@taylorjohnwood
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/unstable-cache-map-bug-mq2xxm

To Reproduce

  1. Start the application in development (next dev)
  2. On initial load, the page will correctly display the contents of the Map
  3. Refresh the page
  4. The page will show an error cachedMap.entries is not a function

Current vs. Expected behavior

Following the steps from the previous section, I expected to still see the contents of the map even after refreshing / loading a cached version of the Map.

It seems to me that the Map is being loaded as an instance of Object. That's why it doesn't have an entries method defined.

If I inspect the cache contents at ./.next/cache/fetch-cache I see

{"kind":"FETCH","data":{"headers":{},"body":"{}","status":200,"url":""},"revalidate":31536000,"tags":[]}

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 14.2.0-canary.9 // There is a newer canary version (14.3.0-canary.13) available, please upgrade! 
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A
 ⚠ There is a newer canary version (14.3.0-canary.13) available, please upgrade! 
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue

Which area(s) are affected? (Select all that apply)

Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local), Vercel (Deployed)

Additional context

I tried to install latest canary in the sandbox, but the install command was erroring in CodeSandbox.

I've observed this exact issue outside of CodeSandbox environments, I'm certain the issue is not environment related (I've observed in Vercel deployments too).

@taylorjohnwood taylorjohnwood added the bug Issue was opened via the bug report template. label Apr 20, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Apr 20, 2024
@taylorjohnwood taylorjohnwood changed the title unstable_cache loads Object instance instead of Map instance from cache. unstable_cache loads Object instance instead of Map instance from unstable_cache. Apr 20, 2024
@taylorjohnwood taylorjohnwood changed the title unstable_cache loads Object instance instead of Map instance from unstable_cache. unstable_cache loads Object instance instead of Map instance from cache. Apr 20, 2024
@hanshs
Copy link

hanshs commented Apr 23, 2024

probably a similar issue with the Date object, as a Date will be returned as string from the cache

@sannajammeh
Copy link
Contributor

sannajammeh commented Apr 23, 2024

This is not a bug, it is a documentation issue however.

Maps & dates are not JSON serializable. It would be the same as returning new Map() from a server action to the client, it crashes.

@taylorjohnwood
Copy link
Author

taylorjohnwood commented Apr 23, 2024

This is not a bug, it is a documentation issue however.

Maps & dates are not JSON serializable. It would be the same as returning new Map() from a server action to the client, it crashes.

Thanks for clarifying @sannajammeh

  1. I could draft a doc's update as my first contribution to Next, but I'm not sure how people feeling about documenting an unstable API (unstable_cache).
  2. I would think there should be some warning/runtime error/lint issue when a user attempts to cache a non-serializable instance. Perhaps this could be added as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants