Skip to content

Commit

Permalink
Prepare custom routes loading for adding additional routes (#13857)
Browse files Browse the repository at this point in the history
Extracted from #13333, the same exact code lives in that PR as well, but we can merge this separately if it makes reviewing #13333 easier

This PR does 3 things 
- deduplicate code from build and next-dev-server that loads custom routes from next.config.js  (`loadCustomRoutes`)
- in `loadCustomRoutes`, load these rewrites, headers and redirects configs concurrently instead of sequentially.
- in next-server, make `this.customRoutes` always defined, this allows us to remove the big `if` around its initialization code in `generateRoutes`, which in turn makes it possible to reuse this code for other routing than user defined routes, which is how #13333 adds its redirects.
  • Loading branch information
Janpot authored Jun 9, 2020
1 parent 7d648da commit 9da99bc
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 196 deletions.
27 changes: 4 additions & 23 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import nanoid from 'next/dist/compiled/nanoid/index.js'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import path from 'path'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import checkCustomRoutes, {
import loadCustomRoutes, {
getRedirectStatus,
Header,
Redirect,
Rewrite,
RouteType,
} from '../lib/check-custom-routes'
} from '../lib/load-custom-routes'
import {
PAGES_404_GET_INITIAL_PROPS_ERROR,
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
Expand Down Expand Up @@ -112,25 +110,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
const { target } = config
const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = path.join(dir, config.distDir)
const headers: Header[] = []
const rewrites: Rewrite[] = []
const redirects: Redirect[] = []

if (typeof config.experimental.redirects === 'function') {
const _redirects = await config.experimental.redirects()
checkCustomRoutes(_redirects, 'redirect')
redirects.push(..._redirects)
}
if (typeof config.experimental.rewrites === 'function') {
const _rewrites = await config.experimental.rewrites()
checkCustomRoutes(_rewrites, 'rewrite')
rewrites.push(..._rewrites)
}
if (typeof config.experimental.headers === 'function') {
const _headers = await config.experimental.headers()
checkCustomRoutes(_headers, 'header')
headers.push(..._headers)
}

const { headers, rewrites, redirects } = await loadCustomRoutes(config)

if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) {
const cacheDir = path.join(distDir, 'cache')
Expand Down
13 changes: 7 additions & 6 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import textTable from 'next/dist/compiled/text-table'
import path from 'path'
import { isValidElementType } from 'react-is'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { Redirect, Rewrite, Header } from '../lib/check-custom-routes'
import {
Redirect,
Rewrite,
Header,
CustomRoutes,
} from '../lib/load-custom-routes'
import {
SSG_GET_INITIAL_PROPS_CONFLICT,
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
Expand Down Expand Up @@ -279,11 +284,7 @@ export function printCustomRoutes({
redirects,
rewrites,
headers,
}: {
redirects: Redirect[]
rewrites: Rewrite[]
headers: Header[]
}) {
}: CustomRoutes) {
const printRoutes = (
routes: Redirect[] | Rewrite[] | Header[],
type: 'Redirects' | 'Rewrites' | 'Headers'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function tryParsePath(route: string, handleUrl?: boolean): ParseAttemptResult {

export type RouteType = 'rewrite' | 'redirect' | 'header'

export default function checkCustomRoutes(
function checkCustomRoutes(
routes: Redirect[] | Header[] | Rewrite[],
type: RouteType
): void {
Expand Down Expand Up @@ -302,3 +302,52 @@ export default function checkCustomRoutes(
throw new Error(`Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found`)
}
}

export interface CustomRoutes {
headers: Header[]
rewrites: Rewrite[]
redirects: Redirect[]
}

async function loadRedirects(config: any) {
if (typeof config.experimental.redirects !== 'function') {
return []
}
const _redirects = await config.experimental.redirects()
checkCustomRoutes(_redirects, 'redirect')
return _redirects
}

async function loadRewrites(config: any) {
if (typeof config.experimental.rewrites !== 'function') {
return []
}
const _rewrites = await config.experimental.rewrites()
checkCustomRoutes(_rewrites, 'rewrite')
return _rewrites
}

async function loadHeaders(config: any) {
if (typeof config.experimental.headers !== 'function') {
return []
}
const _headers = await config.experimental.headers()
checkCustomRoutes(_headers, 'header')
return _headers
}

export default async function loadCustomRoutes(
config: any
): Promise<CustomRoutes> {
const [headers, rewrites, redirects] = await Promise.all([
loadHeaders(config),
loadRewrites(config),
loadRedirects(config),
])

return {
headers,
rewrites,
redirects,
}
}
Loading

0 comments on commit 9da99bc

Please sign in to comment.