Skip to content
Open
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/famous-otters-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: don't record a trailing slash for a prerendered root page combined with a base path
1 change: 1 addition & 0 deletions packages/kit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
!/src/core/adapt/fixtures/*/.svelte-kit
!/test/node_modules
/test/apps/basics/test/errors.json
/test/prerendering/paths-base/prerendered-paths.json
/types/*.map
.custom-out-dir

Expand Down
12 changes: 10 additions & 2 deletions packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
return file;
}

/**
* @param {string} path
*/
function strip_base_only_trailing_slash(path) {
const base_root = `${config.paths.base}/`;
return path === base_root && base_root.length > 1 ? config.paths.base : path;
}

const files = new Set(walk(`${out}/client`).map(posixify));
files.add(`${config.appDir}/env.js`);

Expand Down Expand Up @@ -445,7 +453,7 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
location: resolved
});

prerendered.paths.push(decoded);
prerendered.paths.push(strip_base_only_trailing_slash(decoded));
}
}
} else {
Expand Down Expand Up @@ -487,7 +495,7 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
});
}

prerendered.paths.push(decoded);
prerendered.paths.push(strip_base_only_trailing_slash(decoded));
} else if (response_type !== OK) {
handle_http_error({ status: response.status, path: decoded, referrer, referenceType });
}
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/exports/env/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @import { EnvVarConfig } from '@sveltejs/kit' */

// tsc otherwise reports EnvVarConfig as unused since it's only referenced in a @template bound
export {};
Comment thread
Bishwas-py marked this conversation as resolved.

/**
* Utility for defining [environment variables](https://svelte.dev/docs/kit/environment-variables),
* which are made available via `$app/env/public` and `$app/env/private`.
Expand Down
13 changes: 11 additions & 2 deletions packages/kit/test/prerendering/paths-base/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import adapter from '../../../../adapter-static/index.js';
import { writeFileSync } from 'node:fs';
import static_adapter from '../../../../adapter-static/index.js';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),
adapter: {
name: 'test',
async adapt(builder) {
// capture what adapters actually receive for prerendered paths, so
// tests can assert on it directly instead of inferring it from output
writeFileSync('./prerendered-paths.json', JSON.stringify(builder.prerendered.paths));
await static_adapter().adapt(builder);
}
},

paths: {
base: '/path-base',
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/test/prerendering/paths-base/test/tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const build = fileURLToPath(new URL('../build', import.meta.url));
/** @param {string} file */
const read = (file) => fs.readFileSync(`${build}/${file}`, 'utf-8');

test('root page with a base path is recorded without a trailing slash', () => {
const paths = JSON.parse(
fs.readFileSync(fileURLToPath(new URL('../prerendered-paths.json', import.meta.url)), 'utf-8')
);
assert.ok(paths.includes('/path-base'));
assert.ok(!paths.includes('/path-base/'));
});

test('prerenders /path-base', () => {
const content = read('index.html');
assert.ok(content.includes('favicon.png') && content.includes('nested'));
Expand Down
16 changes: 8 additions & 8 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ declare module '@sveltejs/kit' {
}>;
export const VERSION: string;
class HttpError_1 {

constructor(status: number, body: {
message: string;
} extends App.Error ? (App.Error | string | undefined) : App.Error);
Expand All @@ -3035,7 +3035,7 @@ declare module '@sveltejs/kit' {
toString(): string;
}
class Redirect_1 {

constructor(status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308, location: string);
status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
location: string;
Expand Down Expand Up @@ -3668,9 +3668,9 @@ declare module '$app/server' {
*
* */
function live<Output>(fn: (arg: void) => RemoteLiveQueryUserFunctionReturnType<Output>): RemoteLiveQueryFunction<void, Output>;

function live<Input, Output>(validate: "unchecked", fn: (arg: Input) => RemoteLiveQueryUserFunctionReturnType<Output>): RemoteLiveQueryFunction<Input, Output>;

function live<Schema extends StandardSchemaV1, Output>(schema: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => RemoteLiveQueryUserFunctionReturnType<Output>): RemoteLiveQueryFunction<StandardSchemaV1.InferInput<Schema>, Output, StandardSchemaV1.InferOutput<Schema>>;
}
/**
Expand Down Expand Up @@ -3837,11 +3837,11 @@ declare module '$app/state' {

declare module '$app/stores' {
export function getStores(): {

page: typeof page;

navigating: typeof navigating;

updated: typeof updated;
};
/**
Expand Down Expand Up @@ -4025,4 +4025,4 @@ declare module '$app/types' {
export type Asset = ReturnType<AppTypes['Asset']>;
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
Loading