[Bug]: extracted CSS HMR removes React-owned links and breaks later unmounts
Extracted CSS HMR removes React-owned stylesheet links, causing NotFoundError on unmount
Problem
When React renders an extracted stylesheet's <link>, CSS HMR replaces that DOM
node. The CSS update succeeds, but React still holds the original node. A later
React update that removes the link throws:
NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.This affects frameworks that manage route stylesheet links in their React tree. A plain React reproduction is sufficient: render the link, edit the CSS, and then conditionally stop rendering the link.
Reproduction
Runnable reproduction and candidate patches use these public versions:
- Rspack 2.2.6
- Rsbuild 2.2.8, for its bundled CSS loader
- React and React DOM 19.2.7
- Playwright 1.61.1 / Chromium 149
- Node 24.19.0
git clone https://gist.github.com/matthewdavis-oai/9eb172544976d72ff1964c2827eaea1a.git css-link-reproduction
cd css-link-reproduction
corepack pnpm install
corepack pnpm exec playwright install chromium
corepack pnpm testThe test runs the real compiler in watch mode, edits a CSS file, and applies the
actual generated hot-update manifest/chunks using module.hot.check(true). It
checks both CssExtractRspackPlugin({ runtime: true }) and { runtime: false }.
Each mode also has an unchanged-CSS control.
| Case | Original link after update | React unmount | Remaining links |
|---|---|---|---|
| Either mode, no CSS edit | Connected | Succeeds | 0 |
| Native runtime, CSS edit | Removed by HMR | NotFoundError |
1 |
| JS fallback, CSS edit | Removed by HMR | NotFoundError |
1 |
Computed color changes from red to blue before the failure. The application draft is retained until the failing unmount. No React Router dependency, lazy compilation, or chunk-layout change is needed.
Expected behavior
HMR should preserve a framework-owned stylesheet node, or coordinate replacement with its owner. React should be able to unmount the original link normally after a CSS edit, with no orphan stylesheet left behind.
Source paths
Both extracted-CSS HMR implementations remove the previous link:
crates/rspack_plugin_extract_css/src/runtime/css_loading_with_hmr.ejs:extractCssApplyHandler.applyremoves each entry inextractCssOldTagsafter enabling replacement stylesheets.packages/rspack/src/runtime/cssExtractHmr.ts:updateCssclones the link and removes the original from both the replacement'sloadanderrorlisteners.
The second path is observable independently with runtime: false.
Fix direction and validation
An ownership-preserving prototype keeps the original link, loads a temporary
replacement at the same cascade position, transfers its URL to the original, and
removes the temporary link once the original has loaded. It also cancels pending
work when the owner removes the original node. The separate native and JS source
diffs are included in the reproduction Gist as
native-link-ownership.patch and js-link-ownership.patch.
The current source-only package includes exact dependency pins, a lockfile and a fresh-install validation record. Its patches/rspack-native-css-ownership.patch and patches/rspack-fallback-css-ownership.patch keep the native and JavaScript changes separate. Follow README section 3 for stock and candidate commands; the native binding does not itself patch the JavaScript fallback. The portable reproduction passes all four cases with the candidate native binding and JS runtime: original nodes remain connected until React removes them, there are no React errors, and no stylesheet links remain after unmount.
Further focused Chromium checks cover repeated/overlapping edits, restoring the original CSS, cascade order, first-request failure, removal/re-addition, unmount during loading, and removal by the native handler during a pending JS fallback update. A failure of the original link's second request uses a document reload while retaining the already-loaded temporary stylesheet. The prototype has not been validated across browser engines.
Related: #14793 concerns duplicate extracted-CSS reloads. This reproduction fails with either runtime individually, so eliminating duplicate reloads alone does not address DOM ownership. Initial-chunk reload behavior discussed in #15842 is a separate issue; this fixture has one stable chunk.
Source: web-infra-dev/rspack