@sveltejs/adapter-vercel
Advanced tools
Comparing version 5.1.0 to 5.1.1
15
index.js
@@ -6,3 +6,3 @@ import fs from 'node:fs'; | ||
import esbuild from 'esbuild'; | ||
import { get_pathname } from './utils.js'; | ||
import { get_pathname, pattern_to_src } from './utils.js'; | ||
@@ -309,14 +309,3 @@ const name = '@sveltejs/adapter-vercel'; | ||
const pattern = route.pattern.toString(); | ||
let src = pattern | ||
// remove leading / and trailing $/ | ||
.slice(1, -2) | ||
// replace escaped \/ with / | ||
.replace(/\\\//g, '/'); | ||
// replace the root route "^/" with "^/?" | ||
if (src === '^/') { | ||
src = '^/?'; | ||
} | ||
const src = pattern_to_src(pattern); | ||
const name = functions.get(pattern) ?? 'fn-0'; | ||
@@ -323,0 +312,0 @@ |
{ | ||
"name": "@sveltejs/adapter-vercel", | ||
"version": "5.1.0", | ||
"version": "5.1.1", | ||
"description": "A SvelteKit adapter that creates a Vercel app", | ||
@@ -36,3 +36,3 @@ "repository": { | ||
"vitest": "^1.2.0", | ||
"@sveltejs/kit": "^2.4.1" | ||
"@sveltejs/kit": "^2.5.3" | ||
}, | ||
@@ -39,0 +39,0 @@ "peerDependencies": { |
70
utils.js
@@ -5,20 +5,66 @@ /** @param {import("@sveltejs/kit").RouteDefinition<any>} route */ | ||
return route.segments | ||
const pathname = route.segments | ||
.map((segment) => { | ||
if (!segment.dynamic) { | ||
return segment.content; | ||
return '/' + segment.content; | ||
} | ||
const parts = segment.content.split(/\[(.+?)\](?!\])/); | ||
return parts | ||
.map((content, j) => { | ||
if (j % 2) { | ||
return `$${i++}`; | ||
} else { | ||
return content; | ||
} | ||
}) | ||
.join(''); | ||
let result = ''; | ||
if ( | ||
parts.length === 3 && | ||
!parts[0] && | ||
!parts[2] && | ||
(parts[1].startsWith('...') || parts[1][0] === '[') | ||
) { | ||
// Special case: segment is a single optional or rest parameter. | ||
// In that case we don't prepend a slash (also see comment in pattern_to_src). | ||
result = `$${i++}`; | ||
} else { | ||
result = | ||
'/' + | ||
parts | ||
.map((content, j) => { | ||
if (j % 2) { | ||
return `$${i++}`; | ||
} else { | ||
return content; | ||
} | ||
}) | ||
.join(''); | ||
} | ||
return result; | ||
}) | ||
.join('/'); | ||
.join(''); | ||
return pathname[0] === '/' ? pathname.slice(1) : pathname; | ||
} | ||
/** | ||
* Adjusts the stringified route regex for Vercel's routing system | ||
* @param {string} pattern stringified route regex | ||
*/ | ||
export function pattern_to_src(pattern) { | ||
let src = pattern | ||
// remove leading / and trailing $/ | ||
.slice(1, -2) | ||
// replace escaped \/ with / | ||
.replace(/\\\//g, '/'); | ||
// replace the root route "^/" with "^/?" | ||
if (src === '^/') { | ||
src = '^/?'; | ||
} | ||
// Move non-capturing groups that swallow slashes into their following capturing groups. | ||
// This is necessary because during ISR we're using the regex to construct the __pathname | ||
// query parameter: In case of a route like [required]/[...rest] we need to turn them | ||
// into $1$2 and not $1/$2, because if [...rest] is empty, we don't want to have a trailing | ||
// slash in the __pathname query parameter which wasn't there in the original URL, as that | ||
// could result in a false trailing slash redirect in the SvelteKit runtime, leading to infinite redirects. | ||
src = src.replace(/\(\?:\/\((.+?)\)\)/g, '(/$1)'); | ||
return src; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30324
802