Unverified Commit 6a2c16a9 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Update router to have only one route traverse pass (#9435)

* Fix next-news inconsistent return type

* Add failing test for static file rewrite

* Revert "Fix next-news inconsistent return type"

This reverts commit b66e76fb86061e45741c3c4bb9296a0874900f76.

* Add failing test for double redirect

* Fix bug in matcher when having multiple redirects

* Remove custom traversal in favor of router handling it

* Update next-server.ts

* Update router.ts

* Temporarily disable test

* Don't deeply resolve redirects

* Support combined parameters + query passing

* Make sure params are correctly passed in

* Add test for hash in route
parent c7d2da4a
...@@ -12,12 +12,12 @@ import { ...@@ -12,12 +12,12 @@ import {
CLIENT_PUBLIC_FILES_PATH, CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME, CLIENT_STATIC_FILES_RUNTIME,
DEFAULT_REDIRECT_STATUS,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_PRODUCTION_SERVER, PHASE_PRODUCTION_SERVER,
ROUTES_MANIFEST, ROUTES_MANIFEST,
SERVER_DIRECTORY, SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
DEFAULT_REDIRECT_STATUS,
} from '../lib/constants' } from '../lib/constants'
import { import {
getRouteMatcher, getRouteMatcher,
...@@ -278,9 +278,13 @@ export default class Server { ...@@ -278,9 +278,13 @@ export default class Server {
// Otherwise this will lead to a pretty simple DOS attack. // Otherwise this will lead to a pretty simple DOS attack.
// See more: https://github.com/zeit/next.js/issues/2617 // See more: https://github.com/zeit/next.js/issues/2617
match: route('/static/:path*'), match: route('/static/:path*'),
name: 'static catchall',
fn: async (req, res, params, parsedUrl) => { fn: async (req, res, params, parsedUrl) => {
const p = join(this.dir, 'static', ...(params.path || [])) const p = join(this.dir, 'static', ...(params.path || []))
await this.serveStatic(req, res, p, parsedUrl) await this.serveStatic(req, res, p, parsedUrl)
return {
finished: true,
}
}, },
} as Route, } as Route,
] ]
...@@ -289,13 +293,20 @@ export default class Server { ...@@ -289,13 +293,20 @@ export default class Server {
const routes: Route[] = [ const routes: Route[] = [
{ {
match: route('/_next/static/:path*'), match: route('/_next/static/:path*'),
type: 'route',
name: '_next/static catchall',
fn: async (req, res, params, parsedUrl) => { fn: async (req, res, params, parsedUrl) => {
// The commons folder holds commonschunk files // The commons folder holds commonschunk files
// The chunks folder holds dynamic entries // The chunks folder holds dynamic entries
// The buildId folder holds pages and potentially other assets. As buildId changes per build it can be long-term cached. // The buildId folder holds pages and potentially other assets. As buildId changes per build it can be long-term cached.
// make sure to 404 for /_next/static itself // make sure to 404 for /_next/static itself
if (!params.path) return this.render404(req, res, parsedUrl) if (!params.path) {
await this.render404(req, res, parsedUrl)
return {
finished: true,
}
}
if ( if (
params.path[0] === CLIENT_STATIC_FILES_RUNTIME || params.path[0] === CLIENT_STATIC_FILES_RUNTIME ||
...@@ -310,22 +321,33 @@ export default class Server { ...@@ -310,22 +321,33 @@ export default class Server {
...(params.path || []) ...(params.path || [])
) )
await this.serveStatic(req, res, p, parsedUrl) await this.serveStatic(req, res, p, parsedUrl)
return {
finished: true,
}
}, },
}, },
{ {
match: route('/_next/data/:path*'), match: route('/_next/data/:path*'),
type: 'route',
name: '_next/data catchall',
fn: async (req, res, params, _parsedUrl) => { fn: async (req, res, params, _parsedUrl) => {
// Make sure to 404 for /_next/data/ itself and // Make sure to 404 for /_next/data/ itself and
// we also want to 404 if the buildId isn't correct // we also want to 404 if the buildId isn't correct
if (!params.path || params.path[0] !== this.buildId) { if (!params.path || params.path[0] !== this.buildId) {
return this.render404(req, res, _parsedUrl) await this.render404(req, res, _parsedUrl)
return {
finished: true,
}
} }
// remove buildId from URL // remove buildId from URL
params.path.shift() params.path.shift()
// show 404 if it doesn't end with .json // show 404 if it doesn't end with .json
if (!params.path[params.path.length - 1].endsWith('.json')) { if (!params.path[params.path.length - 1].endsWith('.json')) {
return this.render404(req, res, _parsedUrl) await this.render404(req, res, _parsedUrl)
return {
finished: true,
}
} }
// re-create page's pathname // re-create page's pathname
...@@ -342,26 +364,21 @@ export default class Server { ...@@ -342,26 +364,21 @@ export default class Server {
{ _nextSprData: '1' }, { _nextSprData: '1' },
parsedUrl parsedUrl
) )
return {
finished: true,
}
}, },
}, },
{ {
match: route('/_next/:path*'), match: route('/_next/:path*'),
type: 'route',
name: '_next catchall',
// This path is needed because `render()` does a check for `/_next` and the calls the routing again // This path is needed because `render()` does a check for `/_next` and the calls the routing again
fn: async (req, res, _params, parsedUrl) => { fn: async (req, res, _params, parsedUrl) => {
await this.render404(req, res, parsedUrl) await this.render404(req, res, parsedUrl)
}, return {
}, finished: true,
...publicRoutes, }
...staticFilesRoute,
{
match: route('/api/:path*'),
fn: async (req, res, params, parsedUrl) => {
const { pathname } = parsedUrl
await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
pathname!
)
}, },
}, },
] ]
...@@ -384,50 +401,58 @@ export default class Server { ...@@ -384,50 +401,58 @@ export default class Server {
] ]
routes.push( routes.push(
...customRoutes.map((r, idx) => { ...customRoutes.map(route => {
return { return {
match: r.matcher, match: route.matcher,
fn: async (req, res, params, parsedUrl) => { type: route.type,
let destinationCompiler = pathToRegexp.compile(r.destination) statusCode: route.statusCode,
let newUrl = destinationCompiler(params) // /blog/123 name: `${route.type} ${route.source} route`,
let newParams = params // { id: 123 } fn: async (_req, res, params, _parsedUrl) => {
let statusCode = r.statusCode let destinationCompiler = pathToRegexp.compile(route.destination)
const followingRoutes = customRoutes.slice(idx + 1) let newUrl = destinationCompiler(params)
for (const followingRoute of followingRoutes) { if (route.type === 'redirect') {
if ( res.setHeader('Location', newUrl)
r.type === 'redirect' && res.statusCode = route.statusCode || DEFAULT_REDIRECT_STATUS
followingRoute.type !== 'redirect' res.end()
) { return {
continue finished: true,
}
// 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') { return {
res.setHeader('Location', newUrl) finished: false,
res.statusCode = statusCode || DEFAULT_REDIRECT_STATUS pathname: newUrl,
res.end()
return
} }
return this.render(req, res, newUrl, newParams, parsedUrl)
}, },
} as Route } as Route
}) })
) )
} }
routes.push(
...([
...publicRoutes,
...staticFilesRoute,
{
match: route('/api/:path*'),
type: 'route',
name: 'API Route',
fn: async (req, res, params, parsedUrl) => {
const { pathname } = parsedUrl
await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
pathname!
)
return {
finished: true,
}
},
},
] as Route[])
)
if (this.nextConfig.useFileSystemPublicRoutes) { if (this.nextConfig.useFileSystemPublicRoutes) {
this.dynamicRoutes = this.getDynamicRoutes() this.dynamicRoutes = this.getDynamicRoutes()
...@@ -437,13 +462,25 @@ export default class Server { ...@@ -437,13 +462,25 @@ export default class Server {
// See more: https://github.com/zeit/next.js/issues/2617 // See more: https://github.com/zeit/next.js/issues/2617
routes.push({ routes.push({
match: route('/:path*'), match: route('/:path*'),
fn: async (req, res, _params, parsedUrl) => { type: 'route',
name: 'Catchall render',
fn: async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl const { pathname, query } = parsedUrl
if (!pathname) { if (!pathname) {
throw new Error('pathname is undefined') throw new Error('pathname is undefined')
} }
// Used in development to check public directory paths
if (await this._beforeCatchAllRender(req, res, params, parsedUrl)) {
return {
finished: true,
}
}
await this.render(req, res, pathname, query, parsedUrl) await this.render(req, res, pathname, query, parsedUrl)
return {
finished: true,
}
}, },
}) })
} }
...@@ -451,6 +488,15 @@ export default class Server { ...@@ -451,6 +488,15 @@ export default class Server {
return routes return routes
} }
protected async _beforeCatchAllRender(
_req: IncomingMessage,
_res: ServerResponse,
_params: Params,
_parsedUrl: UrlWithParsedQuery
) {
return false
}
/** /**
* Resolves `API` request, in development builds on demand * Resolves `API` request, in development builds on demand
* @param req http request * @param req http request
...@@ -531,9 +577,14 @@ export default class Server { ...@@ -531,9 +577,14 @@ export default class Server {
if (!pagesManifest[unixPath]) { if (!pagesManifest[unixPath]) {
routes.push({ routes.push({
match: route(unixPath), match: route(unixPath),
type: 'route',
name: 'public catchall',
fn: async (req, res, _params, parsedUrl) => { fn: async (req, res, _params, parsedUrl) => {
const p = join(this.publicDir, unixPath) const p = join(this.publicDir, unixPath)
await this.serveStatic(req, res, p, parsedUrl) await this.serveStatic(req, res, p, parsedUrl)
return {
finished: true,
}
}, },
}) })
} }
...@@ -565,9 +616,8 @@ export default class Server { ...@@ -565,9 +616,8 @@ export default class Server {
this.handleCompression(req, res) this.handleCompression(req, res)
try { try {
const fn = this.router.match(req, res, parsedUrl) const matched = await this.router.execute(req, res, parsedUrl)
if (fn) { if (matched) {
await fn()
return return
} }
} catch (err) { } catch (err) {
......
...@@ -8,14 +8,22 @@ export type Params = { [param: string]: any } ...@@ -8,14 +8,22 @@ export type Params = { [param: string]: any }
export type RouteMatch = (pathname: string | undefined) => false | Params export type RouteMatch = (pathname: string | undefined) => false | Params
type RouteResult = {
finished: boolean
pathname?: string
}
export type Route = { export type Route = {
match: RouteMatch match: RouteMatch
type: string
statusCode?: number
name: string
fn: ( fn: (
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
params: Params, params: Params,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
) => void ) => Promise<RouteResult> | RouteResult
} }
export default class Router { export default class Router {
...@@ -28,17 +36,35 @@ export default class Router { ...@@ -28,17 +36,35 @@ export default class Router {
this.routes.unshift(route) this.routes.unshift(route)
} }
match( async execute(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
) { ): Promise<boolean> {
const { pathname } = parsedUrl let parsedUrlUpdated = parsedUrl
for (const route of this.routes) { for (const route of this.routes) {
const params = route.match(pathname) const newParams = route.match(parsedUrlUpdated.pathname)
if (params) {
return () => route.fn(req, res, params, parsedUrl) // Check if the match function matched
if (newParams) {
// Combine parameters and querystring
if (route.type === 'rewrite' || route.type === 'redirect') {
parsedUrlUpdated.query = { ...parsedUrlUpdated.query, ...newParams }
}
const result = await route.fn(req, res, newParams, parsedUrlUpdated)
// The response was handled
if (result.finished) {
return true
}
if (result.pathname) {
parsedUrlUpdated.pathname = result.pathname
}
} }
} }
return false
} }
} }
...@@ -21,7 +21,7 @@ import { ...@@ -21,7 +21,7 @@ import {
} from '../next-server/lib/router/utils' } from '../next-server/lib/router/utils'
import Server, { ServerConstructor } from '../next-server/server/next-server' import Server, { ServerConstructor } from '../next-server/server/next-server'
import { normalizePagePath } from '../next-server/server/normalize-page-path' import { normalizePagePath } from '../next-server/server/normalize-page-path'
import { route } from '../next-server/server/router' import { route, Params } from '../next-server/server/router'
import { eventVersion } from '../telemetry/events' import { eventVersion } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
import ErrorDebug from './error-debug' import ErrorDebug from './error-debug'
...@@ -103,6 +103,8 @@ export default class DevServer extends Server { ...@@ -103,6 +103,8 @@ export default class DevServer extends Server {
// We use unshift so that we're sure the routes is defined before Next's default routes // We use unshift so that we're sure the routes is defined before Next's default routes
this.router.add({ this.router.add({
match: route(path), match: route(path),
type: 'route',
name: `${path} exportpathmap route`,
fn: async (req, res, params, parsedUrl) => { fn: async (req, res, params, parsedUrl) => {
const { query: urlQuery } = parsedUrl const { query: urlQuery } = parsedUrl
...@@ -117,6 +119,9 @@ export default class DevServer extends Server { ...@@ -117,6 +119,9 @@ export default class DevServer extends Server {
const mergedQuery = { ...urlQuery, ...query } const mergedQuery = { ...urlQuery, ...query }
await this.render(req, res, page, mergedQuery, parsedUrl) await this.render(req, res, page, mergedQuery, parsedUrl)
return {
finished: true,
}
}, },
}) })
} }
...@@ -236,21 +241,14 @@ export default class DevServer extends Server { ...@@ -236,21 +241,14 @@ export default class DevServer extends Server {
} }
} }
async run( protected async _beforeCatchAllRender(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
_params: Params,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
) { ) {
await this.devReady
const { pathname } = parsedUrl const { pathname } = parsedUrl
if (pathname!.startsWith('/_next')) {
try {
await fsStat(join(this.publicDir, '_next'))
throw new Error(PUBLIC_DIR_MIDDLEWARE_CONFLICT)
} catch (err) {}
}
// check for a public file, throwing error if there's a // check for a public file, throwing error if there's a
// conflicting page // conflicting page
if (await this.hasPublicFile(pathname!)) { if (await this.hasPublicFile(pathname!)) {
...@@ -265,9 +263,29 @@ export default class DevServer extends Server { ...@@ -265,9 +263,29 @@ export default class DevServer extends Server {
`A conflicting public file and page file was found for path ${pathname} https://err.sh/zeit/next.js/conflicting-public-file-page` `A conflicting public file and page file was found for path ${pathname} https://err.sh/zeit/next.js/conflicting-public-file-page`
) )
res.statusCode = 500 res.statusCode = 500
return this.renderError(err, req, res, pathname!, {}) await this.renderError(err, req, res, pathname!, {})
return true
} }
return this.servePublic(req, res, pathname!) await this.servePublic(req, res, pathname!)
return true
}
return false
}
async run(
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
) {
await this.devReady
const { pathname } = parsedUrl
if (pathname!.startsWith('/_next')) {
try {
await fsStat(join(this.publicDir, '_next'))
throw new Error(PUBLIC_DIR_MIDDLEWARE_CONFLICT)
} catch (err) {}
} }
const { finished } = (await this.hotReloader!.run(req, res, parsedUrl)) || { const { finished } = (await this.hotReloader!.run(req, res, parsedUrl)) || {
...@@ -308,9 +326,14 @@ export default class DevServer extends Server { ...@@ -308,9 +326,14 @@ export default class DevServer extends Server {
// We use unshift so that we're sure the routes is defined before Next's default routes // We use unshift so that we're sure the routes is defined before Next's default routes
routes.unshift({ routes.unshift({
match: route('/_next/development/:path*'), match: route('/_next/development/:path*'),
type: 'route',
name: '_next/development catchall',
fn: async (req, res, params) => { fn: async (req, res, params) => {
const p = join(this.distDir, ...(params.path || [])) const p = join(this.distDir, ...(params.path || []))
await this.serveStatic(req, res, p) await this.serveStatic(req, res, p)
return {
finished: true,
}
}, },
}) })
......
...@@ -3,6 +3,10 @@ module.exports = { ...@@ -3,6 +3,10 @@ module.exports = {
experimental: { experimental: {
async rewrites() { async rewrites() {
return [ return [
{
source: '/hello-world',
destination: '/static/hello.txt',
},
{ {
source: '/', source: '/',
destination: '/another', destination: '/another',
...@@ -35,6 +39,21 @@ module.exports = { ...@@ -35,6 +39,21 @@ module.exports = {
}, },
async redirects() { async redirects() {
return [ return [
{
source: '/docs/router-status/:code',
destination: '/docs/v2/network/status-codes#:code',
statusCode: 301,
},
{
source: '/docs/github',
destination: '/docs/v2/advanced/now-for-github',
statusCode: 301,
},
{
source: '/docs/v2/advanced/:all(.*)',
destination: '/docs/v2/more/:all',
statusCode: 301,
},
{ {
source: '/hello/:id/another', source: '/hello/:id/another',
destination: '/blog/:id', destination: '/blog/:id',
......
hello world!
\ No newline at end of file
...@@ -36,12 +36,26 @@ const runTests = (isDev = false) => { ...@@ -36,12 +36,26 @@ const runTests = (isDev = false) => {
}) })
it('should handle chained redirects successfully', async () => { it('should handle chained redirects successfully', async () => {
const res = await fetchViaHTTP(appPort, '/redir-chain1', undefined, { const res1 = await fetchViaHTTP(appPort, '/redir-chain1', undefined, {
redirect: 'manual', redirect: 'manual',
}) })
const { pathname } = url.parse(res.headers.get('location')) const res1location = url.parse(res1.headers.get('location')).pathname
expect(res.status).toBe(303) expect(res1.status).toBe(301)
expect(pathname).toBe('/') expect(res1location).toBe('/redir-chain2')
const res2 = await fetchViaHTTP(appPort, res1location, undefined, {
redirect: 'manual',
})
const res2location = url.parse(res2.headers.get('location')).pathname
expect(res2.status).toBe(302)
expect(res2location).toBe('/redir-chain3')
const res3 = await fetchViaHTTP(appPort, res2location, undefined, {
redirect: 'manual',
})
const res3location = url.parse(res3.headers.get('location')).pathname
expect(res3.status).toBe(303)
expect(res3location).toBe('/')
}) })
it('should redirect successfully with default statusCode', async () => { it('should redirect successfully with default statusCode', async () => {
...@@ -62,6 +76,21 @@ const runTests = (isDev = false) => { ...@@ -62,6 +76,21 @@ const runTests = (isDev = false) => {
expect(pathname).toBe('/blog/123') expect(pathname).toBe('/blog/123')
}) })
it('should redirect with hash successfully', async () => {
const res = await fetchViaHTTP(
appPort,
'/docs/router-status/500',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hash } = url.parse(res.headers.get('location'))
expect(res.status).toBe(301)
expect(pathname).toBe('/docs/v2/network/status-codes')
expect(hash).toBe('#500')
})
it('should redirect successfully with provided statusCode', async () => { it('should redirect successfully with provided statusCode', async () => {
const res = await fetchViaHTTP(appPort, '/redirect2', undefined, { const res = await fetchViaHTTP(appPort, '/redirect2', undefined, {
redirect: 'manual', redirect: 'manual',
...@@ -71,11 +100,21 @@ const runTests = (isDev = false) => { ...@@ -71,11 +100,21 @@ const runTests = (isDev = false) => {
expect(pathname).toBe('/') expect(pathname).toBe('/')
}) })
it('should server static files through a rewrite', async () => {
const text = await renderViaHTTP(appPort, '/hello-world')
expect(text).toBe('hello world!')
})
it('should rewrite with params successfully', async () => { it('should rewrite with params successfully', async () => {
const html = await renderViaHTTP(appPort, '/test/hello') const html = await renderViaHTTP(appPort, '/test/hello')
expect(html).toMatch(/Hello/) expect(html).toMatch(/Hello/)
}) })
it('should double redirect successfully', async () => {
const html = await renderViaHTTP(appPort, '/docs/github')
expect(html).toMatch(/hi there/)
})
it('should overwrite param values correctly', async () => { it('should overwrite param values correctly', async () => {
const html = await renderViaHTTP(appPort, '/test-overwrite/first/second') const html = await renderViaHTTP(appPort, '/test-overwrite/first/second')
expect(html).toMatch(/this-should-be-the-value/) expect(html).toMatch(/this-should-be-the-value/)
...@@ -118,6 +157,27 @@ const runTests = (isDev = false) => { ...@@ -118,6 +157,27 @@ const runTests = (isDev = false) => {
expect(manifest).toEqual({ expect(manifest).toEqual({
version: 1, version: 1,
redirects: [ redirects: [
{
source: '/docs/router-status/:code',
destination: '/docs/v2/network/status-codes#:code',
statusCode: 301,
regex: '^\\/docs\\/router-status\\/([^\\/]+?)$',
regexKeys: ['code'],
},
{
source: '/docs/github',
destination: '/docs/v2/advanced/now-for-github',
statusCode: 301,
regex: '^\\/docs\\/github$',
regexKeys: [],
},
{
source: '/docs/v2/advanced/:all(.*)',
destination: '/docs/v2/more/:all',
statusCode: 301,
regex: '^\\/docs\\/v2\\/advanced\\/(.*)$',
regexKeys: ['all'],
},
{ {
source: '/hello/:id/another', source: '/hello/:id/another',
destination: '/blog/:id', destination: '/blog/:id',
...@@ -176,6 +236,12 @@ const runTests = (isDev = false) => { ...@@ -176,6 +236,12 @@ const runTests = (isDev = false) => {
}, },
], ],
rewrites: [ rewrites: [
{
source: '/hello-world',
destination: '/static/hello.txt',
regex: '^\\/hello-world$',
regexKeys: [],
},
{ {
source: '/', source: '/',
destination: '/another', destination: '/another',
......
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