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 {
CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME,
DEFAULT_REDIRECT_STATUS,
PAGES_MANIFEST,
PHASE_PRODUCTION_SERVER,
ROUTES_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
DEFAULT_REDIRECT_STATUS,
} from '../lib/constants'
import {
getRouteMatcher,
......@@ -278,9 +278,13 @@ export default class Server {
// Otherwise this will lead to a pretty simple DOS attack.
// See more: https://github.com/zeit/next.js/issues/2617
match: route('/static/:path*'),
name: 'static catchall',
fn: async (req, res, params, parsedUrl) => {
const p = join(this.dir, 'static', ...(params.path || []))
await this.serveStatic(req, res, p, parsedUrl)
return {
finished: true,
}
},
} as Route,
]
......@@ -289,13 +293,20 @@ export default class Server {
const routes: Route[] = [
{
match: route('/_next/static/:path*'),
type: 'route',
name: '_next/static catchall',
fn: async (req, res, params, parsedUrl) => {
// The commons folder holds commonschunk files
// 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.
// 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 (
params.path[0] === CLIENT_STATIC_FILES_RUNTIME ||
......@@ -310,22 +321,33 @@ export default class Server {
...(params.path || [])
)
await this.serveStatic(req, res, p, parsedUrl)
return {
finished: true,
}
},
},
{
match: route('/_next/data/:path*'),
type: 'route',
name: '_next/data catchall',
fn: async (req, res, params, _parsedUrl) => {
// Make sure to 404 for /_next/data/ itself and
// we also want to 404 if the buildId isn't correct
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
params.path.shift()
// show 404 if it doesn't end with .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
......@@ -342,26 +364,21 @@ export default class Server {
{ _nextSprData: '1' },
parsedUrl
)
return {
finished: true,
}
},
},
{
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
fn: async (req, res, _params, parsedUrl) => {
await this.render404(req, res, parsedUrl)
},
},
...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!
)
return {
finished: true,
}
},
},
]
......@@ -384,50 +401,58 @@ export default class Server {
]
routes.push(
...customRoutes.map((r, idx) => {
...customRoutes.map(route => {
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 }
match: route.matcher,
type: route.type,
statusCode: route.statusCode,
name: `${route.type} ${route.source} route`,
fn: async (_req, res, params, _parsedUrl) => {
let destinationCompiler = pathToRegexp.compile(route.destination)
let newUrl = destinationCompiler(params)
if (route.type === 'redirect') {
res.setHeader('Location', newUrl)
res.statusCode = route.statusCode || DEFAULT_REDIRECT_STATUS
res.end()
return {
finished: true,
}
}
if (r.type === 'redirect') {
res.setHeader('Location', newUrl)
res.statusCode = statusCode || DEFAULT_REDIRECT_STATUS
res.end()
return
return {
finished: false,
pathname: newUrl,
}
return this.render(req, res, newUrl, newParams, parsedUrl)
},
} 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) {
this.dynamicRoutes = this.getDynamicRoutes()
......@@ -437,13 +462,25 @@ export default class Server {
// See more: https://github.com/zeit/next.js/issues/2617
routes.push({
match: route('/:path*'),
fn: async (req, res, _params, parsedUrl) => {
type: 'route',
name: 'Catchall render',
fn: async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl
if (!pathname) {
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)
return {
finished: true,
}
},
})
}
......@@ -451,6 +488,15 @@ export default class Server {
return routes
}
protected async _beforeCatchAllRender(
_req: IncomingMessage,
_res: ServerResponse,
_params: Params,
_parsedUrl: UrlWithParsedQuery
) {
return false
}
/**
* Resolves `API` request, in development builds on demand
* @param req http request
......@@ -531,9 +577,14 @@ export default class Server {
if (!pagesManifest[unixPath]) {
routes.push({
match: route(unixPath),
type: 'route',
name: 'public catchall',
fn: async (req, res, _params, parsedUrl) => {
const p = join(this.publicDir, unixPath)
await this.serveStatic(req, res, p, parsedUrl)
return {
finished: true,
}
},
})
}
......@@ -565,9 +616,8 @@ export default class Server {
this.handleCompression(req, res)
try {
const fn = this.router.match(req, res, parsedUrl)
if (fn) {
await fn()
const matched = await this.router.execute(req, res, parsedUrl)
if (matched) {
return
}
} catch (err) {
......
......@@ -8,14 +8,22 @@ export type Params = { [param: string]: any }
export type RouteMatch = (pathname: string | undefined) => false | Params
type RouteResult = {
finished: boolean
pathname?: string
}
export type Route = {
match: RouteMatch
type: string
statusCode?: number
name: string
fn: (
req: IncomingMessage,
res: ServerResponse,
params: Params,
parsedUrl: UrlWithParsedQuery
) => void
) => Promise<RouteResult> | RouteResult
}
export default class Router {
......@@ -28,17 +36,35 @@ export default class Router {
this.routes.unshift(route)
}
match(
async execute(
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
) {
const { pathname } = parsedUrl
): Promise<boolean> {
let parsedUrlUpdated = parsedUrl
for (const route of this.routes) {
const params = route.match(pathname)
if (params) {
return () => route.fn(req, res, params, parsedUrl)
const newParams = route.match(parsedUrlUpdated.pathname)
// 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 {
} from '../next-server/lib/router/utils'
import Server, { ServerConstructor } from '../next-server/server/next-server'
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 { Telemetry } from '../telemetry/storage'
import ErrorDebug from './error-debug'
......@@ -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
this.router.add({
match: route(path),
type: 'route',
name: `${path} exportpathmap route`,
fn: async (req, res, params, parsedUrl) => {
const { query: urlQuery } = parsedUrl
......@@ -117,6 +119,9 @@ export default class DevServer extends Server {
const mergedQuery = { ...urlQuery, ...query }
await this.render(req, res, page, mergedQuery, parsedUrl)
return {
finished: true,
}
},
})
}
......@@ -236,21 +241,14 @@ export default class DevServer extends Server {
}
}
async run(
protected async _beforeCatchAllRender(
req: IncomingMessage,
res: ServerResponse,
_params: Params,
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) {}
}
// check for a public file, throwing error if there's a
// conflicting page
if (await this.hasPublicFile(pathname!)) {
......@@ -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`
)
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)) || {
......@@ -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
routes.unshift({
match: route('/_next/development/:path*'),
type: 'route',
name: '_next/development catchall',
fn: async (req, res, params) => {
const p = join(this.distDir, ...(params.path || []))
await this.serveStatic(req, res, p)
return {
finished: true,
}
},
})
......
......@@ -3,6 +3,10 @@ module.exports = {
experimental: {
async rewrites() {
return [
{
source: '/hello-world',
destination: '/static/hello.txt',
},
{
source: '/',
destination: '/another',
......@@ -35,6 +39,21 @@ module.exports = {
},
async redirects() {
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',
destination: '/blog/:id',
......
hello world!
\ No newline at end of file
......@@ -36,12 +36,26 @@ const runTests = (isDev = false) => {
})
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',
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(303)
expect(pathname).toBe('/')
const res1location = url.parse(res1.headers.get('location')).pathname
expect(res1.status).toBe(301)
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 () => {
......@@ -62,6 +76,21 @@ const runTests = (isDev = false) => {
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 () => {
const res = await fetchViaHTTP(appPort, '/redirect2', undefined, {
redirect: 'manual',
......@@ -71,11 +100,21 @@ const runTests = (isDev = false) => {
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 () => {
const html = await renderViaHTTP(appPort, '/test/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 () => {
const html = await renderViaHTTP(appPort, '/test-overwrite/first/second')
expect(html).toMatch(/this-should-be-the-value/)
......@@ -118,6 +157,27 @@ const runTests = (isDev = false) => {
expect(manifest).toEqual({
version: 1,
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',
destination: '/blog/:id',
......@@ -176,6 +236,12 @@ const runTests = (isDev = false) => {
},
],
rewrites: [
{
source: '/hello-world',
destination: '/static/hello.txt',
regex: '^\\/hello-world$',
regexKeys: [],
},
{
source: '/',
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