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

revalidateTag not working on vercel if it contains special characters #61390

Closed
OMikkel opened this issue Jan 30, 2024 · 1 comment · Fixed by #61392
Closed

revalidateTag not working on vercel if it contains special characters #61390

OMikkel opened this issue Jan 30, 2024 · 1 comment · Fixed by #61392
Labels
bug Issue was opened via the bug report template. locked Pages Router Related to Pages Router.

Comments

@OMikkel
Copy link
Contributor

OMikkel commented Jan 30, 2024

Link to the code that reproduces this issue

https://github.com/OMikkel/nextjs-fetch-cache-tag-revalidation-issue-replication

To Reproduce

  1. Deploy repo to vercel (important)
  2. Visit your deployment url (https://nextjs-fetch-cache-tag-revalidation-issue-replication.vercel.app/)
  3. Click Reload Page button and see the Current time update
  4. Click Revalidate with tag and then reload page - Notice the Cached time from API does not update
  5. Click Revalidate with encoded tag - Notice the page reloads and the Cached time from API is now updated

Current vs. Expected behavior

Current

If a tag contains special characters that is not safe in a url. The cache with the tag wont be revalidated.
This is due to revalidateTag not encoding the tag before appending it to the cache invalidation url.

`${this.cacheEndpoint}/v1/suspense-cache/revalidate?tags=${tag}`,

If i manually encode the tag like this revalidateTag(encodeURIComponent(<tag>)) it now works every single time 😃

Expected behavior

The tag should revalidate when revalidateTag(<tag>) is called no matter what characters the string contains

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
Binaries:
  Node: 18.18.2
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.1.0
  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, Data fetching (gS(S)P, getInitialProps)

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

Vercel (Deployed)

Additional context

I went through all the releases to find the version where revalidateTag was introduced.
It appears it was introduced in version v13.3.1-canary.17 and it appears to have the same issue back then. The tags are not encoded before being appended to the cache invalidation url.
https://github.com/vercel/next.js/blob/v13.3.1-canary.17/packages/next/src/server/lib/incremental-cache/fetch-cache.ts

I am also using this workaround to use the data cache during vercel build.
https://github.com/evankirkiles/nextjs-broken-data-cache-demo
Issue: vercel/next.js#56370

PR to fix this issue

This pr should fix the issues. #61392
It works in my ecommerce application if i encode tags.

Video of the issue

Temporary fix

revalidateTag(encodeURIComponent("tag"))
@OMikkel OMikkel added the bug Issue was opened via the bug report template. label Jan 30, 2024
@github-actions github-actions bot added the Pages Router Related to Pages Router. label Jan 30, 2024
OMikkel added a commit to OMikkel/next.js that referenced this issue Jan 30, 2024
ijjk added a commit that referenced this issue Feb 5, 2024
fixes #61390

### What?

Added encodeURIComponent to encode revalidateTag value beforing
appending it to the url.

### Why?

If a tag contains special characters the tag wont be revalidated because
the special characters arent encoded.
By encoding the tag any special characters can be used.
Check out issue #61390 for further explanation

I believe this is the reason why users report the revalidateTag as
unreliable. They might be using special characters in their tags to make
sure they are unique, but when deployed to vercel and the fetch cache is
used revalidateTag fail to revalidate them.

### How?

By encoding revalidateTag with encodeURIcomponent the tag can contain
special characters.

Closes NEXT-
Fixes #61390

---------

Co-authored-by: JJ Kasper <[email protected]>
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 Feb 19, 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. locked Pages Router Related to Pages Router.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant