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

revalidatePath with type = "layout" | "page" does not invalidate the data cache #62071

Closed
juliesaia opened this issue Feb 15, 2024 · 6 comments · Fixed by #65365
Closed

revalidatePath with type = "layout" | "page" does not invalidate the data cache #62071

juliesaia opened this issue Feb 15, 2024 · 6 comments · Fixed by #65365
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@juliesaia
Copy link

juliesaia commented Feb 15, 2024

Link to the code that reproduces this issue

https://github.com/juliesaia/revalidate-path-test

To Reproduce

  1. next dev
  2. Click revalidate dynamic route (page) or (layout)
  3. Wait 5 seconds, click revalidate dynamic route again
  4. Click link to dynamic route, verify that server (rendered) time and fetched (external from API) are not in sync

Current vs. Expected behavior

revalidatePath("/dynamic/1", "layout") should invalidate the cached result from fetch. Without this functionality, it's impossible to invalidate a subset of paths.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Thu Oct 5 21:02:42 UTC 2023
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.1.1-canary.54 // Latest available version is detected (14.1.1-canary.54).
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

App Router

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

next dev (local), next start (local), Vercel (Deployed)

Additional context

No response

NEXT-3302

@juliesaia juliesaia added the bug Issue was opened via the bug report template. label Feb 15, 2024
@toonverbeek
Copy link

Can confirm this is happening for me as well, running 14.1.0. Trying to revalidate the root layout in my case does not trigger revalidation of sub paths.

@spiritanand
Copy link

revalidatePath("/any-path "page");

not working for me. I have to either manually refersh or use router.refresh(), which is not ideal since there will be no way for me to refetch the server component in case it is mutated by another server component (server actions)

@brandon-carlisle
Copy link

brandon-carlisle commented Apr 15, 2024

I faced a similar issue while using revalidatePath on a dynamic route and specifying "page" or "layout" as the type. When I omitted the second argument in revalidatePath, the cache was revalidated.

Using 14.2.1

@vitor-antonio
Copy link

Same thing here on my side, using revalidatePath with 'page' or 'layout' doesn't trigger revalidation when navigating back to my dynamic path /page-2. Only started working when I've updated nextjs to version 14.2.3 and removed the second param for revalidatePath.

@github-actions github-actions bot added the linear: next Confirmed issue that is tracked by the Next.js team. label May 2, 2024
@ztanner
Copy link
Member

ztanner commented May 6, 2024

Hi @juliesaia -- it looks like there might be some confusion from the docs around how the type argument is meant to be handled.

I've submitted a PR that attempts to clarify it. When you call revalidatePath("/dynamic/1", "page") or revalidatePath("/dynamic/1", "layout"), you're revalidating a cache tag that doesn't exist.

For your dynamic segment, the possible cache tags are /dynamic/[id]/layout and /dynamic/[id]/page. If you only want to revalidate the fetch for the specific page, calling revalidatePath("/dynamic/1") (without type) is correct. If you want to revalidate the fetch cache for all of the dynamic routes, you'd call revalidatePath("/dynamic/[id]", "page"). And if you only wanted to revalidate the fetches in the layout, you'd call revalidatePath("/dynamic/[id]", "layout").

ztanner added a commit that referenced this issue May 6, 2024
When revalidating a page that corresponds with a dynamic path and when
using the “type” parameter, eg `revalidatePath(“/dynamic/1”, “page”)`
corresponding with `/dynamic/[id]`, the wrong cache tags would be
checked to determine if a revalidation occurred.

This is because the “derived” cache tags created for a
`/dynamic/[id]/page` are:

- /dynamic/[id]/layout
- /dynamic/[id]/page

Additionally, a tag is added for the current canonical URL, so the final
tag would be:
- /dynamic/1

Thus a fetch on `/dynamic/1` would be tagged with the following:
- /layout
- /dynamic/layout
- /dynamic/[id]/layout
- /dynamic/[id]/page
- /dynamic/1

Calling `revalidatePath(“/dynamic/1”, “page”)` would signal to
revalidate caches tagged `/dynamic/1/page` in the current logic.
However, this tag doesn’t exist given the above, so it would be a no-op
and wouldn't properly re-invoke the fetch.

This updates the docs to explicitly call out that if you are attempting
to revalidate a path that corresponds with a dynamic page, that you
should not provide a "type" argument.

Fixes #62071
Closes NEXT-3302
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants