[remix] Fix support for optional path params (#9590)

Changes the way that routes with optional params are stored on Vercel to not use `?` in the name, because it's not possible to have a route destination with a `?` that's pointing to a function.

So instead of the function with a name such as `:lang?`, it will be stored on Vercel with a name like `(:lang)`, which can be routed to.
This commit is contained in:
Nathan Rajlich
2023-03-02 15:28:26 -08:00
committed by GitHub
parent 684f69bc5b
commit fb37ad22ab
4 changed files with 181 additions and 35 deletions

View File

@@ -2,17 +2,18 @@ import { getRegExpFromPath } from '../src/utils';
describe('getRegExpFromPath()', () => {
describe('paths without parameters', () => {
it.each([{ path: 'index' }, { path: 'api/hello' }, { path: 'projects' }])(
'should return `false` for "$path"',
({ path }) => {
expect(getRegExpFromPath(path)).toEqual(false);
}
);
it.each([
{ path: '/index' },
{ path: '/api/hello' },
{ path: '/projects' },
])('should return `false` for "$path"', ({ path }) => {
expect(getRegExpFromPath(path)).toEqual(false);
});
});
describe.each([
{
path: '*',
path: '/:params+',
urls: [
{
url: '/',
@@ -37,7 +38,7 @@ describe('getRegExpFromPath()', () => {
],
},
{
path: 'projects/*',
path: '/projects/:params+',
urls: [
{
url: '/',
@@ -58,7 +59,7 @@ describe('getRegExpFromPath()', () => {
],
},
{
path: ':foo',
path: '/:foo',
urls: [
{
url: '/',
@@ -79,7 +80,7 @@ describe('getRegExpFromPath()', () => {
],
},
{
path: 'blog/:id/edit',
path: '/blog/:id/edit',
urls: [
{
url: '/',
@@ -107,6 +108,65 @@ describe('getRegExpFromPath()', () => {
},
],
},
{
path: '/:lang?',
urls: [
{
url: '/',
expected: true,
},
{
url: '/en',
expected: true,
},
{
url: '/en/other',
expected: false,
},
],
},
{
path: '/:lang?/other',
urls: [
{
url: '/other',
expected: true,
},
{
url: '/en/other',
expected: true,
},
{
url: '/',
expected: false,
},
{
url: '/another',
expected: false,
},
],
},
{
path: '/:lang?/:pid',
urls: [
{
url: '/123',
expected: true,
},
{
url: '/en/123',
expected: true,
},
{
url: '/',
expected: false,
},
{
url: '/en/foo/bar',
expected: false,
},
],
},
])('with path "$path"', ({ path, urls }) => {
const re = getRegExpFromPath(path) as RegExp;