vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.7k stars 26.94k forks source link

After using getServerSideProps, when the server is redeployed, json and their responses are empty {}. #62631

Open rick-liruixin opened 8 months ago

rick-liruixin commented 8 months ago

Link to the code that reproduces this issue

https://github.com/rick-liruixin/body-scroll-lock-upgrade/tree/next/examples/next

To Reproduce

After using getServerSideProps, when the server is redeployed, json and their responses are empty {}.

The code repository address is only a rough code, because it requires redeployment and other operations, and cannot provide a demonstration address

Reproduce the scene: 1, to be replicated in the deployed environment. 2, there are two server rendered a and b pages.

  1. Add middleware generateBuildId, e.g. /_next/data/*/.json
  2. Open page a next to the deployment server. The link to go to page b must be displayed on page a. Link or router jump. Do not refresh the page during this period. The browser stays on page a. 5, modify any code and deploy the server again. 6, and then click on the original a page to jump to the B page, you will find that the B page getServerSideProps return is {}

空对象

Current vs. Expected behavior

There should be data returned instead of empty objects

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
Binaries:
  Node: 18.12.1
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: 8.2.0
Relevant Packages:
  next: 13.5.6
  eslint-config-next: 13.5.6
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.0.2
Next.js Config:
  output: standalone

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

Data fetching (gS(S)P, getInitialProps)

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

Vercel (Deployed)

Additional context

No response

robincarlo84 commented 8 months ago

I have the same issue but on getStaticProps. It happens when you keep a page open and do a rebuild, and right after the rebuild you go to the page again, and click any internal links then you'll see their responses are empty.

So I checked an older Nextjs version(13.4.8) to see how it was working before.

Before, when you click any internal links after a rebuild, the JSON files will return 404 then sends you to the actual page. image_2024_02_28T18_41_38_663Z

Now using the latest version(14.1.0), when you click any internal links after a rebuild, the JSON files will return 200 but wont send you to the actual page. image_2024_02_28T18_50_32_977Z

It's probably a caching issue, the page doesn't seem to realize that the JSON URL doesn't exist anymore.

Bahlaouane-Hamza commented 8 months ago

I believe it's a cache issue. Next base server returns res.body("{}").send(); when there no content change. Just to confirm, after you get that empty object, open the json link in a new tab and force page reload, do you see the content again ? If so, you need to force a res.setHeader('Cache-Control', 'no-store');

Another question, are you using any service workers ? pwa stuff ?

rajtersimon commented 8 months ago

It's an issue if you are using the middleware with the pages directory. More can be found here: https://github.com/vercel/next.js/issues/57207, https://github.com/vercel/next.js/issues/59612. It was fixed in v14.1.1-canary.37, but was not included in the v14.1.1 release 🤷‍♂️

The latest stable release that does not have this issue is v13.4.12

m-sanders commented 6 months ago

I just discovered the same issue. The fix has been released in 14.2.0.

fix: status code for 404 props queries to avoid client side navigation with empty props: https://github.com/vercel/next.js/pull/60968