-
Notifications
You must be signed in to change notification settings - Fork 0
fix: support optional params in Netlify split mode #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: qa/agent-sveltejs-kit/pr-07-17020/base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/adapter-netlify': patch | ||
| --- | ||
|
|
||
| fix: route requests with omitted optional parameters to split serverless functions |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,12 +145,13 @@ function generate_serverless_functions({ builder, publish, split }) { | |
|
|
||
| // The parts should conform to URLPattern syntax | ||
| // https://docs.netlify.com/build/functions/get-started/?fn-language=ts&data-tab=TypeScript#route-requests | ||
| for (const segment of route.segments) { | ||
| for (const [i, segment] of route.segments.entries()) { | ||
| if (segment.rest) { | ||
| parts.push('*'); | ||
| } else if (segment.dynamic) { | ||
| // URLPattern requires params to start with letters | ||
| parts.push(`:param${parts.length}`); | ||
| const optional = /^\[\[.+\]\]$/.test(segment.content) ? '?' : ''; | ||
| parts.push(`:param${i}${optional}`); | ||
| } else { | ||
| parts.push(segment.content); | ||
| } | ||
|
|
@@ -159,13 +160,15 @@ function generate_serverless_functions({ builder, publish, split }) { | |
| // Netlify handles trailing slashes for us, so we don't need to include them in the pattern | ||
| const pattern = `/${parts.join('/')}`; | ||
| const name = | ||
| FUNCTION_PREFIX + (parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index'); | ||
| FUNCTION_PREFIX + | ||
| (parts.join('-').replace(/[:.]/g, '_').replace(/\?/g, '').replace(/\*/g, '__rest') || | ||
| 'index'); | ||
|
|
||
| // skip routes with identical patterns, they were already folded into another function | ||
| if (seen.has(pattern)) continue; | ||
|
|
||
| const patterns = [pattern, `${pattern === '/' ? '' : pattern}/__data.json`]; | ||
| patterns.forEach((p) => seen.add(p)); | ||
| patterns.forEach((pattern) => seen.add(pattern)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shipwright · HIGH The variable shadowing in 'patterns.forEach((pattern) => seen.add(pattern))' reuses the name 'pattern' for the loop parameter, shadowing the outer 'const pattern'. Impact: The variable shadowing in 'patterns.forEach((pattern) => seen.add(pattern))' reuses the name 'pattern' for the loop parameter, shadowing the outer 'const pattern'. This is confusing and error-prone; a future maintainer modifying the loop body may inadvertently reference the wrong 'pattern'. The original code used 'p' to avoid this. This change reduces clarity without functional benefit. Suggested fix: Review the cited evidence, fix the risk if confirmed, and rerun Shipwright. |
||
|
|
||
| // figure out which lower priority routes should be considered fallbacks | ||
| for (let j = i + 1; j < builder.routes.length; j += 1) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** @type {import('./$types').PageServerLoad} */ | ||
| export function load({ params }) { | ||
| return { optional: params.optional ?? null }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <script> | ||
| let { data } = $props(); | ||
| </script> | ||
|
|
||
| <!-- Netlify uses URLPattern for routing; verify direct SSR works whether the optional param is present or omitted --> | ||
| <p>optional: {data.optional ?? 'none'}</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shipwright · CRITICAL
The function name generation uses 'parts.join('-')' where 'parts' now contains ':param1?'.
Impact: The function name generation uses 'parts.join('-')' where 'parts' now contains ':param1?'. The '.replace(/[:.]/g, '_')' runs before '.replace(/?/g, '')', so the '?' is removed, but the test expects the file 'sveltekit-collection-_param1-article.mjs'. However, for a route with multiple optional params or a rest segment combined with optional params, the '?' removal can create name collisions. More critically, the 's…
Suggested fix: Review the cited evidence, fix the risk if confirmed, and rerun Shipwright.