2019-05-13 15:40:24 +02:00
|
|
|
import { IncomingMessage, ServerResponse } from 'http'
|
2020-08-13 14:39:36 +02:00
|
|
|
import { UrlWithParsedQuery } from 'url'
|
|
|
|
|
2021-06-30 13:44:40 +02:00
|
|
|
import pathMatch from '../shared/lib/router/utils/path-match'
|
|
|
|
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
|
|
|
|
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
|
|
|
|
import { RouteHas } from '../lib/load-custom-routes'
|
|
|
|
import { matchHas } from '../shared/lib/router/utils/prepare-destination'
|
2018-12-09 22:46:45 +01:00
|
|
|
|
|
|
|
export const route = pathMatch()
|
|
|
|
|
2019-06-27 18:01:36 +02:00
|
|
|
export type Params = { [param: string]: any }
|
2018-12-09 22:46:45 +01:00
|
|
|
|
2020-01-06 17:43:26 +01:00
|
|
|
export type RouteMatch = (pathname: string | null | undefined) => false | Params
|
2019-05-27 20:20:33 +02:00
|
|
|
|
2019-11-18 01:12:48 +01:00
|
|
|
type RouteResult = {
|
|
|
|
finished: boolean
|
|
|
|
pathname?: string
|
2019-12-31 21:13:55 +01:00
|
|
|
query?: { [k: string]: string }
|
2019-11-18 01:12:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:46:45 +01:00
|
|
|
export type Route = {
|
2019-05-27 20:20:33 +02:00
|
|
|
match: RouteMatch
|
2021-03-24 17:50:16 +01:00
|
|
|
has?: RouteHas[]
|
2019-11-18 01:12:48 +01:00
|
|
|
type: string
|
2019-12-23 22:20:17 +01:00
|
|
|
check?: boolean
|
2019-11-18 01:12:48 +01:00
|
|
|
statusCode?: number
|
|
|
|
name: string
|
2020-07-12 21:03:49 +02:00
|
|
|
requireBasePath?: false
|
2020-12-07 18:36:46 +01:00
|
|
|
internal?: true
|
2019-05-13 15:40:24 +02:00
|
|
|
fn: (
|
|
|
|
req: IncomingMessage,
|
|
|
|
res: ServerResponse,
|
|
|
|
params: Params,
|
2019-05-29 13:57:26 +02:00
|
|
|
parsedUrl: UrlWithParsedQuery
|
2019-11-18 01:12:48 +01:00
|
|
|
) => Promise<RouteResult> | RouteResult
|
2018-12-09 22:46:45 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:20:17 +01:00
|
|
|
export type DynamicRoutes = Array<{ page: string; match: RouteMatch }>
|
|
|
|
|
|
|
|
export type PageChecker = (pathname: string) => Promise<boolean>
|
|
|
|
|
2020-07-12 21:03:49 +02:00
|
|
|
const customRouteTypes = new Set(['rewrite', 'redirect', 'header'])
|
|
|
|
|
|
|
|
function replaceBasePath(basePath: string, pathname: string) {
|
2020-08-19 19:30:33 +02:00
|
|
|
// If replace ends up replacing the full url it'll be `undefined`, meaning we have to default it to `/`
|
2020-07-12 21:03:49 +02:00
|
|
|
return pathname!.replace(basePath, '') || '/'
|
|
|
|
}
|
|
|
|
|
2018-12-09 22:46:45 +01:00
|
|
|
export default class Router {
|
2020-07-12 21:03:49 +02:00
|
|
|
basePath: string
|
2020-02-11 00:06:38 +01:00
|
|
|
headers: Route[]
|
2019-12-23 22:20:17 +01:00
|
|
|
fsRoutes: Route[]
|
2020-02-11 00:06:38 +01:00
|
|
|
redirects: Route[]
|
2021-03-26 16:19:48 +01:00
|
|
|
rewrites: {
|
|
|
|
beforeFiles: Route[]
|
|
|
|
afterFiles: Route[]
|
|
|
|
fallback: Route[]
|
|
|
|
}
|
2019-12-23 22:20:17 +01:00
|
|
|
catchAllRoute: Route
|
|
|
|
pageChecker: PageChecker
|
|
|
|
dynamicRoutes: DynamicRoutes
|
2020-02-11 00:06:38 +01:00
|
|
|
useFileSystemPublicRoutes: boolean
|
2020-11-14 04:35:42 +01:00
|
|
|
locales: string[]
|
2019-12-23 22:20:17 +01:00
|
|
|
|
|
|
|
constructor({
|
2020-07-12 21:03:49 +02:00
|
|
|
basePath = '',
|
2020-02-11 00:06:38 +01:00
|
|
|
headers = [],
|
2019-12-23 22:20:17 +01:00
|
|
|
fsRoutes = [],
|
2021-03-26 16:19:48 +01:00
|
|
|
rewrites = {
|
|
|
|
beforeFiles: [],
|
|
|
|
afterFiles: [],
|
|
|
|
fallback: [],
|
|
|
|
},
|
2020-02-11 00:06:38 +01:00
|
|
|
redirects = [],
|
2019-12-23 22:20:17 +01:00
|
|
|
catchAllRoute,
|
|
|
|
dynamicRoutes = [],
|
|
|
|
pageChecker,
|
2020-02-11 00:06:38 +01:00
|
|
|
useFileSystemPublicRoutes,
|
2020-11-14 04:35:42 +01:00
|
|
|
locales = [],
|
2019-12-23 22:20:17 +01:00
|
|
|
}: {
|
2020-07-12 21:03:49 +02:00
|
|
|
basePath: string
|
2020-02-11 00:06:38 +01:00
|
|
|
headers: Route[]
|
2019-12-23 22:20:17 +01:00
|
|
|
fsRoutes: Route[]
|
2021-03-26 16:19:48 +01:00
|
|
|
rewrites: {
|
|
|
|
beforeFiles: Route[]
|
|
|
|
afterFiles: Route[]
|
|
|
|
fallback: Route[]
|
|
|
|
}
|
2020-02-11 00:06:38 +01:00
|
|
|
redirects: Route[]
|
2019-12-23 22:20:17 +01:00
|
|
|
catchAllRoute: Route
|
|
|
|
dynamicRoutes: DynamicRoutes | undefined
|
|
|
|
pageChecker: PageChecker
|
2020-02-11 00:06:38 +01:00
|
|
|
useFileSystemPublicRoutes: boolean
|
2020-11-14 04:35:42 +01:00
|
|
|
locales: string[]
|
2019-12-23 22:20:17 +01:00
|
|
|
}) {
|
2020-07-12 21:03:49 +02:00
|
|
|
this.basePath = basePath
|
2020-02-11 00:06:38 +01:00
|
|
|
this.headers = headers
|
2019-12-23 22:20:17 +01:00
|
|
|
this.fsRoutes = fsRoutes
|
2020-02-11 00:06:38 +01:00
|
|
|
this.rewrites = rewrites
|
|
|
|
this.redirects = redirects
|
2019-12-23 22:20:17 +01:00
|
|
|
this.pageChecker = pageChecker
|
|
|
|
this.catchAllRoute = catchAllRoute
|
|
|
|
this.dynamicRoutes = dynamicRoutes
|
2020-02-11 00:06:38 +01:00
|
|
|
this.useFileSystemPublicRoutes = useFileSystemPublicRoutes
|
2020-11-14 04:35:42 +01:00
|
|
|
this.locales = locales
|
2019-12-23 22:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setDynamicRoutes(routes: DynamicRoutes = []) {
|
|
|
|
this.dynamicRoutes = routes
|
2018-12-09 22:46:45 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 23:00:22 +02:00
|
|
|
addFsRoute(fsRoute: Route) {
|
|
|
|
this.fsRoutes.unshift(fsRoute)
|
2018-12-09 22:46:45 +01:00
|
|
|
}
|
|
|
|
|
2019-11-18 01:12:48 +01:00
|
|
|
async execute(
|
2019-05-13 15:40:24 +02:00
|
|
|
req: IncomingMessage,
|
|
|
|
res: ServerResponse,
|
2019-05-29 13:57:26 +02:00
|
|
|
parsedUrl: UrlWithParsedQuery
|
2019-11-18 01:12:48 +01:00
|
|
|
): Promise<boolean> {
|
2019-12-23 22:20:17 +01:00
|
|
|
// memoize page check calls so we don't duplicate checks for pages
|
2020-05-22 19:37:54 +02:00
|
|
|
const pageChecks: { [name: string]: Promise<boolean> } = {}
|
2019-12-23 22:20:17 +01:00
|
|
|
const memoizedPageChecker = async (p: string): Promise<boolean> => {
|
2020-11-14 04:35:42 +01:00
|
|
|
p = normalizeLocalePath(p, this.locales).pathname
|
|
|
|
|
2019-12-23 22:20:17 +01:00
|
|
|
if (pageChecks[p]) {
|
|
|
|
return pageChecks[p]
|
|
|
|
}
|
2020-05-22 19:37:54 +02:00
|
|
|
const result = this.pageChecker(p)
|
2019-12-23 22:20:17 +01:00
|
|
|
pageChecks[p] = result
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-18 01:12:48 +01:00
|
|
|
let parsedUrlUpdated = parsedUrl
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2021-03-26 16:19:48 +01:00
|
|
|
const applyCheckTrue = async (checkParsedUrl: UrlWithParsedQuery) => {
|
|
|
|
const originalFsPathname = checkParsedUrl.pathname
|
|
|
|
const fsPathname = replaceBasePath(this.basePath, originalFsPathname!)
|
|
|
|
|
|
|
|
for (const fsRoute of this.fsRoutes) {
|
|
|
|
const fsParams = fsRoute.match(fsPathname)
|
|
|
|
|
|
|
|
if (fsParams) {
|
|
|
|
checkParsedUrl.pathname = fsPathname
|
|
|
|
|
|
|
|
const fsResult = await fsRoute.fn(req, res, fsParams, checkParsedUrl)
|
|
|
|
|
|
|
|
if (fsResult.finished) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
checkParsedUrl.pathname = originalFsPathname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let matchedPage = await memoizedPageChecker(fsPathname)
|
|
|
|
|
|
|
|
// If we didn't match a page check dynamic routes
|
|
|
|
if (!matchedPage) {
|
2021-04-12 11:42:01 +02:00
|
|
|
const normalizedFsPathname = normalizeLocalePath(
|
|
|
|
fsPathname,
|
|
|
|
this.locales
|
|
|
|
).pathname
|
|
|
|
|
2021-03-26 16:19:48 +01:00
|
|
|
for (const dynamicRoute of this.dynamicRoutes) {
|
2021-04-12 11:42:01 +02:00
|
|
|
if (dynamicRoute.match(normalizedFsPathname)) {
|
2021-03-26 16:19:48 +01:00
|
|
|
matchedPage = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matched a page or dynamic route so render it using catchAllRoute
|
|
|
|
if (matchedPage) {
|
|
|
|
const pageParams = this.catchAllRoute.match(checkParsedUrl.pathname)
|
2021-04-16 17:07:24 +02:00
|
|
|
checkParsedUrl.pathname = fsPathname
|
|
|
|
checkParsedUrl.query._nextBubbleNoFallback = '1'
|
2021-03-26 16:19:48 +01:00
|
|
|
|
2021-04-16 17:07:24 +02:00
|
|
|
const result = await this.catchAllRoute.fn(
|
2021-03-26 16:19:48 +01:00
|
|
|
req,
|
|
|
|
res,
|
|
|
|
pageParams as Params,
|
|
|
|
checkParsedUrl
|
|
|
|
)
|
2021-04-16 17:07:24 +02:00
|
|
|
return result.finished
|
2021-03-26 16:19:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 00:06:38 +01:00
|
|
|
/*
|
|
|
|
Desired routes order
|
|
|
|
- headers
|
|
|
|
- redirects
|
|
|
|
- Check filesystem (including pages), if nothing found continue
|
|
|
|
- User rewrites (checking filesystem and pages each match)
|
|
|
|
*/
|
|
|
|
|
2020-06-01 23:00:22 +02:00
|
|
|
const allRoutes = [
|
2020-02-11 00:06:38 +01:00
|
|
|
...this.headers,
|
|
|
|
...this.redirects,
|
2021-03-26 16:19:48 +01:00
|
|
|
...this.rewrites.beforeFiles,
|
2020-02-11 00:06:38 +01:00
|
|
|
...this.fsRoutes,
|
|
|
|
// We only check the catch-all route if public page routes hasn't been
|
|
|
|
// disabled
|
|
|
|
...(this.useFileSystemPublicRoutes
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
type: 'route',
|
2020-07-12 21:03:49 +02:00
|
|
|
name: 'page checker',
|
|
|
|
requireBasePath: false,
|
2020-02-11 00:06:38 +01:00
|
|
|
match: route('/:path*'),
|
2020-06-01 23:00:22 +02:00
|
|
|
fn: async (checkerReq, checkerRes, params, parsedCheckerUrl) => {
|
2020-08-20 06:05:38 +02:00
|
|
|
let { pathname } = parsedCheckerUrl
|
|
|
|
pathname = removePathTrailingSlash(pathname || '/')
|
2020-02-11 00:06:38 +01:00
|
|
|
|
|
|
|
if (!pathname) {
|
|
|
|
return { finished: false }
|
|
|
|
}
|
2020-08-20 06:05:38 +02:00
|
|
|
|
2020-05-22 19:37:54 +02:00
|
|
|
if (await memoizedPageChecker(pathname)) {
|
2020-06-01 23:00:22 +02:00
|
|
|
return this.catchAllRoute.fn(
|
|
|
|
checkerReq,
|
|
|
|
checkerRes,
|
|
|
|
params,
|
|
|
|
parsedCheckerUrl
|
|
|
|
)
|
2020-02-11 00:06:38 +01:00
|
|
|
}
|
|
|
|
return { finished: false }
|
|
|
|
},
|
|
|
|
} as Route,
|
|
|
|
]
|
|
|
|
: []),
|
2021-03-26 16:19:48 +01:00
|
|
|
...this.rewrites.afterFiles,
|
|
|
|
...(this.rewrites.fallback.length
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
type: 'route',
|
|
|
|
name: 'dynamic route/page check',
|
|
|
|
requireBasePath: false,
|
|
|
|
match: route('/:path*'),
|
|
|
|
fn: async (
|
|
|
|
_checkerReq,
|
|
|
|
_checkerRes,
|
|
|
|
_params,
|
|
|
|
parsedCheckerUrl
|
|
|
|
) => {
|
|
|
|
return {
|
|
|
|
finished: await applyCheckTrue(parsedCheckerUrl),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
} as Route,
|
|
|
|
...this.rewrites.fallback,
|
|
|
|
]
|
|
|
|
: []),
|
|
|
|
|
2020-02-11 00:06:38 +01:00
|
|
|
// We only check the catch-all route if public page routes hasn't been
|
|
|
|
// disabled
|
|
|
|
...(this.useFileSystemPublicRoutes ? [this.catchAllRoute] : []),
|
|
|
|
]
|
2020-07-12 21:03:49 +02:00
|
|
|
const originallyHadBasePath =
|
|
|
|
!this.basePath || (req as any)._nextHadBasePath
|
2020-02-11 00:06:38 +01:00
|
|
|
|
2020-06-01 23:00:22 +02:00
|
|
|
for (const testRoute of allRoutes) {
|
2020-07-12 21:03:49 +02:00
|
|
|
// if basePath is being used, the basePath will still be included
|
|
|
|
// in the pathname here to allow custom-routes to require containing
|
|
|
|
// it or not, filesystem routes and pages must always include the basePath
|
|
|
|
// if it is set
|
2020-12-04 11:14:55 +01:00
|
|
|
let currentPathname = parsedUrlUpdated.pathname as string
|
2020-07-12 21:03:49 +02:00
|
|
|
const originalPathname = currentPathname
|
|
|
|
const requireBasePath = testRoute.requireBasePath !== false
|
|
|
|
const isCustomRoute = customRouteTypes.has(testRoute.type)
|
2020-08-19 19:30:33 +02:00
|
|
|
const isPublicFolderCatchall = testRoute.name === 'public folder catchall'
|
|
|
|
const keepBasePath = isCustomRoute || isPublicFolderCatchall
|
2020-12-04 11:14:55 +01:00
|
|
|
const keepLocale = isCustomRoute
|
|
|
|
|
|
|
|
const currentPathnameNoBasePath = replaceBasePath(
|
|
|
|
this.basePath,
|
|
|
|
currentPathname
|
|
|
|
)
|
2020-07-12 21:03:49 +02:00
|
|
|
|
2020-08-19 19:30:33 +02:00
|
|
|
if (!keepBasePath) {
|
2020-12-04 11:14:55 +01:00
|
|
|
currentPathname = currentPathnameNoBasePath
|
2020-07-12 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:14:55 +01:00
|
|
|
const localePathResult = normalizeLocalePath(
|
|
|
|
currentPathnameNoBasePath,
|
|
|
|
this.locales
|
|
|
|
)
|
|
|
|
const activeBasePath = keepBasePath ? this.basePath : ''
|
|
|
|
|
|
|
|
if (keepLocale) {
|
2020-12-07 18:36:46 +01:00
|
|
|
if (
|
|
|
|
!testRoute.internal &&
|
|
|
|
parsedUrl.query.__nextLocale &&
|
|
|
|
!localePathResult.detectedLocale
|
|
|
|
) {
|
2020-12-04 11:14:55 +01:00
|
|
|
currentPathname = `${activeBasePath}/${parsedUrl.query.__nextLocale}${
|
|
|
|
currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath
|
|
|
|
}`
|
2020-11-12 06:26:48 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:46:46 +01:00
|
|
|
if (
|
|
|
|
(req as any).__nextHadTrailingSlash &&
|
|
|
|
!currentPathname.endsWith('/')
|
|
|
|
) {
|
|
|
|
currentPathname += '/'
|
|
|
|
}
|
2020-12-04 11:14:55 +01:00
|
|
|
} else {
|
|
|
|
currentPathname = `${
|
|
|
|
(req as any)._nextHadBasePath ? activeBasePath : ''
|
|
|
|
}${
|
|
|
|
activeBasePath && localePathResult.pathname === '/'
|
|
|
|
? ''
|
|
|
|
: localePathResult.pathname
|
|
|
|
}`
|
2020-10-29 18:48:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:50:16 +01:00
|
|
|
let newParams = testRoute.match(currentPathname)
|
|
|
|
|
|
|
|
if (testRoute.has && newParams) {
|
|
|
|
const hasParams = matchHas(req, testRoute.has, parsedUrlUpdated.query)
|
|
|
|
|
|
|
|
if (hasParams) {
|
|
|
|
Object.assign(newParams, hasParams)
|
|
|
|
} else {
|
|
|
|
newParams = false
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 01:12:48 +01:00
|
|
|
|
|
|
|
// Check if the match function matched
|
|
|
|
if (newParams) {
|
2020-07-12 21:03:49 +02:00
|
|
|
// since we require basePath be present for non-custom-routes we
|
|
|
|
// 404 here when we matched an fs route
|
2020-08-19 19:30:33 +02:00
|
|
|
if (!keepBasePath) {
|
2020-07-12 21:03:49 +02:00
|
|
|
if (!originallyHadBasePath && !(req as any)._nextDidRewrite) {
|
|
|
|
if (requireBasePath) {
|
|
|
|
// consider this a non-match so the 404 renders
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// page checker occurs before rewrites so we need to continue
|
|
|
|
// to check those since they don't always require basePath
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedUrlUpdated.pathname = currentPathname
|
|
|
|
}
|
|
|
|
|
2020-06-01 23:00:22 +02:00
|
|
|
const result = await testRoute.fn(req, res, newParams, parsedUrlUpdated)
|
2019-11-18 01:12:48 +01:00
|
|
|
|
|
|
|
// The response was handled
|
|
|
|
if (result.finished) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-12 21:03:49 +02:00
|
|
|
// since the fs route didn't match we need to re-add the basePath
|
|
|
|
// to continue checking rewrites with the basePath present
|
2020-08-19 19:30:33 +02:00
|
|
|
if (!keepBasePath) {
|
2020-07-12 21:03:49 +02:00
|
|
|
parsedUrlUpdated.pathname = originalPathname
|
|
|
|
}
|
|
|
|
|
2019-11-18 01:12:48 +01:00
|
|
|
if (result.pathname) {
|
|
|
|
parsedUrlUpdated.pathname = result.pathname
|
|
|
|
}
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2019-12-31 21:13:55 +01:00
|
|
|
if (result.query) {
|
|
|
|
parsedUrlUpdated.query = {
|
|
|
|
...parsedUrlUpdated.query,
|
|
|
|
...result.query,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:20:17 +01:00
|
|
|
// check filesystem
|
2020-06-01 23:00:22 +02:00
|
|
|
if (testRoute.check === true) {
|
2021-03-26 16:19:48 +01:00
|
|
|
if (await applyCheckTrue(parsedUrlUpdated)) {
|
2019-12-23 22:20:17 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 22:46:45 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 01:12:48 +01:00
|
|
|
return false
|
2018-12-09 22:46:45 +01:00
|
|
|
}
|
|
|
|
}
|