Skip to content
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

Fix patterns in custom-route destination failing to compile #10291

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions packages/next/next-server/server/lib/path-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,18 @@ export default (customRoute = false) => {
matcherOptions
)

return (pathname: string | null | undefined, params?: any) => {
const wrappedMatcher = (
pathname: string | null | undefined,
params?: any
) => {
const res = pathname == null ? false : matcher(pathname)
if (!res) {
return false
}

if (customRoute) {
const newParams: { [k: string]: string } = {}
for (const key of keys) {
// unnamed matches should always be a number while named
// should be a string
if (typeof key.name === 'number') {
newParams[key.name + 1 + ''] = (res.params as any)[key.name + '']
delete (res.params as any)[key.name + '']
}
}
res.params = {
...res.params,
...newParams,
}
}

return { ...params, ...res.params }
}
return wrappedMatcher
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,12 @@ export default class Server {
fn: async (_req, res, params, _parsedUrl) => {
const parsedDestination = parseUrl(route.destination, true)
const destQuery = parsedDestination.query
let destinationCompiler = compilePathToRegex(
`${parsedDestination.pathname!}${parsedDestination.hash || ''}`
)
let destToCompile = `${parsedDestination.pathname!}${parsedDestination.hash ||
''}`

let destinationCompiler = compilePathToRegex(destToCompile, {
validate: false,
Copy link
Member

@Timer Timer Jan 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still on the fence about this, but this is required if we want to support :0.

Can we only skip validation for :0 somehow?

Alternative is still to disallow :0 syntax.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can skip validation for :0 by doing the initial commit here which replaces the patterns instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the mentioned issue is not limited to unnamed regexes but named also. I added a test to reflect this here: https://github.com/zeit/next.js/pull/10291/files#diff-40a2f83c72ec325a3777e5ef9df5b60eR293

})
let newUrl

Object.keys(destQuery).forEach(key => {
Expand Down
14 changes: 12 additions & 2 deletions test/integration/custom-routes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
},
{
source: '/api-hello-regex/(.*)',
destination: '/api/hello?name=:1',
destination: '/api/hello?name=:0',
},
{
source: '/api-hello-param/:name',
Expand Down Expand Up @@ -138,14 +138,24 @@ module.exports = {
},
{
source: '/unnamed/(first|second)/(.*)',
destination: '/:1/:2',
destination: '/:0/:1',
permanent: false,
},
{
source: '/named-like-unnamed/:0',
destination: '/:0',
permanent: false,
},
{
source: '/unnamed-params/nested/(.*?)/:test/(.*)',
destination: '/another/:0/:test/:1',
permanent: false,
},
{
source: '/docs/:first(integrations|now-cli)/v2:second(.*)',
destination: '/:first/:second',
permanent: false,
},
]
},

Expand Down
50 changes: 47 additions & 3 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ const runTests = (isDev = false) => {
it('should handle api rewrite with un-named param successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello-regex/hello/world')
expect(JSON.parse(data)).toEqual({
query: { '1': 'hello/world', name: 'hello/world' },
query: { '0': 'hello/world', name: 'hello/world' },
})
})

Expand All @@ -276,6 +276,34 @@ const runTests = (isDev = false) => {
expect(JSON.parse(data)).toEqual({ query: { name: 'hello' } })
})

it('should handle unnamed parameters with multi-match successfully', async () => {
const res = await fetchViaHTTP(
appPort,
'/unnamed-params/nested/first/second/hello/world',
undefined,
{
redirect: 'manual',
}
)
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/another/first/second/hello/world')
})

it('should handle named regex parameters with multi-match successfully', async () => {
const res = await fetchViaHTTP(
appPort,
'/docs/integrations/v2-some/thing',
undefined,
{
redirect: 'manual',
}
)
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/integrations/-some/thing')
})

if (!isDev) {
it('should output routes-manifest successfully', async () => {
const manifest = await fs.readJSON(
Expand Down Expand Up @@ -376,7 +404,7 @@ const runTests = (isDev = false) => {
statusCode: 307,
},
{
destination: '/:1/:2',
destination: '/:0/:1',
regex: normalizeRegEx(
'^\\/unnamed(?:\\/(first|second))(?:\\/(.*))$'
),
Expand All @@ -389,6 +417,22 @@ const runTests = (isDev = false) => {
source: '/named-like-unnamed/:0',
statusCode: 307,
},
{
destination: '/another/:0/:test/:1',
regex: normalizeRegEx(
'^\\/unnamed-params\\/nested(?:\\/(.*?))(?:\\/([^\\/]+?))(?:\\/(.*))$'
),
source: '/unnamed-params/nested/(.*?)/:test/(.*)',
statusCode: 307,
},
{
destination: '/:first/:second',
regex: normalizeRegEx(
'^\\/docs(?:\\/(integrations|now-cli))\\/v2(.*)$'
),
source: '/docs/:first(integrations|now-cli)/v2:second(.*)',
statusCode: 307,
},
],
headers: [
{
Expand Down Expand Up @@ -498,7 +542,7 @@ const runTests = (isDev = false) => {
source: '/api-hello',
},
{
destination: '/api/hello?name=:1',
destination: '/api/hello?name=:0',
regex: normalizeRegEx('^\\/api-hello-regex(?:\\/(.*))$'),
source: '/api-hello-regex/(.*)',
},
Expand Down