next.js: Styling broken on route navigation when using vanilla-extract
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
Binaries:
Node: 18.12.1
npm: 8.19.2
Yarn: 1.22.19
pnpm: 7.18.1
Relevant Packages:
next: 13.4.14-canary.1
eslint-config-next: 13.4.5
react: 18.2.0
react-dom: 18.2.0
typescript: 4.9.4
Next.js Config:
output: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue or a replay of the bug
https://share.cleanshot.com/2HmrBYgs
To Reproduce
Use only pages dir, implement styling via vanilla-extract, navigate between pages using router
Describe the Bug
Styles are broken after navigating to a different page, but on full page loads they are fine
On version "next": "13.4.5", everything works fine, but after that version where big push was made with a lot of optimizations for the app dir, a lot of things started breaking for us, like styling as seen in the video and hot reload not working (any little change reloading the whole page always) so we need to stay on 13.4.5
Expected Behavior
Styles should be good all the time
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 16
- Comments: 20
Can confirm the issue for 14.1.0 with App Router. Enforcing the the css output to a single chunk as a band-aid solution is working:
this works. I’m using pages router with Nextjs 14.2.4 But loading css in a single chunk is not a good idea
We have exact same issue on
13.4.18(it works fine on13.4.12) in one single case, not for every navigation. Hard navigating to 404 page, then soft navigating to other places seems to not load some (exactly some) of the CSS. We use CSS Modules.In our project this bug reproduces only on hard navigation to dynamic segment that throws
notFound(). Hard navigation to non-existent segment doesn’t produce this bug. Soft navigation to 404 doesn’t produce this bug either.I can see these lines, containing needed CSS chunk in the response
and after navigation I can see this script
But all the CSS in head is
Note that
b08a2188dd1c88b2.cssis missing.I am not sure but I guess I was loading CSS in a single Chunk hence the issue was happening .
this happened to fix it :
webpack(config, { nextRuntime }) { if (!nextRuntime) { config.optimization.splitChunks.cacheGroups = { …config.optimization.splitChunks.cacheGroups, styles: { name: “styles”, type: “css/mini-extract”, chunks: “all”, enforce: true, }, }; } return config; },
thanks just figured out the fix, it was my mistake all working good now
Are you using parallel routes? Next has a css loading issue for client navigation when using parallel routes. You could try updating to 14.3.0-canary.48 and see if the problem persists.
@hazzo It is NextJS issue in newer versions, any styling solution that does not use one big CSS file which is imported at the top level suffer from this, which is I would wager large percentage of projects