Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare custom routes loading for adding additional routes #13857

Merged
merged 3 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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