Skip to content

Commit dfb3bf5

Browse files
authored
replace %svelte.assets% with relative path (#3234)
* replace %svelte.assets% with relative path * changeset * update templates * create-svelte changeset
1 parent e100b42 commit dfb3bf5

File tree

11 files changed

+40
-8
lines changed

11 files changed

+40
-8
lines changed

.changeset/proud-hairs-chew.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+
Replace %svelte.assets% with relative path

.changeset/tasty-islands-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
Update template files to include %svelte.assets%

packages/create-svelte/templates/default/src/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8" />
55
<meta name="description" content="Svelte demo app" />
6-
<link rel="icon" href="/favicon.png" />
6+
<link rel="icon" href="%svelte.assets%/favicon.png" />
77
<meta name="viewport" content="width=device-width, initial-scale=1" />
88
%svelte.head%
99
</head>

packages/create-svelte/templates/skeleton/src/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8" />
55
<meta name="description" content="" />
6-
<link rel="icon" href="/favicon.png" />
6+
<link rel="icon" href="%svelte.assets%/favicon.png" />
77
<meta name="viewport" content="width=device-width, initial-scale=1" />
88
%svelte.head%
99
</head>

packages/kit/src/core/build/build_server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import { set_paths, assets, base } from './runtime/paths.js';
2626
import { set_prerendering } from './runtime/env.js';
2727
import * as user_hooks from ${s(hooks)};
2828
29-
const template = ({ head, body }) => ${s(load_template(cwd, config))
29+
const template = ({ head, body, assets }) => ${s(load_template(cwd, config))
3030
.replace('%svelte.head%', '" + head + "')
31-
.replace('%svelte.body%', '" + body + "')};
31+
.replace('%svelte.body%', '" + body + "')
32+
.replace(/%svelte\.assets%/g, '" + assets + "')};
3233
3334
let read = null;
3435

packages/kit/src/core/dev/plugin.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ export function create_plugin(config, output, cwd, amp) {
218218
router: config.kit.router,
219219
ssr: config.kit.ssr,
220220
target: config.kit.target,
221-
template: ({ head, body }) => {
221+
template: ({ head, body, assets }) => {
222222
let rendered = load_template(cwd, config)
223223
.replace('%svelte.head%', () => head)
224-
.replace('%svelte.body%', () => body);
224+
.replace('%svelte.body%', () => body)
225+
.replace(/%svelte\.assets%/g, assets);
225226

226227
if (amp) {
227228
const result = amp.validateString(rendered);

packages/kit/src/runtime/server/page/render.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,18 @@ export async function render_response({
213213
headers['permissions-policy'] = 'interest-cohort=()';
214214
}
215215

216+
const segments = url.pathname.slice(options.paths.base.length).split('/').slice(2);
217+
const assets =
218+
options.paths.assets || (segments.length > 0 ? segments.map(() => '..').join('/') : '.');
219+
216220
return {
217221
status,
218222
headers,
219-
body: options.template({ head, body })
223+
body: options.template({
224+
head,
225+
body,
226+
assets
227+
})
220228
};
221229
}
222230

packages/kit/test/apps/basics/src/app.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<link rel="icon" type="image/png" href="%svelte.assets%/favicon.png" />
67
%svelte.head%
78
</head>
89
<body>
1.53 KB
Loading

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,17 @@ test.describe.parallel('$app/paths', () => {
10721072
})
10731073
);
10741074
});
1075+
1076+
test('replaces %svelte.assets% in template with relative path', async ({ page }) => {
1077+
await page.goto('/');
1078+
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('./favicon.png');
1079+
1080+
await page.goto('/routing');
1081+
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('./favicon.png');
1082+
1083+
await page.goto('/routing/rest/foo/bar/baz');
1084+
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('../../../../favicon.png');
1085+
});
10751086
});
10761087

10771088
test.describe.parallel('$app/stores', () => {

packages/kit/types/internal.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export interface SSRRenderOptions {
144144
service_worker?: string;
145145
ssr: boolean;
146146
target: string;
147-
template({ head, body }: { head: string; body: string }): string;
147+
template({ head, body, assets }: { head: string; body: string; assets: string }): string;
148148
trailing_slash: TrailingSlash;
149149
}
150150

0 commit comments

Comments
 (0)