[remix] Support optional static path segments (#9607)

Fixes https://github.com/orgs/vercel/discussions/1707.
This commit is contained in:
Nathan Rajlich
2023-03-06 16:50:37 -08:00
committed by GitHub
parent 2e8423e24e
commit 57e8ec13cf
7 changed files with 235 additions and 9 deletions

View File

@@ -138,12 +138,24 @@ export function getPathFromRoute(
for (const currentRoute of getRouteIterator(route, routes)) {
if (!currentRoute.path) continue;
pathParts.push(
currentRoute.path.replace(/:(.+)\?/g, (_, name) => `(:${name})`)
);
rePathParts.push(currentRoute.path);
const currentRouteParts = currentRoute.path.split('/').reverse();
for (const part of currentRouteParts) {
if (part.endsWith('?')) {
if (part.startsWith(':')) {
// Optional path parameter
pathParts.push(`(${part.substring(0, part.length - 1)})`);
rePathParts.push(part);
} else {
// Optional static segment
const p = `(${part.substring(0, part.length - 1)})`;
pathParts.push(p);
rePathParts.push(`${p}?`);
}
} else {
pathParts.push(part);
rePathParts.push(part);
}
}
}
const path = pathParts.reverse().join('/');