Skip to content

Commit 107f767

Browse files
eltigerchinodummdidummRich-Harris
authored
fix: avoid copying unused SSR stylesheets to client assets (#13069)
* don't copy ssr css * changeset * fix lint error * Update packages/kit/src/exports/vite/index.js * tweak * more detailed comment --------- Co-authored-by: Simon H <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 281ae80 commit 107f767

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

.changeset/twelve-gifts-wait.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: avoid copying SSR stylesheets to client assets

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,11 +1064,25 @@ async function kit({ svelte_config }) {
10641064
throw stackless(error.stack ?? error.message);
10651065
}
10661066

1067-
copy(
1068-
`${out}/server/${kit.appDir}/immutable/assets`,
1069-
`${out}/client/${kit.appDir}/immutable/assets`
1067+
// We use `build.ssrEmitAssets` so that asset URLs created from
1068+
// imports in server-only modules correspond to files in the build,
1069+
// but we don't want to copy over CSS imports as these are already
1070+
// accounted for in the client bundle. In most cases it would be
1071+
// a no-op, but for SSR builds `url(...)` paths are handled
1072+
// differently (relative for client, absolute for server)
1073+
// resulting in different hashes, and thus duplication
1074+
const ssr_stylesheets = new Set(
1075+
Object.values(server_manifest)
1076+
.map((chunk) => chunk.css ?? [])
1077+
.flat()
10701078
);
10711079

1080+
const assets_path = `${kit.appDir}/immutable/assets`;
1081+
1082+
copy(`${out}/server/${assets_path}`, `${out}/client/${assets_path}`, {
1083+
filter: (basename) => !ssr_stylesheets.has(`${assets_path}/${basename}`)
1084+
});
1085+
10721086
/** @type {import('vite').Manifest} */
10731087
const client_manifest = JSON.parse(read(`${out}/client/.vite/manifest.json`));
10741088

0 commit comments

Comments
 (0)