Commit d19e825d authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Implement custom routes RFC (#9157)

* add initial custom-routes handling

* Add tests for custom-routes

* Handle chained redirects, separate dev custom
routes reading, and add version to routes manifest

* Handle no routes manifest

* Swap build custom routes calling

* Add flatten-routes

* Add flattening of custom routes

* Re-work implementation to follow routes top-down

* Add regex field to routes-manifest

* Fix path-to-regexp match breaking after upgrade

* Fix duplicate const from merge

* Add some changes from review

* Don't make path-match strict

* add default custom route values

* Update routes-manifest

* Update options for path-match

* Remove todo

* Add test for rewrite with params

* Only use strict mode for custom routes

* Update dynamic-routing test

* Move getCustomRoutes to prepare

* Remove extra change

* Update handling for error-in-error test

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Apply suggestions from review

* Update slice change

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Apply suggestions from review

* Fix TypeScript error

* Fix getCustomRoutes in dev mode

* Apply suggestions from code review

* Update slice

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Delete un-used test page

* Add test for param overwriting

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Add extra check to param test
parent d5486e5b
...@@ -51,6 +51,7 @@ import { ...@@ -51,6 +51,7 @@ 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)
...@@ -93,6 +94,15 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -93,6 +94,15 @@ export default async function build(dir: string, conf = null): Promise<void> {
const { target } = config const { target } = config
const buildId = await generateBuildId(config.generateBuildId, nanoid) const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = path.join(dir, config.distDir) const distDir = path.join(dir, config.distDir)
const rewrites = []
const redirects = []
if (typeof config.experimental.redirects === 'function') {
redirects.push(...(await config.experimental.redirects()))
}
if (typeof config.experimental.rewrites === 'function') {
rewrites.push(...(await config.experimental.rewrites()))
}
if (ciEnvironment.isCI) { if (ciEnvironment.isCI) {
const cacheDir = path.join(distDir, 'cache') const cacheDir = path.join(distDir, 'cache')
...@@ -471,21 +481,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -471,21 +481,6 @@ 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))
await fsWriteFile(
path.join(distDir, ROUTES_MANIFEST),
JSON.stringify(
{
version: 0,
dynamicRoutes: getSortedRoutes(dynamicRoutes).map(page => ({
page,
regex: getRouteRegex(page).re.source,
})),
},
null,
2
)
)
const finalPrerenderRoutes: { [route: string]: SprRoute } = {} const finalPrerenderRoutes: { [route: string]: SprRoute } = {}
const tbdPrerenderRoutes: string[] = [] const tbdPrerenderRoutes: string[] = []
...@@ -623,6 +618,31 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -623,6 +618,31 @@ export default async function build(dir: string, conf = null): Promise<void> {
await fsRmdir(exportOptions.outdir) await fsRmdir(exportOptions.outdir)
await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8') await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8')
} }
const buildCustomRoute = (r: { source: string }) => {
return {
...r,
regex: pathToRegexp(r.source, [], {
strict: true,
sensitive: false,
}).source,
}
}
await fsWriteFile(
path.join(distDir, ROUTES_MANIFEST),
JSON.stringify({
version: 1,
redirects: redirects.map(r => buildCustomRoute(r)),
rewrites: rewrites.map(r => buildCustomRoute(r)),
dynamicRoutes: getSortedRoutes(dynamicRoutes).map(page => ({
page,
regex: getRouteRegex(page).re.source,
})),
}),
'utf8'
)
if (postBuildSpinner) postBuildSpinner.stopAndPersist() if (postBuildSpinner) postBuildSpinner.stopAndPersist()
console.log() console.log()
......
...@@ -4,10 +4,19 @@ ...@@ -4,10 +4,19 @@
// (When webpack resolving packages) // (When webpack resolving packages)
const pathToRegexp = require('path-to-regexp') const pathToRegexp = require('path-to-regexp')
export default () => { export default (customRoute = false) => {
return (path: string) => { return (path: string) => {
const keys: any[] = [] const keys: any[] = []
const re = pathToRegexp(path, keys, {}) const re = pathToRegexp(
path,
keys,
customRoute
? {
strict: true,
sensitive: false,
}
: {}
)
return (pathname: string | undefined, params?: any) => { return (pathname: string | undefined, params?: any) => {
const m = re.exec(pathname) const m = re.exec(pathname)
......
...@@ -15,6 +15,7 @@ import { ...@@ -15,6 +15,7 @@ import {
PHASE_PRODUCTION_SERVER, PHASE_PRODUCTION_SERVER,
SERVER_DIRECTORY, SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
ROUTES_MANIFEST,
} from '../lib/constants' } from '../lib/constants'
import { import {
getRouteMatcher, getRouteMatcher,
...@@ -35,9 +36,23 @@ import { sendHTML } from './send-html' ...@@ -35,9 +36,23 @@ import { sendHTML } from './send-html'
import { serveStatic } from './serve-static' import { serveStatic } from './serve-static'
import { getSprCache, initializeSprCache, setSprCache } from './spr-cache' import { getSprCache, initializeSprCache, setSprCache } from './spr-cache'
import { isBlockedPage, isInternalUrl } from './utils' import { isBlockedPage, isInternalUrl } from './utils'
import { fileExists } from '../../lib/file-exists'
import pathToRegexp from 'path-to-regexp'
import pathMatch from './lib/path-match'
const getCustomRouteMatcher = pathMatch(true)
type NextConfig = any type NextConfig = any
type Rewrite = {
source: string
destination: string
}
type Redirect = Rewrite & {
statusCode?: number
}
type Middleware = ( type Middleware = (
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
...@@ -87,6 +102,10 @@ export default class Server { ...@@ -87,6 +102,10 @@ export default class Server {
private onErrorMiddleware?: ({ err }: { err: Error }) => void private onErrorMiddleware?: ({ err }: { err: Error }) => void
router: Router router: Router
protected dynamicRoutes?: Array<{ page: string; match: RouteMatch }> protected dynamicRoutes?: Array<{ page: string; match: RouteMatch }>
protected customRoutes?: {
rewrites: Rewrite[]
redirects: Redirect[]
}
public constructor({ public constructor({
dir = '.', dir = '.',
...@@ -151,8 +170,7 @@ export default class Server { ...@@ -151,8 +170,7 @@ export default class Server {
}) })
} }
const routes = this.generateRoutes() this.router = new Router(this.generateRoutes())
this.router = new Router(routes)
this.setAssetPrefix(assetPrefix) this.setAssetPrefix(assetPrefix)
// call init-server middleware, this is also handled // call init-server middleware, this is also handled
...@@ -239,10 +257,17 @@ export default class Server { ...@@ -239,10 +257,17 @@ export default class Server {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
} }
protected getCustomRoutes() {
return require(join(this.distDir, ROUTES_MANIFEST))
}
protected generateRoutes(): Route[] { protected generateRoutes(): Route[] {
this.customRoutes = this.getCustomRoutes()
const publicRoutes = fs.existsSync(this.publicDir) const publicRoutes = fs.existsSync(this.publicDir)
? this.generatePublicRoutes() ? this.generatePublicRoutes()
: [] : []
const staticFilesRoute = fs.existsSync(join(this.dir, 'static')) const staticFilesRoute = fs.existsSync(join(this.dir, 'static'))
? [ ? [
{ {
...@@ -339,6 +364,68 @@ export default class Server { ...@@ -339,6 +364,68 @@ export default class Server {
}, },
] ]
if (this.customRoutes) {
const { redirects, rewrites } = this.customRoutes
const getCustomRoute = (
r: { source: string; destination: string; statusCode?: number },
type: 'redirect' | 'rewrite'
) => ({
...r,
type,
matcher: getCustomRouteMatcher(r.source),
})
const customRoutes = [
...redirects.map(r => getCustomRoute(r, 'redirect')),
...rewrites.map(r => getCustomRoute(r, 'rewrite')),
]
routes.push(
...customRoutes.map((r, idx) => {
return {
match: r.matcher,
fn: async (req, res, params, parsedUrl) => {
let destinationCompiler = pathToRegexp.compile(r.destination)
let newUrl = destinationCompiler(params) // /blog/123
let newParams = params // { id: 123 }
let statusCode = r.statusCode
const followingRoutes = customRoutes.slice(idx + 1)
for (const followingRoute of followingRoutes) {
if (
r.type === 'redirect' &&
followingRoute.type !== 'redirect'
) {
continue
}
// TODO: add an error if they try to rewrite to a dynamic page
const curParams = followingRoute.matcher(newUrl)
if (curParams) {
destinationCompiler = pathToRegexp.compile(
followingRoute.destination
)
newUrl = destinationCompiler(newParams)
statusCode = followingRoute.statusCode
newParams = { ...newParams, ...curParams }
}
}
if (r.type === 'redirect') {
res.setHeader('Location', newUrl)
res.statusCode = statusCode || 307
res.end()
return
}
return this.render(req, res, newUrl, newParams, parsedUrl)
},
} as Route
})
)
}
if (this.nextConfig.useFileSystemPublicRoutes) { if (this.nextConfig.useFileSystemPublicRoutes) {
this.dynamicRoutes = this.getDynamicRoutes() this.dynamicRoutes = this.getDynamicRoutes()
......
...@@ -103,7 +103,7 @@ ...@@ -103,7 +103,7 @@
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"node-fetch": "2.6.0", "node-fetch": "2.6.0",
"ora": "3.4.0", "ora": "3.4.0",
"path-to-regexp": "2.1.0", "path-to-regexp": "3.1.0",
"pnp-webpack-plugin": "1.5.0", "pnp-webpack-plugin": "1.5.0",
"postcss-flexbugs-fixes": "4.1.0", "postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0", "postcss-loader": "3.0.0",
......
...@@ -73,7 +73,7 @@ function addCorsSupport(req: IncomingMessage, res: ServerResponse) { ...@@ -73,7 +73,7 @@ function addCorsSupport(req: IncomingMessage, res: ServerResponse) {
} }
const matchNextPageBundleRequest = route( const matchNextPageBundleRequest = route(
'/_next/static/:buildId/pages/:path*.js(.map)?' '/_next/static/:buildId/pages/:path*.js(\\.map|)'
) )
// Recursively look up the issuer till it ends up at the root // Recursively look up the issuer till it ends up at the root
......
...@@ -196,6 +196,16 @@ export default class DevServer extends Server { ...@@ -196,6 +196,16 @@ export default class DevServer extends Server {
async prepare() { async prepare() {
await verifyTypeScriptSetup(this.dir, this.pagesDir!) await verifyTypeScriptSetup(this.dir, this.pagesDir!)
await this.loadCustomRoutes()
if (this.customRoutes) {
const { redirects, rewrites } = this.customRoutes
if (redirects.length || rewrites.length) {
// TODO: don't reach into router instance
this.router.routes = this.generateRoutes()
}
}
this.hotReloader = new HotReloader(this.dir, { this.hotReloader = new HotReloader(this.dir, {
pagesDir: this.pagesDir!, pagesDir: this.pagesDir!,
...@@ -270,6 +280,27 @@ export default class DevServer extends Server { ...@@ -270,6 +280,27 @@ export default class DevServer extends Server {
return super.run(req, res, parsedUrl) return super.run(req, res, parsedUrl)
} }
// override production loading of routes-manifest
protected getCustomRoutes() {
return this.customRoutes
}
private async loadCustomRoutes() {
const result = {
redirects: [],
rewrites: [],
}
const { redirects, rewrites } = this.nextConfig.experimental
if (typeof redirects === 'function') {
result.redirects = await redirects()
}
if (typeof rewrites === 'function') {
result.rewrites = await rewrites()
}
this.customRoutes = result
}
generateRoutes() { generateRoutes() {
const routes = super.generateRoutes() const routes = super.generateRoutes()
......
module.exports = {
experimental: {
async rewrites () {
return [
{
source: '/',
destination: '/another'
},
{
source: '/another',
destination: '/multi-rewrites'
},
{
source: '/first',
destination: '/hello'
},
{
source: '/second',
destination: '/hello-again'
},
{
source: '/test/:path',
destination: '/:path'
},
{
source: '/test-overwrite/:something/:another',
destination: '/params/this-should-be-the-value'
},
{
source: '/params/:something',
destination: '/with-params'
}
]
},
async redirects () {
return [
{
source: '/hello/:id/another',
destination: '/blog/:id'
},
{
source: '/redirect1',
destination: '/'
},
{
source: '/redirect2',
destination: '/',
statusCode: 301
},
{
source: '/redirect3',
destination: '/another',
statusCode: 302
},
{
source: '/redirect4',
destination: '/',
statusCode: 308
},
{
source: '/redir-chain1',
destination: '/redir-chain2',
statusCode: 301
},
{
source: '/redir-chain2',
destination: '/redir-chain3',
statusCode: 302
},
{
source: '/redir-chain3',
destination: '/',
statusCode: 303
}
]
}
}
}
import Link from 'next/link'
export default () => (
<>
<h3 id='hello-again'>Hello again</h3>
<Link href='/nav'>
<a id='to-nav'>to nav</a>
</Link>
</>
)
import Link from 'next/link'
export default () => (
<>
<h3 id='hello'>Hello</h3>
<Link href='/nav'>
<a id='to-nav'>to nav</a>
</Link>
</>
)
import Link from 'next/link'
export default () => (
<>
<h3 id='nav'>Nav</h3>
<Link href='/hello' as='/first'>
<a id='to-hello'>to hello</a>
</Link>
<Link href='/hello-again' as='/second'>
<a id='to-hello-again'>to hello-again</a>
</Link>
</>
)
import { useRouter } from 'next/router'
const Page = () => {
const { query } = useRouter()
return <p>{JSON.stringify(query)}</p>
}
Page.getInitialProps = () => ({ hello: 'GIPGIP' })
export default Page
/* eslint-env jest */
/* global jasmine */
import url from 'url'
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
launchApp,
killApp,
findPort,
nextBuild,
nextStart,
fetchViaHTTP,
renderViaHTTP,
getBrowserBodyText,
waitFor
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let appDir = join(__dirname, '..')
let appPort
let app
const runTests = () => {
it('should handle one-to-one rewrite successfully', async () => {
const html = await renderViaHTTP(appPort, '/first')
expect(html).toMatch(/hello/)
})
it('should handle chained rewrites successfully', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/multi-rewrites/)
})
it('should handle chained redirects successfully', async () => {
const res = await fetchViaHTTP(appPort, '/redir-chain1', undefined, {
redirect: 'manual'
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(303)
expect(pathname).toBe('/')
})
it('should redirect successfully with default statusCode', async () => {
const res = await fetchViaHTTP(appPort, '/redirect1', undefined, {
redirect: 'manual'
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(307)
expect(pathname).toBe('/')
})
it('should redirect with params successfully', async () => {
const res = await fetchViaHTTP(appPort, '/hello/123/another', undefined, {
redirect: 'manual'
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(307)
expect(pathname).toBe('/blog/123')
})
it('should redirect successfully with provided statusCode', async () => {
const res = await fetchViaHTTP(appPort, '/redirect2', undefined, {
redirect: 'manual'
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(301)
expect(pathname).toBe('/')
})
it('should rewrite with params successfully', async () => {
const html = await renderViaHTTP(appPort, '/test/hello')
expect(html).toMatch(/Hello/)
})
it('should overwrite param values correctly', async () => {
const html = await renderViaHTTP(appPort, '/test-overwrite/first/second')
expect(html).toMatch(/this-should-be-the-value/)
expect(html).not.toMatch(/first/)
expect(html).toMatch(/second/)
})
it('should work successfully on the client', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.elementByCss('#to-hello').click()
await browser.waitForElementByCss('#hello')
expect(await browser.eval('window.location.href')).toMatch(/\/first$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello/)
await browser.eval('window.location.href = window.location.href')
await waitFor(500)
expect(await browser.eval('window.location.href')).toMatch(/\/first$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello/)
await browser.elementByCss('#to-nav').click()
await browser.waitForElementByCss('#to-hello-again')
await browser.elementByCss('#to-hello-again').click()
await browser.waitForElementByCss('#hello-again')
expect(await browser.eval('window.location.href')).toMatch(/\/second$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello again/)
await browser.eval('window.location.href = window.location.href')
await waitFor(500)
expect(await browser.eval('window.location.href')).toMatch(/\/second$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello again/)
})
}
describe('Custom routes', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
...@@ -232,7 +232,9 @@ function runTests (dev) { ...@@ -232,7 +232,9 @@ function runTests (dev) {
) )
expect(manifest).toEqual({ expect(manifest).toEqual({
version: 0, version: 1,
rewrites: [],
redirects: [],
dynamicRoutes: [ dynamicRoutes: [
{ {
page: '/blog/[name]/comment/[id]', page: '/blog/[name]/comment/[id]',
......
import React from 'react' import React from 'react'
class Error extends React.Component { class Error extends React.Component {
static async getInitialProps ({ res, err }) { static async getInitialProps ({ req, res, err }) {
await Promise.reject(new Error('an error in error')) if (req.url !== '/404.html') {
await Promise.reject(new Error('an error in error'))
}
const statusCode = res ? res.statusCode : err ? err.statusCode : null const statusCode = res ? res.statusCode : err ? err.statusCode : null
return { statusCode } return { statusCode }
} }
......
...@@ -4090,9 +4090,9 @@ caniuse-api@^3.0.0: ...@@ -4090,9 +4090,9 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0" lodash.uniq "^4.5.0"
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000980, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001002, caniuse-lite@^1.0.30001004: caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000980, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001002, caniuse-lite@^1.0.30001004:
version "1.0.30001004" version "1.0.30001008"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001004.tgz#d879b73981b255488316da946c39327d8c00a586" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001008.tgz#b8841b1df78a9f5ed9702537ef592f1f8772c0d9"
integrity sha512-3nfOR4O8Wa2RWoYfJkMtwRVOsK96TQ+eq57wd0iKaEWl8dwG4hKZ/g0MVBfCvysFvMLi9fQGR/DvozMdkEPl3g== integrity sha512-b8DJyb+VVXZGRgJUa30cbk8gKHZ3LOZTBLaUEEVr2P4xpmFigOCc62CO4uzquW641Ouq1Rm9N+rWLWdSYDaDIw==
capitalize@1.0.0: capitalize@1.0.0:
version "1.0.0" version "1.0.0"
...@@ -11270,10 +11270,10 @@ path-to-regexp@0.1.7: ...@@ -11270,10 +11270,10 @@ path-to-regexp@0.1.7:
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w= integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
path-to-regexp@2.1.0: path-to-regexp@3.1.0:
version "2.1.0" version "3.1.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-2.1.0.tgz#7e30f9f5b134bd6a28ffc2e3ef1e47075ac5259b" resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-3.1.0.tgz#f45a9cc4dc6331ae8f131e0ce4fde8607f802367"
integrity sha512-dZY7QPCPp5r9cnNuQ955mOv4ZFVDXY/yvqeV7Y1W2PJA3PEFcuow9xKFfJxbBj1pIjOAP+M2B4/7xubmykLrXw== integrity sha512-PtHLisEvUOepjc+sStXxJ/pDV/s5UBTOKWJY2SOz3e6E/iN/jLknY9WL72kTwRrwXDUbZTEAtSnJbz2fF127DA==
path-type@^1.0.0: path-type@^1.0.0:
version "1.1.0" version "1.1.0"
......
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