next.js: unstable_cache does not deserialize dates
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:24 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T6000
Binaries:
Node: 18.12.1
npm: 9.6.4
Yarn: 1.22.19
pnpm: 7.13.1
Relevant packages:
next: 13.4.7-canary.2
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: 4.9.5
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true), Data fetching (gS(S)P, getInitialProps)
Link to the code that reproduces this issue or a replay of the bug
https://github.com/SSardorf/next_unstable_cache_date_deserialization
To Reproduce
npm inpx prisma generatenpx prisma db pushnpx prisma db seednpm run dev
When you first run the project, you’ll see that it loads correctly (cause first prisma invocation will result in non-cached data)
When you refresh, the cached data will be shown, and it’ll throw an error, as post.createdAt is no longer a Date, but a string:
<p>{post.createdAt.toISOString()}</p>
Describe the Bug
When you first run the project, you’ll see that it loads correctly (cause first prisma invocation will result in non-cached data)
When you refresh, the cached data will be shown, and it’ll throw an error, as post.createdAt is no longer a Date, but a string:
<p>{post.createdAt.toISOString()}</p>
Expected Behavior
unstable_cache should correctly de-serialize Dates.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 23
- Comments: 25 (9 by maintainers)
Commits related to this issue
- correct better `unstable_cache`, see vercel/next.js#51613 — committed to mnpqraven/othi-monorepo by mnpqraven 2 years ago
here’s a typed version from @mordechaim’s solution:
I would like to point out that not only that it returns Date as string, it reports Typescript type as unchanged. Thus making the entire codebase using that data invalid since TS reports Date, but the data is actually string.
Yes, pretty straightforward.
Cache tags are done via
cacheTag()function, which allows you to tag over the actual data! I don’t know if you realize how big of a deal this is.You can do:
revalidateTagandrevalidatePathcontinue to work as expected.Based on the suggestion from @numman-ali, here’s a utility you can use:
Be careful when using the custom
cachesolution mentioned above!unstable_cacheby default uses the passed-in function’s definition and its arguments as the cache key. If this wrapper solution is used, then the function definition ofwrap, which does not change, and any arguments passed tofnwill be part of the cache key. Therefore, if you plan to use thecachesolution for multiple cache entries, uniquekeyPartsMUST be passed in, in order to differentiate the cache entries.I’m embarrassed to say that I scratched my head for two days about why my cache was returning the wrong data, only to read into what
unstable_cachewas doing.Please see this discussion thread for more info.
We can maybe improve the
cachesolution by always includingfn.toString()as a key part (somebody check my work as I’m not a typescript person), which is whatunstable_cachewould do.I made the change today to transition from unstable_cache_safe to use cache. Was relatively painless and seems to be working well with dates too. Hopefully it also fixes my other cache inconsistency issue that was happening to me.
The new
"use cache"directive will now be able to cache any RSC payload, including dates and many more.Or just use this and call it a day…
Had the same problem with dates, using the “use cache” functionality resolved the issue.
You can use the
experimental.useCacheflag which doesn’t require enablingdynamicIONot necessarily as to turn on the
dynamicIOflag, its going to require you to removeexport const dynamic = "force-dynamic";from your pages if you use that.Then theres the issue of https://nextjs.org/docs/messages/next-prerender-missing-suspense
So I gave up and I’ll wait for this stuff to be stable or better docs/migration path comes out.
I was surprised when I ran into this error. In my opinion this should be handled by
next/cacheitself without the need for extra code or dependencies.The two solutions proposed above work well in the meantime as a workaround.
yep, just offering improved type declarations for
unstable_cacheas an alternative measure to improve type safety of the built-in serialization behavior and avoid unexpected runtime errors without having to add an external library