Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-stingrays-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: use correct relative paths when rendering base path
19 changes: 4 additions & 15 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,12 @@ export async function render_response({

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

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

base_expression = `new URL(${s(base)}, location).pathname`;
} else {
base =
segments
.slice(2)
.map(() => '..')
.join('/') || '.';

// resolve e.g. '../..' against current location, then remove trailing slash
base_expression = `new URL(${s(base)}, location).pathname.slice(0, -1)`;
}
// resolve e.g. '../..' against current location, then remove trailing slash
base_expression = `new URL(${s(base)}, location).pathname.slice(0, -1)`;

if (!paths.assets || (paths.assets[0] === '/' && paths.assets !== SVELTE_KIT_ASSETS)) {
assets = base;
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ export async function respond(request, options, manifest, state) {
try {
// determine whether we need to redirect to add/remove a trailing slash
if (route && !is_data_request) {
if (route.page) {
// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
// regardless of the `trailingSlash` route option
if (url.pathname === base || url.pathname === base + '/') {
trailing_slash = 'always';
} else if (route.page) {
const nodes = await Promise.all([
// we use == here rather than === because [undefined] serializes as "[null]"
...route.page.layouts.map((n) => (n == undefined ? n : manifest._.nodes[n]())),
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test.describe('paths', () => {
test('uses relative paths during SSR', async ({ page, javaScriptEnabled }) => {
await page.goto('/basepath');

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

Expand Down