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 ciEnvironment from 'ci-info'
import findUp from 'find-up'
import fs from 'fs'
import Worker from 'jest-worker'
import mkdirpOrig from 'mkdirp'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import path from 'path'
import pathToRegexp from 'path-to-regexp'
import { promisify } from 'util'
import findUp from 'find-up'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir'
......@@ -15,18 +17,18 @@ import { recursiveReadDir } from '../lib/recursive-readdir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import {
BUILD_MANIFEST,
DEFAULT_REDIRECT_STATUS,
PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST,
ROUTES_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
ROUTES_MANIFEST,
DEFAULT_REDIRECT_STATUS,
} from '../next-server/lib/constants'
import {
getRouteRegex,
isDynamicRoute,
getSortedRoutes,
isDynamicRoute,
} from '../next-server/lib/router/utils'
import loadConfig, {
isTargetLikeServerless,
......@@ -52,7 +54,6 @@ import {
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id'
import pathToRegexp from 'path-to-regexp'
const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink)
......@@ -482,6 +483,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
await writeBuildId(distDir, buildId)
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 tbdPrerenderRoutes: string[] = []
......
......@@ -8,12 +8,15 @@ export function getRouteMatcher(routeRegex: ReturnType<typeof getRouteRegex>) {
return false
}
const params: { [paramName: string]: string } = {}
const params: { [paramName: string]: string | string[] } = {}
Object.keys(groups).forEach((slugName: string) => {
const m = routeMatch[groups[slugName]]
if (m !== undefined) {
params[slugName] = decodeURIComponent(m)
params[slugName] =
m.indexOf('/') !== -1
? m.split('/').map(entry => decodeURIComponent(entry))
: decodeURIComponent(m)
}
})
return params
......
......@@ -17,9 +17,10 @@ export function getRouteRegex(
$1
// Un-escape key
.replace(/\\([|\\{}()[\]^$+*?.-])/g, '$1')
.replace(/^\.{3}/, '')
// eslint-disable-next-line no-sequences
] = groupIndex++),
'/([^/]+?)'
$1.indexOf('\\.\\.\\.') === 0 ? '/(.+?)' : '/([^/]+?)'
)
)
......
......@@ -2,9 +2,10 @@ class UrlNode {
placeholder: boolean = true
children: Map<string, UrlNode> = new Map()
slugName: string | null = null
restSlugName: string | null = null
insert(urlPath: string): void {
this._insert(urlPath.split('/').filter(Boolean))
this._insert(urlPath.split('/').filter(Boolean), [], false)
}
smoosh(): string[] {
......@@ -16,6 +17,9 @@ class UrlNode {
if (this.slugName !== null) {
childrenPaths.splice(childrenPaths.indexOf('[]'), 1)
}
if (this.restSlugName !== null) {
childrenPaths.splice(childrenPaths.indexOf('[...]'), 1)
}
const routes = childrenPaths
.map(c => this.children.get(c)!._smoosh(`${prefix}${c}/`))
......@@ -31,22 +35,48 @@ class UrlNode {
routes.unshift(prefix === '/' ? '/' : prefix.slice(0, -1))
}
if (this.restSlugName !== null) {
routes.push(
...this.children
.get('[...]')!
._smoosh(`${prefix}[...${this.restSlugName}]/`)
)
}
return routes
}
private _insert(urlPaths: string[], slugNames: string[] = []): void {
private _insert(
urlPaths: string[],
slugNames: string[],
isCatchAll: boolean
): void {
if (urlPaths.length === 0) {
this.placeholder = false
return
}
if (isCatchAll) {
throw new Error(`Catch-all must be the last part of the URL.`)
}
// The next segment in the urlPaths list
let nextSegment = urlPaths[0]
// Check if the segment matches `[something]`
if (nextSegment.startsWith('[') && nextSegment.endsWith(']')) {
// 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) {
if (previousSlug !== null) {
......@@ -72,11 +102,19 @@ class UrlNode {
slugNames.push(nextSlug)
}
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 (isCatchAll) {
handleSlug(this.restSlugName, segmentName)
// slugName is kept as it can only be one particular slugName
this.restSlugName = segmentName
// 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
......@@ -84,7 +122,9 @@ class 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 } = {
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1
),
catchAllRouting: false,
css: false,
documentMiddleware: false,
granularChunks: false,
......
module.exports = {
experimental: {
modern: true,
publicDirectory: true,
catchAllRouting: true,
},
}
......@@ -26,6 +26,18 @@ const Page = () => (
<Link href="/on-mount/[post]" as="/on-mount/test-w-hash#item-400">
<a id="view-dynamic-with-hash">View test with hash</a>
</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>
)
......
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'
import fs from 'fs-extra'
import {
renderViaHTTP,
fetchViaHTTP,
findPort,
launchApp,
killApp,
......@@ -12,8 +13,9 @@ import {
nextBuild,
nextStart,
} 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 appPort
......@@ -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 () => {
const html = await renderViaHTTP(appPort, '/on-mount/post-1')
expect(html).toMatch(/onmpost:.*pending/)
......@@ -244,6 +329,10 @@ function runTests(dev) {
page: '/on-mount/[post]',
regex: '^\\/on\\-mount\\/([^\\/]+?)(?:\\/)?$',
},
{
page: '/p1/p2/all-ssr/[...rest]',
regex: '^\\/p1\\/p2\\/all\\-ssr\\/(.+?)(?:\\/)?$',
},
{
page: '/[name]',
regex: '^\\/([^\\/]+?)(?:\\/)?$',
......@@ -286,7 +375,7 @@ describe('Dynamic Routing', () => {
module.exports = {
experimental: {
modern: true,
publicDirectory: true
catchAllRouting: true
}
}
`
......@@ -312,7 +401,7 @@ describe('Dynamic Routing', () => {
target: 'serverless',
experimental: {
modern: true,
publicDirectory: true
catchAllRouting: true
}
}
`
......
......@@ -10,8 +10,13 @@ Array [
"/blog/[id]",
"/blog/[id]/comments/[cid]",
"/foo/[d]/bar/baz/[f]",
"/p/[...rest]",
"/p2/[id]",
"/p2/[id]/abc",
"/p2/[...rest]",
"/posts",
"/posts/[id]",
"/[root-slug]",
"/[...rest]",
]
`;
......@@ -25,8 +25,13 @@ describe('getSortedRoutes', () => {
'/posts/[id]',
'/blog/[id]/comments/[cid]',
'/blog/abc/[id]',
'/[...rest]',
'/blog/abc/post',
'/blog/abc',
'/p/[...rest]',
'/p2/[...rest]',
'/p2/[id]',
'/p2/[id]/abc',
'/blog/[id]',
'/foo/[d]/bar/baz/[f]',
'/apples/[ab]/[cd]/ef',
......@@ -51,4 +56,34 @@ describe('getSortedRoutes', () => {
getSortedRoutes(['/', '/blog', '/blog/[id]/comments/[id]', '/blog/[id]'])
).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