next.js: Loading.tsx and Suspense do not work on second page load and on
Link to the code that reproduces this issue
https://github.com/Vovch/test-next-2
To Reproduce
- pnpm i
- add .env file for DB connection (Postgres)
- pnpm dev
- navigate to localhost:3000/dashboard
- reload the page
- reload the page again
- comment out or delete the Suspense wrapper in app\dashboard\page.tsx
- reload the page
- reload the page again
Current vs. Expected behavior
Expected: On steps 4 and 5 I expect the Suspense component to be rendered correctly without delaying the page load. Steps 8 and 9 - Expected component from loading.tsx to be shown on the screen
Current: The page is not loaded into the browser at all before all the components are rendered on the server (3+ seconds). Suspense or loading.tsx components are not shown.
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Enterprise
Available memory (MB): 32257
Available CPU cores: 8
Binaries:
Node: 18.20.3
npm: 10.8.1
Yarn: N/A
pnpm: 9.11.0
Relevant Packages:
next: 15.0.4-canary.36 // Latest available version is detected (15.0.4-canary.36).
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: 5.5.2
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Lazy Loading
Which stage(s) are affected? (Select all that apply)
next dev (local) next start (local)
Additional context
I was also able to reproduce this issue with next 15.0.3 and 14.2.18.
The code was created by following the tutorial up to and including chapter 9 https://nextjs.org/learn/dashboard-app/streaming and then trying to find the issue (since I couldn’t make it work while following the tutorial).
in app\dashboard\page.tsx there is a commented out string with await new Promise(...). If enabled it causes the app to work correctly. The effect depends on milliseconds value passed to the timeout, higher values (1000 and on) make the best result, lower values like 500 makes the defect reproducible again.
Intermittently I’m unable to get the loaders even when navigating between the pages by links in the app even if the first loaded page is not the dashboard itself.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 18
It could be caused by security software, such as Kaspersky. When I disabled Kaspersky’s Web Anti-Virus, everything worked fine. I guess Kaspersky’s Web Anti-Virus caches responses with content-type: text/html or content-type: text/x-component for virus scanning.
Loading.tsx does only work on the very first render of the route. If you want to re-trigger suspense you need to set the “key” prop in suspense and use what ever that are changing, such as the searchParams or Params as the key.