Skip to content

Commit a7ed70c

Browse files
authored
fix: prerender optional parameters as empty when entries contains '*' (#11178)
1 parent a2fe7ab commit a7ed70c

File tree

6 files changed

+19
-4
lines changed

6 files changed

+19
-4
lines changed

.changeset/good-jars-relax.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: prerender optional parameters as empty when `entries` contains `'*'`

packages/kit/src/core/postbuild/prerender.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,12 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
426426
if (entry === '*') {
427427
for (const [id, prerender] of prerender_map) {
428428
if (prerender) {
429-
if (id.includes('[')) continue;
430-
const path = `/${get_route_segments(id).join('/')}`;
429+
// remove optional parameters from the route
430+
const segments = get_route_segments(id).filter((segment) => !segment.startsWith('[['));
431+
const processed_id = '/' + segments.join('/');
432+
433+
if (processed_id.includes('[')) continue;
434+
const path = `/${get_route_segments(processed_id).join('/')}`;
431435
enqueue(null, config.paths.base + path);
432436
}
433437
}

packages/kit/src/exports/public.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export interface KitConfig {
498498
*/
499499
crawl?: boolean;
500500
/**
501-
* An array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]`, because SvelteKit doesn't know what value the parameters should have).
501+
* An array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all routes containing no required `[parameters]` with optional parameters included as being empty (since SvelteKit doesn't know what value any parameters should have).
502502
* @default ["*"]
503503
*/
504504
entries?: Array<'*' | `/${string}`>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="/optional-params/with-value">Path with Value</a>

packages/kit/test/prerendering/basics/test/tests.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,8 @@ test('prerenders responses with immutable Headers', () => {
244244
const content = read('immutable-headers');
245245
expect(content).toMatch('foo');
246246
});
247+
248+
test('prerenders paths with optional parameters with empty values', () => {
249+
const content = read('optional-params.html');
250+
expect(content).includes('Path with Value');
251+
});

packages/kit/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ declare module '@sveltejs/kit' {
480480
*/
481481
crawl?: boolean;
482482
/**
483-
* An array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]`, because SvelteKit doesn't know what value the parameters should have).
483+
* An array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all routes containing no required `[parameters]` with optional parameters included as being empty (since SvelteKit doesn't know what value any parameters should have).
484484
* @default ["*"]
485485
*/
486486
entries?: Array<'*' | `/${string}`>;

0 commit comments

Comments
 (0)