Skip to content

Commit

Permalink
replace %svelte.assets% with relative path (#3234)
Browse files Browse the repository at this point in the history
* replace %svelte.assets% with relative path

* changeset

* update templates

* create-svelte changeset
  • Loading branch information
Rich-Harris authored Jan 7, 2022
1 parent e100b42 commit dfb3bf5
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-hairs-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Replace %svelte.assets% with relative path
5 changes: 5 additions & 0 deletions .changeset/tasty-islands-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Update template files to include %svelte.assets%
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/default/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="description" content="Svelte demo app" />
<link rel="icon" href="/favicon.png" />
<link rel="icon" href="%svelte.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/skeleton/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="description" content="" />
<link rel="icon" href="/favicon.png" />
<link rel="icon" href="%svelte.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import { set_paths, assets, base } from './runtime/paths.js';
import { set_prerendering } from './runtime/env.js';
import * as user_hooks from ${s(hooks)};
const template = ({ head, body }) => ${s(load_template(cwd, config))
const template = ({ head, body, assets }) => ${s(load_template(cwd, config))
.replace('%svelte.head%', '" + head + "')
.replace('%svelte.body%', '" + body + "')};
.replace('%svelte.body%', '" + body + "')
.replace(/%svelte\.assets%/g, '" + assets + "')};
let read = null;
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@ export function create_plugin(config, output, cwd, amp) {
router: config.kit.router,
ssr: config.kit.ssr,
target: config.kit.target,
template: ({ head, body }) => {
template: ({ head, body, assets }) => {
let rendered = load_template(cwd, config)
.replace('%svelte.head%', () => head)
.replace('%svelte.body%', () => body);
.replace('%svelte.body%', () => body)
.replace(/%svelte\.assets%/g, assets);

if (amp) {
const result = amp.validateString(rendered);
Expand Down
10 changes: 9 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,18 @@ export async function render_response({
headers['permissions-policy'] = 'interest-cohort=()';
}

const segments = url.pathname.slice(options.paths.base.length).split('/').slice(2);
const assets =
options.paths.assets || (segments.length > 0 ? segments.map(() => '..').join('/') : '.');

return {
status,
headers,
body: options.template({ head, body })
body: options.template({
head,
body,
assets
})
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/png" href="%svelte.assets%/favicon.png" />
%svelte.head%
</head>
<body>
Expand Down
Binary file added packages/kit/test/apps/basics/static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,17 @@ test.describe.parallel('$app/paths', () => {
})
);
});

test('replaces %svelte.assets% in template with relative path', async ({ page }) => {
await page.goto('/');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('./favicon.png');

await page.goto('/routing');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('./favicon.png');

await page.goto('/routing/rest/foo/bar/baz');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('../../../../favicon.png');
});
});

test.describe.parallel('$app/stores', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface SSRRenderOptions {
service_worker?: string;
ssr: boolean;
target: string;
template({ head, body }: { head: string; body: string }): string;
template({ head, body, assets }: { head: string; body: string; assets: string }): string;
trailing_slash: TrailingSlash;
}

Expand Down

0 comments on commit dfb3bf5

Please sign in to comment.