question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

On-demand revalidated prerendered pages show old data after React hydration

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:46:32 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T6000 Binaries: Node: 14.18.1 npm: 6.14.15 Yarn: N/A pnpm: N/A Relevant packages: next: 12.2.3-canary.8 eslint-config-next: 12.2.2 react: 18.2.0 react-dom: 18.2.0

What browser are you using? (if relevant)

Edge/Chrome

How are you deploying your application? (if relevant)

npx vercel --prod

Describe the Bug

Any pages that are specified in getStaticPaths() will revert to the old data once the page is hydrated if on-demand revalidation has been executed on the path.

Minimal reproduction: https://github.com/jmotes/nextjs-ondemand-caching-bug

Deployed here: https://nextjs-ondemand-caching-bug.kairosdxp.com/

The generated HTML has the correct data, but the fetch for the the index.json?site=nextjs-ondemand-caching-bug.kairosdxp.com file returns the old cached response:

curl 'https://nextjs-ondemand-caching-bug.kairosdxp.com/_next/data/qeYGj4VSRQRyCHVLMprrF/index.json?site=nextjs-ondemand-caching-bug.kairosdxp.com' \
  -H 'authority: nextjs-ondemand-caching-bug.kairosdxp.com' \
  -H 'accept: */*' \
  -H 'accept-language: en-US,en;q=0.9' \
  -H 'referer: https://nextjs-ondemand-caching-bug.kairosdxp.com/' \
  -H 'sec-ch-ua: " Not;A Brand";v="99", "Microsoft Edge";v="103", "Chromium";v="103"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-origin' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.51' \
  -H 'x-nextjs-data: 1' \
  --compressed

I’ve noticed that removing the x-nextjs-data: 1 header causes the request to return the new data, if that helps trace the bug.

Note that the bug is only reproducible on the homepage of the minimal reproduction since that’s the only pre-rendered/static path. See line 38:

https://github.com/jmotes/nextjs-ondemand-caching-bug/blob/main/pages/_sites/[site]/index.tsx#L38

Expected Behavior

Page should always show new data after an on-demand revalidation.

Link to reproduction

https://github.com/jmotes/nextjs-ondemand-caching-bug

To Reproduce

  1. Go to https://nextjs-ondemand-caching-bug.kairosdxp.com/.
  2. Note the Revalidated Time.
  3. Click Revalidate Page.
  4. Refresh the page.
  5. Observe the new Revalidated Time briefly shows before reverting back to the previous date.
  6. Now click on Documentation, then click “Next.js” to return to the homepage.
  7. Observe the revalidated time is now correct (client side navigation works correctly, pulling the new data).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jmotescommented, Jul 15, 2022

It’s working! Thanks so much @ijjk!

1reaction
ijjkcommented, Jul 15, 2022

Hi, a related fix for this has now been landed, please re-deploy your project and give it a try!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Pre-rendering and Potential Hydration Issue
Explanation about Reacts hydration when pre-rendering. Showcased on an actual issue I had.
Read more >
A Complete Guide To Incremental Static Regeneration (ISR ...
js will invalidate the cache and show the updated product page. If the background regeneration fails, the old page remains unaltered. // pages/ ......
Read more >
The Perils of Rehydration - Josh W Comeau
This deep-dive tutorial examines how React and Gatsby can be used to pre-render ... My React component was rendering in the wrong spot!...
Read more >
Data Fetching: getServerSideProps - Next.js
First, immediately show the page without data. Parts of the page can be pre-rendered using Static Generation. You can show loading states for...
Read more >
NextJS / React SSR: 21 Universal Data Fetching Patterns ...
example of fetching data from "/api" is really simple, ... On the client-side, we re-hydrate the page with the user object and don't ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found