Unverified Commit ba5e6943 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

[Experimental] Catch-all Route Support (#9416)

* Catch-all Route Support

* Add SSR tests

* Add additional test

* Add unit tests

* Put the feature behind a flag

* Apply suggestions from code review
Co-Authored-By: 's avatarJJ Kasper <jj@jjsweb.site>

* Fix test
parent 34a38d2e
import chalk from 'chalk' import chalk from 'chalk'
import ciEnvironment from 'ci-info' import ciEnvironment from 'ci-info'
import findUp from 'find-up'
import fs from 'fs' import fs from 'fs'
import Worker from 'jest-worker' import Worker from 'jest-worker'
import mkdirpOrig from 'mkdirp' import mkdirpOrig from 'mkdirp'
import nanoid from 'next/dist/compiled/nanoid/index.js' import nanoid from 'next/dist/compiled/nanoid/index.js'
import path from 'path' import path from 'path'
import pathToRegexp from 'path-to-regexp'
import { promisify } from 'util' import { promisify } from 'util'
import findUp from 'find-up'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages' import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants' import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir' import { findPagesDir } from '../lib/find-pages-dir'
...@@ -15,18 +17,18 @@ import { recursiveReadDir } from '../lib/recursive-readdir' ...@@ -15,18 +17,18 @@ import { recursiveReadDir } from '../lib/recursive-readdir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup' import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { import {
BUILD_MANIFEST, BUILD_MANIFEST,
DEFAULT_REDIRECT_STATUS,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
ROUTES_MANIFEST,
SERVER_DIRECTORY, SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
ROUTES_MANIFEST,
DEFAULT_REDIRECT_STATUS,
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import { import {
getRouteRegex, getRouteRegex,
isDynamicRoute,
getSortedRoutes, getSortedRoutes,
isDynamicRoute,
} from '../next-server/lib/router/utils' } from '../next-server/lib/router/utils'
import loadConfig, { import loadConfig, {
isTargetLikeServerless, isTargetLikeServerless,
...@@ -52,7 +54,6 @@ import { ...@@ -52,7 +54,6 @@ import {
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import pathToRegexp from 'path-to-regexp'
const fsAccess = promisify(fs.access) const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink) const fsUnlink = promisify(fs.unlink)
...@@ -482,6 +483,13 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -482,6 +483,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
await writeBuildId(distDir, buildId) await writeBuildId(distDir, buildId)
const dynamicRoutes = pageKeys.filter(page => isDynamicRoute(page)) const dynamicRoutes = pageKeys.filter(page => isDynamicRoute(page))
if (config.experimental.catchAllRouting !== true) {
if (dynamicRoutes.some(page => /\/\[\.{3}[^/]+?\](?=\/|$)/.test(page))) {
throw new Error(
'Catch-all routing is still experimental. You cannot use it yet.'
)
}
}
const finalPrerenderRoutes: { [route: string]: SprRoute } = {} const finalPrerenderRoutes: { [route: string]: SprRoute } = {}
const tbdPrerenderRoutes: string[] = [] const tbdPrerenderRoutes: string[] = []
......
...@@ -8,12 +8,15 @@ export function getRouteMatcher(routeRegex: ReturnType<typeof getRouteRegex>) { ...@@ -8,12 +8,15 @@ export function getRouteMatcher(routeRegex: ReturnType<typeof getRouteRegex>) {
return false return false
} }
const params: { [paramName: string]: string } = {} const params: { [paramName: string]: string | string[] } = {}
Object.keys(groups).forEach((slugName: string) => { Object.keys(groups).forEach((slugName: string) => {
const m = routeMatch[groups[slugName]] const m = routeMatch[groups[slugName]]
if (m !== undefined) { if (m !== undefined) {
params[slugName] = decodeURIComponent(m) params[slugName] =
m.indexOf('/') !== -1
? m.split('/').map(entry => decodeURIComponent(entry))
: decodeURIComponent(m)
} }
}) })
return params return params
......
...@@ -17,9 +17,10 @@ export function getRouteRegex( ...@@ -17,9 +17,10 @@ export function getRouteRegex(
$1 $1
// Un-escape key // Un-escape key
.replace(/\\([|\\{}()[\]^$+*?.-])/g, '$1') .replace(/\\([|\\{}()[\]^$+*?.-])/g, '$1')
.replace(/^\.{3}/, '')
// eslint-disable-next-line no-sequences // eslint-disable-next-line no-sequences
] = groupIndex++), ] = groupIndex++),
'/([^/]+?)' $1.indexOf('\\.\\.\\.') === 0 ? '/(.+?)' : '/([^/]+?)'
) )
) )
......
...@@ -2,9 +2,10 @@ class UrlNode { ...@@ -2,9 +2,10 @@ class UrlNode {
placeholder: boolean = true placeholder: boolean = true
children: Map<string, UrlNode> = new Map() children: Map<string, UrlNode> = new Map()
slugName: string | null = null slugName: string | null = null
restSlugName: string | null = null
insert(urlPath: string): void { insert(urlPath: string): void {
this._insert(urlPath.split('/').filter(Boolean)) this._insert(urlPath.split('/').filter(Boolean), [], false)
} }
smoosh(): string[] { smoosh(): string[] {
...@@ -16,6 +17,9 @@ class UrlNode { ...@@ -16,6 +17,9 @@ class UrlNode {
if (this.slugName !== null) { if (this.slugName !== null) {
childrenPaths.splice(childrenPaths.indexOf('[]'), 1) childrenPaths.splice(childrenPaths.indexOf('[]'), 1)
} }
if (this.restSlugName !== null) {
childrenPaths.splice(childrenPaths.indexOf('[...]'), 1)
}
const routes = childrenPaths const routes = childrenPaths
.map(c => this.children.get(c)!._smoosh(`${prefix}${c}/`)) .map(c => this.children.get(c)!._smoosh(`${prefix}${c}/`))
...@@ -31,22 +35,48 @@ class UrlNode { ...@@ -31,22 +35,48 @@ class UrlNode {
routes.unshift(prefix === '/' ? '/' : prefix.slice(0, -1)) routes.unshift(prefix === '/' ? '/' : prefix.slice(0, -1))
} }
if (this.restSlugName !== null) {
routes.push(
...this.children
.get('[...]')!
._smoosh(`${prefix}[...${this.restSlugName}]/`)
)
}
return routes return routes
} }
private _insert(urlPaths: string[], slugNames: string[] = []): void { private _insert(
urlPaths: string[],
slugNames: string[],
isCatchAll: boolean
): void {
if (urlPaths.length === 0) { if (urlPaths.length === 0) {
this.placeholder = false this.placeholder = false
return return
} }
if (isCatchAll) {
throw new Error(`Catch-all must be the last part of the URL.`)
}
// The next segment in the urlPaths list // The next segment in the urlPaths list
let nextSegment = urlPaths[0] let nextSegment = urlPaths[0]
// Check if the segment matches `[something]` // Check if the segment matches `[something]`
if (nextSegment.startsWith('[') && nextSegment.endsWith(']')) { if (nextSegment.startsWith('[') && nextSegment.endsWith(']')) {
// Strip `[` and `]`, leaving only `something` // Strip `[` and `]`, leaving only `something`
const segmentName = nextSegment.slice(1, -1) let segmentName = nextSegment.slice(1, -1)
if (segmentName.startsWith('...')) {
segmentName = segmentName.substring(3)
isCatchAll = true
}
if (segmentName.startsWith('.')) {
throw new Error(
`Segment names may not start with erroneous periods ('${segmentName}').`
)
}
function handleSlug(previousSlug: string | null, nextSlug: string) { function handleSlug(previousSlug: string | null, nextSlug: string) {
if (previousSlug !== null) { if (previousSlug !== null) {
...@@ -72,11 +102,19 @@ class UrlNode { ...@@ -72,11 +102,19 @@ class UrlNode {
slugNames.push(nextSlug) slugNames.push(nextSlug)
} }
handleSlug(this.slugName, segmentName) if (isCatchAll) {
// slugName is kept as it can only be one particular slugName handleSlug(this.restSlugName, segmentName)
this.slugName = segmentName // slugName is kept as it can only be one particular slugName
// nextSegment is overwritten to [] so that it can later be sorted specifically this.restSlugName = segmentName
nextSegment = '[]' // nextSegment is overwritten to [] so that it can later be sorted specifically
nextSegment = '[...]'
} else {
handleSlug(this.slugName, segmentName)
// slugName is kept as it can only be one particular slugName
this.slugName = segmentName
// nextSegment is overwritten to [] so that it can later be sorted specifically
nextSegment = '[]'
}
} }
// If this UrlNode doesn't have the nextSegment yet we create a new child UrlNode // If this UrlNode doesn't have the nextSegment yet we create a new child UrlNode
...@@ -84,7 +122,9 @@ class UrlNode { ...@@ -84,7 +122,9 @@ class UrlNode {
this.children.set(nextSegment, new UrlNode()) this.children.set(nextSegment, new UrlNode())
} }
this.children.get(nextSegment)!._insert(urlPaths.slice(1), slugNames) this.children
.get(nextSegment)!
._insert(urlPaths.slice(1), slugNames, isCatchAll)
} }
} }
......
...@@ -42,6 +42,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -42,6 +42,7 @@ const defaultConfig: { [key: string]: any } = {
(Number(process.env.CIRCLE_NODE_TOTAL) || (Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1 (os.cpus() || { length: 1 }).length) - 1
), ),
catchAllRouting: false,
css: false, css: false,
documentMiddleware: false, documentMiddleware: false,
granularChunks: false, granularChunks: false,
......
module.exports = { module.exports = {
experimental: { experimental: {
modern: true, modern: true,
publicDirectory: true, catchAllRouting: true,
}, },
} }
...@@ -26,6 +26,18 @@ const Page = () => ( ...@@ -26,6 +26,18 @@ const Page = () => (
<Link href="/on-mount/[post]" as="/on-mount/test-w-hash#item-400"> <Link href="/on-mount/[post]" as="/on-mount/test-w-hash#item-400">
<a id="view-dynamic-with-hash">View test with hash</a> <a id="view-dynamic-with-hash">View test with hash</a>
</Link> </Link>
<Link href="/p1/p2/all-ssr/[...rest]" as="/p1/p2/all-ssr/hello">
<a id="catch-all-single">Catch-all route (single)</a>
</Link>
<Link href="/p1/p2/all-ssr/[...rest]" as="/p1/p2/all-ssr/hello1/hello2">
<a id="catch-all-multi">Catch-all route (multi)</a>
</Link>
<Link
href="/p1/p2/all-ssr/[...rest]"
as="/p1/p2/all-ssr/hello1%2F/he%2Fllo2"
>
<a id="catch-all-enc">Catch-all route (encoded)</a>
</Link>
</div> </div>
) )
......
function All({ query }) {
return <div id="all-ssr-content">{JSON.stringify(query)}</div>
}
All.getInitialProps = ({ query }) => ({ query })
export default All
...@@ -5,6 +5,7 @@ import { join } from 'path' ...@@ -5,6 +5,7 @@ import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import { import {
renderViaHTTP, renderViaHTTP,
fetchViaHTTP,
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
...@@ -12,8 +13,9 @@ import { ...@@ -12,8 +13,9 @@ import {
nextBuild, nextBuild,
nextStart, nextStart,
} from 'next-test-utils' } from 'next-test-utils'
import cheerio from 'cheerio'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let app let app
let appPort let appPort
...@@ -141,6 +143,89 @@ function runTests(dev) { ...@@ -141,6 +143,89 @@ function runTests(dev) {
} }
}) })
it('[catch all] should not match root on SSR', async () => {
const res = await fetchViaHTTP(appPort, '/p1/p2/all-ssr')
expect(res.status).toBe(404)
})
it('[catch all] should pass param in getInitialProps during SSR', async () => {
const html = await renderViaHTTP(appPort, '/p1/p2/all-ssr/test1')
const $ = cheerio.load(html)
expect($('#all-ssr-content').text()).toBe('{"rest":"test1"}')
})
it('[catch all] should pass params in getInitialProps during SSR', async () => {
const html = await renderViaHTTP(appPort, '/p1/p2/all-ssr/test1/test2')
const $ = cheerio.load(html)
expect($('#all-ssr-content').text()).toBe('{"rest":["test1","test2"]}')
})
it('[catch all] should strip trailing slash', async () => {
const html = await renderViaHTTP(appPort, '/p1/p2/all-ssr/test1/test2/')
const $ = cheerio.load(html)
expect($('#all-ssr-content').text()).toBe('{"rest":["test1","test2"]}')
})
it('[catch all] should not decode slashes (start)', async () => {
const html = await renderViaHTTP(appPort, '/p1/p2/all-ssr/test1/%2Ftest2')
const $ = cheerio.load(html)
expect($('#all-ssr-content').text()).toBe('{"rest":["test1","/test2"]}')
})
it('[catch all] should not decode slashes (end)', async () => {
const html = await renderViaHTTP(appPort, '/p1/p2/all-ssr/test1%2F/test2')
const $ = cheerio.load(html)
expect($('#all-ssr-content').text()).toBe('{"rest":["test1/","test2"]}')
})
it('[catch all] should not decode slashes (middle)', async () => {
const html = await renderViaHTTP(appPort, '/p1/p2/all-ssr/test1/te%2Fst2')
const $ = cheerio.load(html)
expect($('#all-ssr-content').text()).toBe('{"rest":["test1","te/st2"]}')
})
it('[catch-all] should pass params in getInitialProps during client navigation (single)', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.elementByCss('#catch-all-single').click()
await browser.waitForElementByCss('#all-ssr-content')
const text = await browser.elementByCss('#all-ssr-content').text()
expect(text).toBe('{"rest":"hello"}')
} finally {
if (browser) await browser.close()
}
})
it('[catch-all] should pass params in getInitialProps during client navigation (multi)', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.elementByCss('#catch-all-multi').click()
await browser.waitForElementByCss('#all-ssr-content')
const text = await browser.elementByCss('#all-ssr-content').text()
expect(text).toBe('{"rest":["hello1","hello2"]}')
} finally {
if (browser) await browser.close()
}
})
it('[catch-all] should pass params in getInitialProps during client navigation (encoded)', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.elementByCss('#catch-all-enc').click()
await browser.waitForElementByCss('#all-ssr-content')
const text = await browser.elementByCss('#all-ssr-content').text()
expect(text).toBe('{"rest":["hello1/","he/llo2"]}')
} finally {
if (browser) await browser.close()
}
})
it('should update dynamic values on mount', async () => { it('should update dynamic values on mount', async () => {
const html = await renderViaHTTP(appPort, '/on-mount/post-1') const html = await renderViaHTTP(appPort, '/on-mount/post-1')
expect(html).toMatch(/onmpost:.*pending/) expect(html).toMatch(/onmpost:.*pending/)
...@@ -244,6 +329,10 @@ function runTests(dev) { ...@@ -244,6 +329,10 @@ function runTests(dev) {
page: '/on-mount/[post]', page: '/on-mount/[post]',
regex: '^\\/on\\-mount\\/([^\\/]+?)(?:\\/)?$', regex: '^\\/on\\-mount\\/([^\\/]+?)(?:\\/)?$',
}, },
{
page: '/p1/p2/all-ssr/[...rest]',
regex: '^\\/p1\\/p2\\/all\\-ssr\\/(.+?)(?:\\/)?$',
},
{ {
page: '/[name]', page: '/[name]',
regex: '^\\/([^\\/]+?)(?:\\/)?$', regex: '^\\/([^\\/]+?)(?:\\/)?$',
...@@ -286,7 +375,7 @@ describe('Dynamic Routing', () => { ...@@ -286,7 +375,7 @@ describe('Dynamic Routing', () => {
module.exports = { module.exports = {
experimental: { experimental: {
modern: true, modern: true,
publicDirectory: true catchAllRouting: true
} }
} }
` `
...@@ -312,7 +401,7 @@ describe('Dynamic Routing', () => { ...@@ -312,7 +401,7 @@ describe('Dynamic Routing', () => {
target: 'serverless', target: 'serverless',
experimental: { experimental: {
modern: true, modern: true,
publicDirectory: true catchAllRouting: true
} }
} }
` `
......
...@@ -10,8 +10,13 @@ Array [ ...@@ -10,8 +10,13 @@ Array [
"/blog/[id]", "/blog/[id]",
"/blog/[id]/comments/[cid]", "/blog/[id]/comments/[cid]",
"/foo/[d]/bar/baz/[f]", "/foo/[d]/bar/baz/[f]",
"/p/[...rest]",
"/p2/[id]",
"/p2/[id]/abc",
"/p2/[...rest]",
"/posts", "/posts",
"/posts/[id]", "/posts/[id]",
"/[root-slug]", "/[root-slug]",
"/[...rest]",
] ]
`; `;
...@@ -25,8 +25,13 @@ describe('getSortedRoutes', () => { ...@@ -25,8 +25,13 @@ describe('getSortedRoutes', () => {
'/posts/[id]', '/posts/[id]',
'/blog/[id]/comments/[cid]', '/blog/[id]/comments/[cid]',
'/blog/abc/[id]', '/blog/abc/[id]',
'/[...rest]',
'/blog/abc/post', '/blog/abc/post',
'/blog/abc', '/blog/abc',
'/p/[...rest]',
'/p2/[...rest]',
'/p2/[id]',
'/p2/[id]/abc',
'/blog/[id]', '/blog/[id]',
'/foo/[d]/bar/baz/[f]', '/foo/[d]/bar/baz/[f]',
'/apples/[ab]/[cd]/ef', '/apples/[ab]/[cd]/ef',
...@@ -51,4 +56,34 @@ describe('getSortedRoutes', () => { ...@@ -51,4 +56,34 @@ describe('getSortedRoutes', () => {
getSortedRoutes(['/', '/blog', '/blog/[id]/comments/[id]', '/blog/[id]']) getSortedRoutes(['/', '/blog', '/blog/[id]/comments/[id]', '/blog/[id]'])
).toThrowError(/the same slug name/) ).toThrowError(/the same slug name/)
}) })
it('catches reused param names', () => {
expect(() =>
getSortedRoutes(['/blog/[id]', '/blog/[id]/[...id]'])
).toThrowError(/the same slug name/)
})
it('catches middle catch-all', () => {
expect(() => getSortedRoutes(['/blog/[...id]/[...id2]'])).toThrowError(
/must be the last part/
)
})
it('catches middle catch-all', () => {
expect(() => getSortedRoutes(['/blog/[...id]/abc'])).toThrowError(
/must be the last part/
)
})
it('catches extra dots in catch-all', () => {
expect(() => getSortedRoutes(['/blog/[....id]/abc'])).toThrowError(
/erroneous period/
)
})
it('catches missing dots in catch-all', () => {
expect(() => getSortedRoutes(['/blog/[..id]/abc'])).toThrowError(
/erroneous period/
)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment