Skip to content

Commit 0f00498

Browse files
authored
fix: prevent duplicate <link> module preloads (#10442)
fixes #10358 fixes #10379 The path optimization was applied too often, causing relative/absolute path mismatches, causing Vite to add duplicate link module preloads
1 parent fc75511 commit 0f00498

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

.changeset/funny-items-deny.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: prevent duplicate module preload

packages/kit/src/exports/vite/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ function kit({ svelte_config }) {
545545
// E.g. Vite generates `new URL('/asset.png', import.meta).href` for a relative path vs just '/asset.png'.
546546
// That's larger and takes longer to run and also causes an HTML diff between SSR and client
547547
// causing us to do a more expensive hydration check.
548-
const client_base = kit.paths.relative || kit.paths.assets ? './' : kit.paths.base || '/';
548+
const client_base =
549+
kit.paths.relative !== false || kit.paths.assets ? './' : kit.paths.base || '/';
549550

550551
new_config = {
551552
base: ssr ? assets_base(kit) : client_base,

packages/kit/test/apps/basics/test/client.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,27 @@ test.describe('Actions', () => {
802802
await expect(pre).toHaveText('prop: 1, store: 1');
803803
});
804804
});
805+
806+
test.describe('Assets', () => {
807+
test('only one link per stylesheet', async ({ page }) => {
808+
if (process.env.DEV) return;
809+
810+
await page.goto('/');
811+
812+
expect(
813+
await page.evaluate(() => {
814+
const links = Array.from(document.head.querySelectorAll('link[rel=stylesheet]'));
815+
816+
for (let i = 0; i < links.length; ) {
817+
const link = links.shift();
818+
const asset_name = link.href.split('/').at(-1);
819+
if (links.some((link) => link.href.includes(asset_name))) {
820+
return false;
821+
}
822+
}
823+
824+
return true;
825+
})
826+
).toBe(true);
827+
});
828+
});

0 commit comments

Comments
 (0)