Skip to content

Commit 6885211

Browse files
authored
always add trailing slash to base path (#9343)
* always add trailing slash to base path - fixes #9341 * tidy up * Update packages/kit/src/runtime/server/respond.js
1 parent 7225d20 commit 6885211

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed
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: use correct relative paths when rendering base path

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,12 @@ export async function render_response({
9595

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

100-
if (segments.length === 1 && paths.base !== '') {
101-
// if we're on `/my-base-path`, relative links need to start `./my-base-path` rather than `.`
102-
base = `./${paths.base.split('/').at(-1)}`;
100+
base = segments.map(() => '..').join('/') || '.';
103101

104-
base_expression = `new URL(${s(base)}, location).pathname`;
105-
} else {
106-
base =
107-
segments
108-
.slice(2)
109-
.map(() => '..')
110-
.join('/') || '.';
111-
112-
// resolve e.g. '../..' against current location, then remove trailing slash
113-
base_expression = `new URL(${s(base)}, location).pathname.slice(0, -1)`;
114-
}
102+
// resolve e.g. '../..' against current location, then remove trailing slash
103+
base_expression = `new URL(${s(base)}, location).pathname.slice(0, -1)`;
115104

116105
if (!paths.assets || (paths.assets[0] === '/' && paths.assets !== SVELTE_KIT_ASSETS)) {
117106
assets = base;

packages/kit/src/runtime/server/respond.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ export async function respond(request, options, manifest, state) {
176176
try {
177177
// determine whether we need to redirect to add/remove a trailing slash
178178
if (route && !is_data_request) {
179-
if (route.page) {
179+
// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
180+
// regardless of the `trailingSlash` route option
181+
if (url.pathname === base || url.pathname === base + '/') {
182+
trailing_slash = 'always';
183+
} else if (route.page) {
180184
const nodes = await Promise.all([
181185
// we use == here rather than === because [undefined] serializes as "[null]"
182186
...route.page.layouts.map((n) => (n == undefined ? n : manifest._.nodes[n]())),

packages/kit/test/apps/options-2/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test.describe('paths', () => {
2727
test('uses relative paths during SSR', async ({ page, javaScriptEnabled }) => {
2828
await page.goto('/basepath');
2929

30-
let base = javaScriptEnabled ? '/basepath' : './basepath';
30+
let base = javaScriptEnabled ? '/basepath' : '.';
3131
expect(await page.textContent('[data-testid="base"]')).toBe(`base: ${base}`);
3232
expect(await page.textContent('[data-testid="assets"]')).toBe(`assets: ${base}`);
3333

0 commit comments

Comments
 (0)