Skip to content

Commit

Permalink
fix: prevent duplicate <link> module preloads (#10442)
Browse files Browse the repository at this point in the history
fixes #10358
fixes #10379
The path optimization was applied too often, causing relative/absolute path mismatches, causing Vite to add duplicate link module preloads
  • Loading branch information
eltigerchino committed Jul 31, 2023
1 parent fc75511 commit 0f00498
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/funny-items-deny.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: prevent duplicate module preload
3 changes: 2 additions & 1 deletion packages/kit/src/exports/vite/index.js
Expand Up @@ -545,7 +545,8 @@ function kit({ svelte_config }) {
// E.g. Vite generates `new URL('/asset.png', import.meta).href` for a relative path vs just '/asset.png'.
// That's larger and takes longer to run and also causes an HTML diff between SSR and client
// causing us to do a more expensive hydration check.
const client_base = kit.paths.relative || kit.paths.assets ? './' : kit.paths.base || '/';
const client_base =
kit.paths.relative !== false || kit.paths.assets ? './' : kit.paths.base || '/';

new_config = {
base: ssr ? assets_base(kit) : client_base,
Expand Down
24 changes: 24 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Expand Up @@ -802,3 +802,27 @@ test.describe('Actions', () => {
await expect(pre).toHaveText('prop: 1, store: 1');
});
});

test.describe('Assets', () => {
test('only one link per stylesheet', async ({ page }) => {
if (process.env.DEV) return;

await page.goto('/');

expect(
await page.evaluate(() => {
const links = Array.from(document.head.querySelectorAll('link[rel=stylesheet]'));

for (let i = 0; i < links.length; ) {
const link = links.shift();
const asset_name = link.href.split('/').at(-1);
if (links.some((link) => link.href.includes(asset_name))) {
return false;
}
}

return true;
})
).toBe(true);
});
});

0 comments on commit 0f00498

Please sign in to comment.