Unverified Commit 2b11d01a authored by Spyros Ioakeimidis's avatar Spyros Ioakeimidis Committed by GitHub

Fix: Turbopack CSS HMR error when editing an unmounted merged CSS chunk (#94551)

### Fixing a bug

- [x] Related issues linked using `fixes #74749`.
- [x] Tests added (`test/development/css-hmr-unmounted-chunk`)
- [x] N/A — removes an erroneous thrown error

### What?

In `next dev` with Turbopack, editing a `.module.css` whose component
had unmounted threw `Error: No link element found for chunk
.../components_<hash>._.css` as an unhandled rejection. The edit
**silently failed to apply** — you had to reload the page manually to
see it.

### Why?

`applyChunkListUpdate` handles chunk updates asymmetrically: `'added'`
calls `loadChunkCached` (loads if missing), but `'total'` calls
`reloadChunk`, which is rejected when no `<link>` exists. When a lazy /
`dynamic(ssr:false)` component unmounts, `unloadChunk` removes its
merged-aggregate `<link>` but the chunk list stays subscribed; a later
CSS edit emits a `'total'` update for the now-unlinked aggregate →
`reloadChunk` rejects → the `'total'` branch discards the promise →
unhandled rejection, and the edit never applies.

### How?

`reloadChunk` now loads the fresh stylesheet when no `<link>` is present
(mirroring the `'added'` branch) instead of rejecting, so the edit
applies without a manual reload. Regression test reproduces with a
minimal `dynamic(ssr:false)` + 3 `React.lazy` routes fixture (red before
— the error is thrown and the edit doesn't apply; green after).

Fixes #74749

<!-- NEXT_JS_LLM_PR -->

---------
Co-authored-by: 's avatarBenjamin Woodruff <benjamin.woodruff@vercel.com>
parent 02a429ae
import { lazy, Suspense, useState } from 'react'
const routes = {
R0: lazy(() => import('./routes/Route0')),
R1: lazy(() => import('./routes/Route1')),
R2: lazy(() => import('./routes/Route2')),
}
export default function App() {
const [cur, setCur] = useState('R0')
const Route = routes[cur]
return (
<main>
<nav>
{Object.keys(routes).map((k) => (
<button key={k} id={'btn-' + k} onClick={() => setCur(k)}>
{k}
</button>
))}
</nav>
<div id="route">
<Suspense fallback={<p>loading…</p>}>
<Route />
</Suspense>
</div>
</main>
)
}
import s from './C0.module.css'
export default function C0() {
return (
<div id="c0" className={s.box}>
C0
</div>
)
}
import s from './C1.module.css'
export default function C1() {
return (
<div id="c1" className={s.box}>
C1
</div>
)
}
import s from './C2.module.css'
export default function C2() {
return (
<div id="c2" className={s.box}>
C2
</div>
)
}
import s from './C3.module.css'
export default function C3() {
return (
<div id="c3" className={s.box}>
C3
</div>
)
}
import s from './C4.module.css'
export default function C4() {
return (
<div id="c4" className={s.box}>
C4
</div>
)
}
import s from './C5.module.css'
export default function C5() {
return (
<div id="c5" className={s.box}>
C5
</div>
)
}
import { join } from 'path'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import type * as Playwright from 'playwright'
// Regression test for https://github.com/vercel/next.js/issues/74749
//
// In Turbopack dev, editing a CSS module whose component had unmounted threw
// `Error: No link element found for chunk .../components_<hash>._.css` as an
// unhandledRejection, and the edit silently failed to apply — you had to reload
// the page manually to pick it up.
//
// Repro shape (see the fixture): a `dynamic(ssr:false)` shell renders one of a
// few `React.lazy` routes that import an overlapping pool of CSS-module
// components, so Turbopack emits per-route MERGED AGGREGATE css chunks
// (`components_<hash>._.css`). Route 1 owns component C4 (which Route 0 does
// not). Navigating into Route 1 and back to Route 0 unmounts C4, and
// `unloadChunk` removes its aggregate's <link> — but the chunk list stays
// subscribed. Editing `C4.module.css` then produced a 'total' chunk update for
// the now-unlinked aggregate; `reloadChunk` found zero <link>s and rejected
// (the 'total' branch of `applyChunkListUpdate` discards the promise, so it
// surfaced as an unhandledRejection). The fix loads the chunk instead, so the
// edit applies.
describe('css-hmr-unmounted-chunk', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
patchFileDelay: 500,
})
if (!isTurbopack) {
it('skipped on non-Turbopack environments', () => {})
return
}
it('applies a CSS edit for an unmounted route without erroring or needing a manual reload', async () => {
const browserErrors: string[] = []
const browser = await next.browser('/', {
beforePageLoad(page: Playwright.Page) {
page.on('pageerror', (err) =>
browserErrors.push(String(err?.message ?? err))
)
},
})
// Wait for the ssr:false shell + default route (R0) to mount.
await retry(async () => {
expect(await browser.elementByCss('#route').text()).toContain('Route 0')
})
// Navigate into R1 (mounts it, links its CSS chunks) then back to R0.
// R1 owns C4, which R0 does not — so leaving R1 unmounts C4 and removes its
// merged CSS aggregate's <link>.
await browser.elementByCss('#btn-R1').click()
await retry(async () => {
expect(await browser.elementByCss('#route').text()).toContain('Route 1')
})
await browser.elementByCss('#btn-R0').click()
await retry(async () => {
expect(await browser.elementByCss('#route').text()).toContain('Route 0')
})
const cliOutputBefore = next.cliOutput.length
// Edit a CSS module owned by the now-unmounted R1. This is the edit that
// threw before the fix.
const unmountedCssPath = join('components', 'C4.module.css')
const originalUnmountedCss = await next.readFile(unmountedCssPath)
// Also edit a CSS module used by the currently-mounted R0 (C0). Waiting for
// this visible change to hot-apply gives the (buggy) C4 'total' update time
// to round-trip, and proves HMR is still alive afterwards.
const mountedCssPath = join('components', 'C0.module.css')
const originalMountedCss = await next.readFile(mountedCssPath)
try {
await next.patchFile(
unmountedCssPath,
originalUnmountedCss.replace('color: red', 'color: blue')
)
await next.patchFile(
mountedCssPath,
originalMountedCss.replace('color: red', 'color: green')
)
// The mounted component's edit hot-applies (HMR is processing updates).
await retry(async () => {
const color = await browser.eval(
`window.getComputedStyle(document.getElementById('c0')).color`
)
expect(color).toBe('rgb(0, 128, 0)')
}, 10000)
// The unmounted-chunk edit must NOT have thrown the unlinked-chunk error.
const newCliOutput = next.cliOutput.slice(cliOutputBefore)
expect(newCliOutput).not.toContain('No link element found for chunk')
expect(browserErrors.join('\n')).not.toContain(
'No link element found for chunk'
)
// The edit is applied (the fix re-links the now-unlinked chunk with the
// fresh CSS), so revisiting the route shows the updated color — without a
// manual reload.
await browser.elementByCss('#btn-R1').click()
await retry(async () => {
const color = await browser.eval(
`window.getComputedStyle(document.getElementById('c4')).color`
)
expect(color).toBe('rgb(0, 0, 255)')
}, 10000)
} finally {
await next.patchFile(unmountedCssPath, originalUnmountedCss)
await next.patchFile(mountedCssPath, originalMountedCss)
}
})
})
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
import dynamic from 'next/dynamic'
const App = dynamic(() => import('../App'), {
ssr: false,
loading: () => <p>app loading…</p>,
})
export default function Home() {
return <App />
}
import C0 from '../components/C0'
import C1 from '../components/C1'
import C2 from '../components/C2'
import C3 from '../components/C3'
export default function Route0() {
return (
<section>
<h2>Route 0</h2>
<C0 />
<C1 />
<C2 />
<C3 />
</section>
)
}
import C1 from '../components/C1'
import C2 from '../components/C2'
import C3 from '../components/C3'
import C4 from '../components/C4'
export default function Route1() {
return (
<section>
<h2>Route 1</h2>
<C1 />
<C2 />
<C3 />
<C4 />
</section>
)
}
import C2 from '../components/C2'
import C3 from '../components/C3'
import C4 from '../components/C4'
import C5 from '../components/C5'
export default function Route2() {
return (
<section>
<h2>Route 2</h2>
<C2 />
<C3 />
<C4 />
<C5 />
</section>
)
}
...@@ -62,11 +62,6 @@ let DEV_BACKEND: DevRuntimeBackend ...@@ -62,11 +62,6 @@ let DEV_BACKEND: DevRuntimeBackend
`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]` `link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`
) )
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`))
return
}
const link = document.createElement('link') const link = document.createElement('link')
link.rel = 'stylesheet' link.rel = 'stylesheet'
link.crossOrigin = CROSS_ORIGIN link.crossOrigin = CROSS_ORIGIN
...@@ -111,12 +106,23 @@ let DEV_BACKEND: DevRuntimeBackend ...@@ -111,12 +106,23 @@ let DEV_BACKEND: DevRuntimeBackend
resolve() resolve()
} }
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement!.insertBefore( // component unmounted via navigation or a `dynamic(ssr: false)`
link, // boundary, so `unloadChunk` removed it), but its chunk list stays
previousLinks[0].nextSibling // subscribed and can still receive a 'total' update. Mirror the
) // 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link)
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement!.insertBefore(
link,
previousLinks[0].nextSibling
)
}
}) })
}, },
......
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="2181f6eb-6351-4a6d-d061-6290ea95e414")}catch(e){}}(); ;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="f71e4bf6-0f5b-0cff-0120-ee23e8ff7775")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/0rv8_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0bjegbrfzt05o.js", "output/0rv8_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0bjegbrfzt05o.js",
{"otherChunks":["output/0_9x_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvsq4xsbk.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/0_9x_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvsq4xsbk.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
...@@ -2420,10 +2420,6 @@ let DEV_BACKEND; ...@@ -2420,10 +2420,6 @@ let DEV_BACKEND;
const baseChunkUrl = chunkUrl.split('?')[0]; const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl); const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`); const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.crossOrigin = CROSS_ORIGIN; link.crossOrigin = CROSS_ORIGIN;
...@@ -2458,9 +2454,20 @@ let DEV_BACKEND; ...@@ -2458,9 +2454,20 @@ let DEV_BACKEND;
// loaded instantly. // loaded instantly.
resolve(); resolve();
}; };
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling); // component unmounted via navigation or a `dynamic(ssr: false)`
// boundary, so `unloadChunk` removed it), but its chunk list stays
// subscribed and can still receive a 'total' update. Mirror the
// 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link);
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling);
}
}); });
}, },
restart: ()=>self.location.reload() restart: ()=>self.location.reload()
...@@ -2487,5 +2494,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -2487,5 +2494,5 @@ chunkListsToRegister.forEach(registerChunkList);
})(); })();
//# debugId=2181f6eb-6351-4a6d-d061-6290ea95e414 //# debugId=f71e4bf6-0f5b-0cff-0120-ee23e8ff7775
//# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0bjegbrfzt05o.js.map //# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0bjegbrfzt05o.js.map
\ No newline at end of file
...@@ -2419,10 +2419,6 @@ let DEV_BACKEND; ...@@ -2419,10 +2419,6 @@ let DEV_BACKEND;
const baseChunkUrl = chunkUrl.split('?')[0]; const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl); const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`); const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.crossOrigin = CROSS_ORIGIN; link.crossOrigin = CROSS_ORIGIN;
...@@ -2457,9 +2453,20 @@ let DEV_BACKEND; ...@@ -2457,9 +2453,20 @@ let DEV_BACKEND;
// loaded instantly. // loaded instantly.
resolve(); resolve();
}; };
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling); // component unmounted via navigation or a `dynamic(ssr: false)`
// boundary, so `unloadChunk` removed it), but its chunk list stays
// subscribed and can still receive a 'total' update. Mirror the
// 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link);
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling);
}
}); });
}, },
restart: ()=>self.location.reload() restart: ()=>self.location.reload()
......
...@@ -2419,10 +2419,6 @@ let DEV_BACKEND; ...@@ -2419,10 +2419,6 @@ let DEV_BACKEND;
const baseChunkUrl = chunkUrl.split('?')[0]; const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl); const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`); const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.crossOrigin = CROSS_ORIGIN; link.crossOrigin = CROSS_ORIGIN;
...@@ -2457,9 +2453,20 @@ let DEV_BACKEND; ...@@ -2457,9 +2453,20 @@ let DEV_BACKEND;
// loaded instantly. // loaded instantly.
resolve(); resolve();
}; };
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling); // component unmounted via navigation or a `dynamic(ssr: false)`
// boundary, so `unloadChunk` removed it), but its chunk list stays
// subscribed and can still receive a 'total' update. Mirror the
// 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link);
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling);
}
}); });
}, },
restart: ()=>self.location.reload() restart: ()=>self.location.reload()
......
...@@ -2419,10 +2419,6 @@ let DEV_BACKEND; ...@@ -2419,10 +2419,6 @@ let DEV_BACKEND;
const baseChunkUrl = chunkUrl.split('?')[0]; const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl); const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`); const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.crossOrigin = CROSS_ORIGIN; link.crossOrigin = CROSS_ORIGIN;
...@@ -2457,9 +2453,20 @@ let DEV_BACKEND; ...@@ -2457,9 +2453,20 @@ let DEV_BACKEND;
// loaded instantly. // loaded instantly.
resolve(); resolve();
}; };
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling); // component unmounted via navigation or a `dynamic(ssr: false)`
// boundary, so `unloadChunk` removed it), but its chunk list stays
// subscribed and can still receive a 'total' update. Mirror the
// 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link);
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling);
}
}); });
}, },
restart: ()=>self.location.reload() restart: ()=>self.location.reload()
......
...@@ -2419,10 +2419,6 @@ let DEV_BACKEND; ...@@ -2419,10 +2419,6 @@ let DEV_BACKEND;
const baseChunkUrl = chunkUrl.split('?')[0]; const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl); const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`); const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.crossOrigin = CROSS_ORIGIN; link.crossOrigin = CROSS_ORIGIN;
...@@ -2457,9 +2453,20 @@ let DEV_BACKEND; ...@@ -2457,9 +2453,20 @@ let DEV_BACKEND;
// loaded instantly. // loaded instantly.
resolve(); resolve();
}; };
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling); // component unmounted via navigation or a `dynamic(ssr: false)`
// boundary, so `unloadChunk` removed it), but its chunk list stays
// subscribed and can still receive a 'total' update. Mirror the
// 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link);
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling);
}
}); });
}, },
restart: ()=>self.location.reload() restart: ()=>self.location.reload()
......
...@@ -2419,10 +2419,6 @@ let DEV_BACKEND; ...@@ -2419,10 +2419,6 @@ let DEV_BACKEND;
const baseChunkUrl = chunkUrl.split('?')[0]; const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl); const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`); const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.crossOrigin = CROSS_ORIGIN; link.crossOrigin = CROSS_ORIGIN;
...@@ -2457,9 +2453,20 @@ let DEV_BACKEND; ...@@ -2457,9 +2453,20 @@ let DEV_BACKEND;
// loaded instantly. // loaded instantly.
resolve(); resolve();
}; };
// Make sure to insert the new CSS right after the previous one, so that if (previousLinks.length === 0) {
// its precedence is higher. // The chunk's <link> was already removed from the DOM (the importing
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling); // component unmounted via navigation or a `dynamic(ssr: false)`
// boundary, so `unloadChunk` removed it), but its chunk list stays
// subscribed and can still receive a 'total' update. Mirror the
// 'added' branch of `applyChunkListUpdate` and load the fresh
// stylesheet instead of rejecting with "No link element found for
// chunk" (an unhandledRejection that forced a full page reload).
document.head.appendChild(link);
} else {
// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLinks[0].parentElement.insertBefore(link, previousLinks[0].nextSibling);
}
}); });
}, },
restart: ()=>self.location.reload() restart: ()=>self.location.reload()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment