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

Using revalidateTag() inside dynamic route handler is unstable #69064

Open
younes101020 opened this issue Aug 19, 2024 · 2 comments
Open

Using revalidateTag() inside dynamic route handler is unstable #69064

younes101020 opened this issue Aug 19, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@younes101020
Copy link

younes101020 commented Aug 19, 2024

Link to the code that reproduces this issue

https://github.com/younes101020/electra-v2/

To Reproduce

  1. Clone the repository
youyou@youyou:~/electra$ git clone git@github.com:younes101020/Electra-v2.git
  1. create .env.development file in root directory and past .env.example into it and for TMDB_API_KEY + TMDB_ACCESS_TOKEN env you can retrieve them from https://www.themoviedb.org/settings/api
  2. Install dependencies and start dev environment (you need docker compose)
youyou@youyou:~/electra$ yarn
youyou@youyou:~/electra$ yarn dev
  1. go to /src/app/api/movie/[sessionid]/[accountid]/[movieid]/rating/route.ts file and replace:
// Replace this (this code is the only way I've found to fix the initial issue)
await new Promise((resolve) => {
      setTimeout(() => {
        resolve(revalidateTag(`rated:${params.accountid}`));
      }, 3000);
});
// By this
revalidateTag(`rated:${params.accountid}`)
  1. Go to http://localhost:3000 logged in, click on any movie card image and add/update movies rating

Current vs. Expected behavior

I expect the cache initiated by the unstable_cache() method to be revalidated each time a movie's score is assigned.

Sometimes the on-demand cache isn't revalidated while the route that executes revalidateTag() is called. I have the impression that the route handler returns a response even before letting revalidateTag() finish the job properly, so I've temporarily wrapped this function in a promise with a fairly long delay.

I think it would be good to return a promise to revalidateTag() instead of nothing once the cache has been revalidated or to run this function in a worker, it wouldn't bother me personally, I would only have to manage the UI with useOptimistic

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Thu Oct 5 21:02:42 UTC 2023
  Available memory (MB): 7908
  Available CPU cores: 12
Binaries:
  Node: 21.6.0
  npm: 10.8.1
  Yarn: 1.22.21
  pnpm: 9.1.2
Relevant Packages:
  next: 14.2.3 // There is a newer version (14.2.5) available, upgrade recommended! 
  eslint-config-next: 14.2.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: standalone

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

Runtime

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

next dev (local), next build (local), next start (local)

Additional context

No response

@younes101020 younes101020 added the bug Issue was opened via the bug report template. label Aug 19, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Aug 19, 2024
@eps1lon
Copy link
Member

eps1lon commented Aug 19, 2024

Sometimes the on-demand cache isn't revalidated while the route that executes revalidateTag() is called.

Can you trim down your app to just the essential parts and then explain what steps you took to reach the actual behavior and what you expected to happen instead?

Keep in mind that revalidateTag isn't synchronous. When a cache is invalidated, calling fetch immediately afterwards will still get the cached entry.

@younes101020
Copy link
Author

younes101020 commented Aug 19, 2024

Thnaks for the response @eps1lon even in subsequent fetch we can see that the the cache isnt revalidated, concerning the app you need to logged in, click on any movie and add/update movie rating you will see that the cache is not always revalidated

The cached part is the movie ratings you can find the logic for it here /src/app/api/account/[sessionid]/rated/[accountid]/movies/route.ts and the invalidation logic here /src/app/api/movie/[sessionid]/[accountid]/[movieid]/rating/route.ts

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

2 participants