Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: undefined is no longer a valid value for paths.relative #11185

Merged
merged 12 commits into from
Dec 10, 2023
5 changes: 5 additions & 0 deletions .changeset/tall-suns-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

breaking: undefined is no longer a valid value for paths.relative
8 changes: 4 additions & 4 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const get_defaults = (prefix = '') => ({
paths: {
base: '',
assets: '',
relative: undefined
relative: true
},
prerender: {
concurrency: 1,
Expand Down Expand Up @@ -321,7 +321,7 @@ validate_paths(
{
base: '/path/to/base',
assets: '',
relative: undefined
relative: true
}
);

Expand All @@ -333,7 +333,7 @@ validate_paths(
{
base: '',
assets: 'https://cdn.example.com',
relative: undefined
relative: true
}
);

Expand All @@ -346,7 +346,7 @@ validate_paths(
{
base: '/path/to/base',
assets: 'https://cdn.example.com',
relative: undefined
relative: true
}
);

Expand Down
8 changes: 1 addition & 7 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,7 @@ const options = object(

return input;
}),
relative: validate(undefined, (input, keypath) => {
if (typeof input !== 'boolean') {
throw new Error(`${keypath} option must be a boolean or undefined`);
}

return input;
})
relative: boolean(true)
}),

prerender: object({
Expand Down
11 changes: 7 additions & 4 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,18 @@ export interface KitConfig {
*/
base?: '' | `/${string}`;
/**
* Whether to use relative asset paths. By default, if `paths.assets` is not external, SvelteKit will replace `%sveltekit.assets%` with a relative path and use relative paths to reference build artifacts, but `base` and `assets` imported from `$app/paths` will be as specified in your config.
* Whether to use relative asset paths.
*
* If `true`, `base` and `assets` imported from `$app/paths` will be replaced with relative asset paths during server-side rendering, resulting in portable HTML.
* If `true`, `base` and `assets` imported from `$app/paths` will be replaced with relative asset paths during server-side rendering, resulting in more portable HTML.
* If `false`, `%sveltekit.assets%` and references to build artifacts will always be root-relative paths, unless `paths.assets` is an external URL
*
* If your app uses a `<base>` element, you should set this to `false`, otherwise asset URLs will incorrectly be resolved against the `<base>` URL rather than the current page.
* @default undefined
*
* In 1.0, `undefined` was a valid value, which was set by default. In that case, if `paths.assets` was not external, SvelteKit would replace `%sveltekit.assets%` with a relative path and use relative paths to reference build artifacts, but `base` and `assets` imported from `$app/paths` would be as specified in your config.
*
* @default true
*/
relative?: boolean | undefined;
relative?: boolean;
};
/**
* See [Prerendering](https://kit.svelte.dev/docs/page-options#prerender).
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export async function render_response({
let base_expression = s(paths.base);

// if appropriate, use relative paths for greater portability
if (paths.relative !== false && !state.prerendering?.fallback) {
if (paths.relative && !state.prerendering?.fallback) {
const segments = event.url.pathname.slice(paths.base.length).split('/').slice(2);

base = segments.map(() => '..').join('/') || '.';
Expand Down
18 changes: 5 additions & 13 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,24 +642,16 @@ test.describe('$app/environment', () => {
});

test.describe('$app/paths', () => {
test('includes paths', async ({ page }) => {
test('includes paths', async ({ page, javaScriptEnabled }) => {
await page.goto('/paths');

expect(await page.innerHTML('pre')).toBe(
JSON.stringify({
base: '',
assets: ''
})
);
let base = javaScriptEnabled ? '' : '.';
expect(await page.innerHTML('pre')).toBe(JSON.stringify({ base, assets: base }));

await page.goto('/paths/deeply/nested');

expect(await page.innerHTML('pre')).toBe(
JSON.stringify({
base: '',
assets: ''
})
);
base = javaScriptEnabled ? '' : '../..';
expect(await page.innerHTML('pre')).toBe(JSON.stringify({ base, assets: base }));
});

// some browsers will re-request assets after a `pushState`
Expand Down
Loading