Skip to content

Commit

Permalink
fix: prerender optional parameters as empty when entries contains `…
Browse files Browse the repository at this point in the history
…'*'` (#11178)
  • Loading branch information
LorisSigrist authored Dec 12, 2023
1 parent a2fe7ab commit a7ed70c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-jars-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: prerender optional parameters as empty when `entries` contains `'*'`
8 changes: 6 additions & 2 deletions packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,12 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
if (entry === '*') {
for (const [id, prerender] of prerender_map) {
if (prerender) {
if (id.includes('[')) continue;
const path = `/${get_route_segments(id).join('/')}`;
// remove optional parameters from the route
const segments = get_route_segments(id).filter((segment) => !segment.startsWith('[['));
const processed_id = '/' + segments.join('/');

if (processed_id.includes('[')) continue;
const path = `/${get_route_segments(processed_id).join('/')}`;
enqueue(null, config.paths.base + path);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ export interface KitConfig {
*/
crawl?: boolean;
/**
* 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).
* 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).
* @default ["*"]
*/
entries?: Array<'*' | `/${string}`>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="/optional-params/with-value">Path with Value</a>
5 changes: 5 additions & 0 deletions packages/kit/test/prerendering/basics/test/tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,8 @@ test('prerenders responses with immutable Headers', () => {
const content = read('immutable-headers');
expect(content).toMatch('foo');
});

test('prerenders paths with optional parameters with empty values', () => {
const content = read('optional-params.html');
expect(content).includes('Path with Value');
});
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ declare module '@sveltejs/kit' {
*/
crawl?: boolean;
/**
* 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).
* 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).
* @default ["*"]
*/
entries?: Array<'*' | `/${string}`>;
Expand Down

0 comments on commit a7ed70c

Please sign in to comment.