Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eager-rats-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: Stop re-loading already-loaded CSS during server-side route resolution
18 changes: 11 additions & 7 deletions packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ export function is_external_url(url, base, hash_routing) {
return false;
}

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

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

seen ??= new Set(
Array.from(document.querySelectorAll('link[rel="stylesheet"]')).map((link) => {
return /** @type {HTMLLinkElement} */ (link).href;
})
);

for (const dep of deps) {
if (dep in seen) continue;
seen[dep] = true;
const href = new URL(dep, document.baseURI).href;

if (document.querySelector(`link[href="${dep}"][rel="stylesheet"]`)) {
continue;
}
if (seen.has(href)) continue;
seen.add(href);

const link = document.createElement('link');
link.rel = 'stylesheet';
Expand Down
12 changes: 6 additions & 6 deletions packages/kit/test/apps/basics/test/cross-platform/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
test.describe.configure({ mode: 'parallel' });

test.describe('a11y', () => {
test('resets focus', async ({ page, clicknav, browserName }) => {

Check warning on line 12 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, windows-latest, chromium, dev)

flaky test: resets focus

retries: 2
const tab = browserName === 'webkit' ? 'Alt+Tab' : 'Tab';

await page.goto('/accessibility/a');
Expand All @@ -33,7 +33,7 @@
expect(await page.evaluate(() => document.documentElement.getAttribute('tabindex'))).toBe(null);
});

test('applies autofocus after a navigation', async ({ page, clicknav }) => {

Check warning on line 36 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, windows-latest, chromium, dev)

flaky test: applies autofocus after a navigation

retries: 2
await page.goto('/accessibility/autofocus/a');

await clicknav('[href="/accessibility/autofocus/b"]');
Expand Down Expand Up @@ -182,7 +182,7 @@
expect(await page.innerHTML('pre')).toBe('1 true leave');
});

test('beforeNavigate is not triggered on redirect', async ({ page, baseURL }) => {

Check warning on line 185 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, ubuntu-latest, firefox, build)

flaky test: beforeNavigate is not triggered on redirect

retries: 2
await page.goto('/navigation-lifecycle/before-navigate/prevent-navigation');

await page.click('[href="/navigation-lifecycle/before-navigate/redirect"]');
Expand Down Expand Up @@ -927,9 +927,9 @@
// we start watching requests
await page.goto('/routing/form-get', { waitUntil: 'load' });

expect(await page.textContent('h1')).toBe('...');
expect(await page.textContent('h2')).toBe('enter');
expect(await page.textContent('h3')).toBe('...');
await expect(page.locator('h1')).toHaveText('...');
await expect(page.locator('h2')).toHaveText('enter');
await expect(page.locator('h3')).toHaveText('...');

/** @type {string[]} */
const requests = [];
Expand All @@ -939,10 +939,10 @@
await page.locator('button').click();

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

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