alfonsusac / nextjs-better-unstable-cache

Wrapper function for unstable_cache() from next/cache (Next.js Caching)
https://www.npmjs.com/package/nextjs-better-unstable-cache
40 stars 4 forks source link

Does not revalidate on 14.2 #9

Closed benjick closed 1 month ago

benjick commented 2 months ago

Hello!

I'm facing an issue where I can't revalidate cached data when upgrading to 14.2. Works fine on 14.1.0.

I've created a reproduction for the issue here: https://github.com/benjick/unstable-cache-error-reproduction

alfonsusac commented 2 months ago

Hi, thanks for trying the lib. the repository is private. Have you made sure that its not the problem in unstable_cache itself?

benjick commented 2 months ago

Sorry, I made it public now.

I haven't tried verifying with unstable_cache yet, but it's a good idea!

benjick commented 2 months ago

I've updated the repo with an unstable_cache example as well. It works after upgrading next to 14.2.3

alfonsusac commented 2 months ago

Thanks, will test this out

alfonsusac commented 2 months ago

Ive tried the repo and it works well for me?

image
benjick commented 2 months ago

Just to clarify, did you update next to 14.2.3?

alfonsusac commented 2 months ago

No i didnt, ok now i can see it. Ill try to see what causes this 🤔

alfonsusac commented 1 month ago

I just discovered what causes this. This is because functions that are exported from "use server" will be converted into async functions. This makes the () => ['test-case-value] part to become async and will failed to get read by memoize.

I just experimented with it now and I completely understand that its not your fault. But do know that "use server" shouldnt be used to place data fetching function as any function that is exported from that file will become an endpoint.

I will put a warning in the function to better communicate with developers on how they should use their memoize functions.

benjick commented 1 month ago

I just verified that and you are right! Thank you so much for discovering the reason behind this! 🙏

My pattern has been to group both "reads and writes" in a single file based on their purpose, like groups.ts and putting "use server" in that file.

Thanks again @alfonsusac!