Fix webpack runtime CSS ordering before first app router commit (#102027)
### What?
With webpack, CSS from a lazily loaded chunk (for example a
`next/dynamic` component) could be inserted **before** the route's own
stylesheets when the chunk's CSS was registered before the app router's
first commit. In that case, global CSS from a layout wrongly overrides
the dynamic component's styles. The wrong order is permanent for the
page load. This shows up as the intermittent failure in
`test/e2e/app-dir/next-dynamic-css` (webpack, `start` mode), where
`#inner2` / `#component` render `rgb(139, 0, 0)` (the layout's
`.global-class` darkred) instead of the expected colours.
### Why?
- In webpack app-router builds, CSS for chunks loaded at runtime goes
through `_N_E_STYLE_LOAD`. It is rendered by `RuntimeStylesForWebpack`
as `<link precedence="next">`, the same precedence as the route's
stylesheets, and React keeps same-precedence stylesheets in insertion
order.
- In this fixture the page is client-rendered: a client component throws
during SSR. The `next/dynamic` chunk's CSS can then be registered before
the first root commit, so `RuntimeStylesForWebpack` includes it in that
initial commit.
- The runtime-only stylesheet has no preload hint. It was observed being
inserted ahead of the route's preloaded stylesheets:
- bad head order: `component.css, base.css, root.css, layout global.css,
…`
- good head order: `root.css, layout global.css, …, base.css,
inner2.css, component.css`
- After the recent vendored React upgrade this became frequent. With
only the vendored React swapped back, the same fixture never reproduced
it.
#### Observed on canary
All in webpack `test prod (N/10, 18.3.1)` shards. Every failure is
`Received: "rgb(139, 0, 0)"` at `next-dynamic-css.test.ts:39` (`#inner2`
color) and/or `:53` (`#component` background-color).
| Run (tip) | Job (attempt) | In-job tries (line 39 / line 53) | Outcome
|
|---|---|---|---|
| 44320861472 (`a86aeba1`) | `test prod (10/10, 18.3.1)` (1) | fail,
fail, pass / pass, fail, pass | passed on in-job retry 2 |
| 44346360284 (`4f45c752`) | `test prod (5/10, 18.3.1)` (1) | fail ×3 /
— | job failed; passed on workflow attempt 2 |
| 44357189817 (`ab0c6c77`) | `test prod (3/10, 18.3.1)` (1) | fail ×3 /
fail, fail, pass | job failed; passed on workflow attempt 2 |
| 44624852454 (`e8adfc0e`) | `test prod (9/10, 18.3.1)` (1) | fail ×3 /
pass, fail, fail | job failed; passed on workflow attempt 2 |
| 44625790784 (`148f62f0`) | `test prod (10/10, 18.3.1)` (1) | fail,
pass, fail / pass, fail, fail | job failed; passed on workflow attempt 2
|
| 44713578995 (`6d5078c5`) | `test prod (6/10, 18.3.1)` (1) | fail ×3 /
pass, pass, fail | job failed; passed on workflow attempt 2 |
| 44739458109 (`0e728ca7`) | `test prod (7/10, 18.3.1)` (1) | pass,
fail, pass / fail, pass, pass | passed on in-job retry 2 (20.2 s) |
| 44762756332 (`fe94c9de`) | `test prod (1/10, 18.3.1)` (1) | pass, pass
/ fail, pass | passed on in-job retry 1 (22.2 s) |
| 44840825999 (`794836c3`) | `test prod (9/10, 18.3.1)` (1) | pass ×3 /
fail ×3 | job failed; passed on workflow attempt 2 |
| 45055453202 (`57edfeac`) | `test prod (2/10, 18.3.1)` (1) | fail ×3 /
fail, pass, pass | job failed; passed on workflow attempt 2 |
| 45154132010 (`7c179aab`) | `test prod (9/10, 18.3.1)` (1) | fail ×3 /
fail, pass, pass | job failed; passed on workflow attempt 2 |
| 45167988574 (`c0e90163`) | `test prod (5/10, 18.3.1)` (1) | fail ×3 /
fail ×3 | job failed; passed on workflow attempt 2 |
All rows are canary tips **without** this PR's change (`app-router.tsx`
there has no mounted state), so they are recurrences of the two cases
this PR targets, not results with the fix. 12 occurrences on 12 distinct
canary tips between 2026-11-18 and 2026-11-23, including after this PR
was opened. Three jobs recovered in place on an in-job retry, so those
jobs reported green although the file failed at least once; that is why
they don't show up as red canary runs. The other nine needed a fresh
workflow attempt.
Also seen outside canary, on the **default-React** shard
(`NEXT_TEST_REACT_VERSION` empty), which matches the app router always
using the vendored React: `pull_request` run 45142886308 (`062b8a05`, no
change to this suite, without this PR's change), `test prod (3/10)` (1).
Line 39 went pass, fail, pass and line 53 failed ×3; the file failed the
job.
Both assertions hit the same collision: the shared `base.module.css`
(`#inner2`) and `component.module.css` (`#component`) are both loaded
with the `next/dynamic` chunk through `_N_E_STYLE_LOAD` and rendered by
`RuntimeStylesForWebpack`. Each test does its own page load, so each one
can win or lose the race independently.
A workflow re-attempt restores the per-run `next-test-passed` cache, so
the fresh job re-runs only the failing file instead of the whole shard.
### How?
`RuntimeStylesForWebpack` renders no links until after it has mounted.
Runtime styles registered before the first commit are then rendered by
the post-mount update and appended after the route's stylesheets. That
is the same path already taken by every chunk loaded later.
The new test `should have correct order of styles when next/dynamic CSS
is registered before the first commit` (webpack, production only) forces
that timing deterministically: it holds the page's client chunk until
the `next/dynamic` chunk has finished loading, so the dynamic
component's CSS is always registered before the first commit. The
fixture's `next/dynamic` import gets a `webpackChunkName` so the test
can identify that chunk.
### Verification
#### Deterministic test (no CPU load)
Same command, without burners, `--retries 0 --debug`, rebuilding `next`
with only `app-router.tsx` swapped:
| | new test |
|---|---|
| before (`app-router.tsx` from the parent commit) | **fails 5 / 5**
(`#inner2` color `rgb(139, 0, 0)`). The four existing tests in the file
passed in all 5 runs. |
| after (this PR) | **passes 6 / 6** (5 plain runs plus one `--debug`
run) |
#### Flake rate under load
All runs below use `--retries 0` on the same 8 vCPU machine, with the
same command and 8 CPU-burner processes for both sides (measured before
the new test was added):
```sh
IS_WEBPACK_TEST=1 NEXT_TEST_MODE=start NEXT_TEST_REACT_VERSION=18.3.1 __NEXT_EXPERIMENTAL_STRICT_ROUTE_TYPES=true \
node run-tests.js --type production --retries 0 test/e2e/app-dir/next-dynamic-css/next-dynamic-css.test.ts
```
| | failed runs |
|---|---|
| before (unchanged canary) | **4 / 15** (2× `#inner2` color, 2×
`#component` background, all `rgb(139, 0, 0)`) |
| after (this PR) | **0 / 15** |
A direct Playwright probe of a webpack production build of the fixture
(fresh browser context per load) went from **15/40** wrong loads to
**0/40**. In failing loads the colour was still wrong after
`networkidle`.
Existing tests that passed with this change (`--retries 0`):
- webpack start: `app-dir/dynamic-css`, `app-dir/css-chunking`,
`app-dir/initial-css-order`, `app-dir/css-order`
- webpack dev: `app-dir/css-order`, `app-dir/next-dynamic-css`
- turbopack start: `app-dir/next-dynamic-css` (path not affected; the
new test is skipped for Turbopack and the file passes 4/4)
- webpack start with default React: `app-dir/next-dynamic-css`, all 5
tests, 1 run
- Cache Components variant of `app-dir/next-dynamic-css`: attempted, and
`next build` fails both with and without this PR's test change
(`cookies()` outside `<Suspense>` in the fixture's `page.tsx`). The
suite is already excluded for that variant in
`test/cache-components-tests-manifest.json`.
- Not measured: whether rendering runtime stylesheets in the post-mount
update (instead of the first commit) can cause a brief unstyled first
paint of dynamically loaded content. The tests check computed styles
once the element is present, not first-paint behavior.
<!-- NEXT_JS_LLM -->
<!-- fleet 08416a45-0990-4ab1-afc8-c07e694f29e9 -->
---------
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>