Skip to content

Commit c8f7ac3

Browse files
authored
fix: make paths in .css assets relative (#14262)
* make paths in .css files relative * fix, add test * changeset * unused * use copy
1 parent 0852d7e commit c8f7ac3

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

.changeset/four-insects-shave.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: make paths in .css assets relative

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,10 +1078,30 @@ async function kit({ svelte_config }) {
10781078
);
10791079

10801080
const assets_path = `${kit.appDir}/immutable/assets`;
1081+
const server_assets = `${out}/server/${assets_path}`;
1082+
const client_assets = `${out}/client/${assets_path}`;
10811083

1082-
copy(`${out}/server/${assets_path}`, `${out}/client/${assets_path}`, {
1083-
filter: (basename) => !ssr_stylesheets.has(`${assets_path}/${basename}`)
1084-
});
1084+
if (fs.existsSync(server_assets)) {
1085+
for (const file of fs.readdirSync(server_assets)) {
1086+
const src = `${server_assets}/${file}`;
1087+
const dest = `${client_assets}/${file}`;
1088+
1089+
if (fs.existsSync(dest) || ssr_stylesheets.has(`${assets_path}/${file}`)) {
1090+
continue;
1091+
}
1092+
1093+
if (file.endsWith('.css')) {
1094+
// make absolute paths in CSS relative, for portability
1095+
const content = fs
1096+
.readFileSync(src, 'utf-8')
1097+
.replaceAll(`${kit.paths.base}/${assets_path}`, '.');
1098+
1099+
fs.writeFileSync(src, content);
1100+
}
1101+
1102+
copy(src, dest);
1103+
}
1104+
}
10851105

10861106
/** @type {import('vite').Manifest} */
10871107
const client_manifest = JSON.parse(read(`${out}/client/.vite/manifest.json`));

packages/kit/test/apps/basics/src/routes/read-file/+page.server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { dev } from '$app/environment';
22
import { read } from '$app/server';
33
import auto from './[auto].txt';
44
import url from './[url].txt?url';
5+
import styles from './[styles].css?url';
56

67
/** @type {Record<string, { default: string }>} */
78
const local_glob = import.meta.glob('./assets/**', {
@@ -23,6 +24,7 @@ export async function load() {
2324
return {
2425
auto: await read(auto).text(),
2526
url: await read(url).text(),
27+
styles: await read(styles).text(),
2628
local_glob: await read(local_glob['./assets/[file].txt'].default).text(),
2729
external_glob: await read(Object.values(external_glob)[0].default).text(),
2830
svg: await read(local_glob['./assets/icon.svg'].default).text()

packages/kit/test/apps/basics/src/routes/read-file/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
<p data-testid="auto">{data.auto}</p>
66
<p data-testid="url">{data.url}</p>
7+
<p data-testid="styles">{data.styles}</p>
78
<p data-testid="local_glob">{data.local_glob}</p>
89
<p data-testid="external_glob">{data.external_glob}</p>
910
<div data-testid="svg">{@html data.svg}</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.logo {
2+
background: url(../asset-import/large.jpg);
3+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ test.describe('$app/server', () => {
10721072

10731073
const auto = await page.textContent('[data-testid="auto"]');
10741074
const url = await page.textContent('[data-testid="url"]');
1075+
const styles = await page.textContent('[data-testid="styles"]');
10751076
const local_glob = await page.textContent('[data-testid="local_glob"]');
10761077
const external_glob = await page.textContent('[data-testid="external_glob"]');
10771078
const svg = await page.innerHTML('[data-testid="svg"]');
@@ -1084,5 +1085,8 @@ test.describe('$app/server', () => {
10841085
'Imported with url glob from the read-file test in basics. Placed here outside the app folder to force a /@fs prefix 😎'
10851086
);
10861087
expect(svg).toContain('<rect width="24" height="24" rx="2" fill="#ff3e00"></rect>');
1088+
1089+
// check that paths in .css files are relative
1090+
expect(styles).toContain('url(.');
10871091
});
10881092
});

0 commit comments

Comments
 (0)