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
72 stars 5 forks source link

Does not revalidate on 14.2 #9

Closed benjick closed 6 months ago

benjick commented 6 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 6 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 6 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 6 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 6 months ago

Thanks, will test this out

alfonsusac commented 6 months ago

Ive tried the repo and it works well for me?

image
benjick commented 6 months ago

Just to clarify, did you update next to 14.2.3?

alfonsusac commented 6 months ago

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

alfonsusac commented 6 months 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 6 months 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!

ScreamZ commented 3 months ago

I'm not sure to understand why the callback gets converted to async, it's not exported. Can anyone explains me ?

@benjick Not sure to understand your solution, maybe you could give some examples ?

Thanks

benjick commented 3 months ago

@ScreamZ hey!

I split "server actions" and "data" into different files. For server actions I put "use server" in the file, for data (where I use memoize) I do not.

ScreamZ commented 3 months ago

@ScreamZ hey!

I split "server actions" and "data" into different files. For server actions I put "use server" in the file, for data (where I use memoize) I do not.

Okay so we can say that any defined function within the file, even as inline callback is exported as a server action, probably because of hoisting or something like this ?