Skip to content

Commit ad71199

Browse files
fix: Stop re-loading already-loaded CSS during server-side route resolution (#15014)
* fix: Stop re-loading already-loaded CSS during server-side route resolution * remove comments * guarantee we only query the DOM once --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent f555ba6 commit ad71199

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

.changeset/eager-rats-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: Stop re-loading already-loaded CSS during server-side route resolution

packages/kit/src/runtime/client/utils.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ export function is_external_url(url, base, hash_routing) {
323323
return false;
324324
}
325325

326-
/** @type {Record<string, boolean>} */
327-
const seen = {};
326+
/** @type {Set<string> | null} */
327+
let seen = null;
328328

329329
/**
330330
* Used for server-side resolution, to replicate Vite's CSS loading behaviour in production.
@@ -341,13 +341,17 @@ export function load_css(deps) {
341341
);
342342
const csp_nonce = csp_nonce_meta?.nonce || csp_nonce_meta?.getAttribute('nonce');
343343

344+
seen ??= new Set(
345+
Array.from(document.querySelectorAll('link[rel="stylesheet"]')).map((link) => {
346+
return /** @type {HTMLLinkElement} */ (link).href;
347+
})
348+
);
349+
344350
for (const dep of deps) {
345-
if (dep in seen) continue;
346-
seen[dep] = true;
351+
const href = new URL(dep, document.baseURI).href;
347352

348-
if (document.querySelector(`link[href="${dep}"][rel="stylesheet"]`)) {
349-
continue;
350-
}
353+
if (seen.has(href)) continue;
354+
seen.add(href);
351355

352356
const link = document.createElement('link');
353357
link.rel = 'stylesheet';

packages/kit/test/apps/basics/test/cross-platform/client.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,9 @@ test.describe('Routing', () => {
927927
// we start watching requests
928928
await page.goto('/routing/form-get', { waitUntil: 'load' });
929929

930-
expect(await page.textContent('h1')).toBe('...');
931-
expect(await page.textContent('h2')).toBe('enter');
932-
expect(await page.textContent('h3')).toBe('...');
930+
await expect(page.locator('h1')).toHaveText('...');
931+
await expect(page.locator('h2')).toHaveText('enter');
932+
await expect(page.locator('h3')).toHaveText('...');
933933

934934
/** @type {string[]} */
935935
const requests = [];
@@ -939,10 +939,10 @@ test.describe('Routing', () => {
939939
await page.locator('button').click();
940940

941941
// Filter out server-side route resolution request
942+
await expect(page.locator('h1')).toHaveText('updated');
943+
await expect(page.locator('h2')).toHaveText('form');
944+
await expect(page.locator('h3')).toHaveText('bar');
942945
expect(requests.filter((r) => !r.includes('__route.js'))).toEqual([]);
943-
expect(await page.textContent('h1')).toBe('updated');
944-
expect(await page.textContent('h2')).toBe('form');
945-
expect(await page.textContent('h3')).toBe('bar');
946946
});
947947

948948
test('responds to <form target="_blank"> submission with new tab', async ({ page }) => {

0 commit comments

Comments
 (0)