Add custom routes to build output (#9517)
* Add custom routes to build output * De-dupe code a bit
This commit is contained in:
parent
04a7f1e85d
commit
cd8e72d5e3
4 changed files with 90 additions and 5 deletions
|
@ -51,6 +51,7 @@ import {
|
||||||
hasCustomAppGetInitialProps,
|
hasCustomAppGetInitialProps,
|
||||||
PageInfo,
|
PageInfo,
|
||||||
printTreeView,
|
printTreeView,
|
||||||
|
printCustomRoutes,
|
||||||
} from './utils'
|
} from './utils'
|
||||||
import getBaseWebpackConfig from './webpack-config'
|
import getBaseWebpackConfig from './webpack-config'
|
||||||
import { writeBuildId } from './write-build-id'
|
import { writeBuildId } from './write-build-id'
|
||||||
|
@ -718,6 +719,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
|
||||||
})
|
})
|
||||||
|
|
||||||
printTreeView(Object.keys(mappedPages), allPageInfos, isLikeServerless)
|
printTreeView(Object.keys(mappedPages), allPageInfos, isLikeServerless)
|
||||||
|
printCustomRoutes({ redirects, rewrites })
|
||||||
|
|
||||||
if (tracer) {
|
if (tracer) {
|
||||||
const parsedResults = await tracer.profiler.stopProfiling()
|
const parsedResults = await tracer.profiler.stopProfiling()
|
||||||
|
|
|
@ -11,6 +11,8 @@ import prettyBytes from '../lib/pretty-bytes'
|
||||||
import { recursiveReadDir } from '../lib/recursive-readdir'
|
import { recursiveReadDir } from '../lib/recursive-readdir'
|
||||||
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
|
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
|
||||||
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
|
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
|
||||||
|
import { Redirect, Rewrite } from '../next-server/server/next-server'
|
||||||
|
import { DEFAULT_REDIRECT_STATUS } from '../next-server/lib/constants'
|
||||||
|
|
||||||
const fsStatPromise = promisify(fs.stat)
|
const fsStatPromise = promisify(fs.stat)
|
||||||
const fileStats: { [k: string]: Promise<fs.Stats> } = {}
|
const fileStats: { [k: string]: Promise<fs.Stats> } = {}
|
||||||
|
@ -130,6 +132,59 @@ export function printTreeView(
|
||||||
console.log()
|
console.log()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function printCustomRoutes({
|
||||||
|
redirects,
|
||||||
|
rewrites,
|
||||||
|
}: {
|
||||||
|
redirects: Redirect[]
|
||||||
|
rewrites: Rewrite[]
|
||||||
|
}) {
|
||||||
|
const printRoutes = (
|
||||||
|
routes: Redirect[] | Rewrite[],
|
||||||
|
type: 'Redirects' | 'Rewrites'
|
||||||
|
) => {
|
||||||
|
const isRedirects = type === 'Redirects'
|
||||||
|
console.log(chalk.underline(type))
|
||||||
|
console.log()
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
textTable(
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'Source',
|
||||||
|
'Destination',
|
||||||
|
...(isRedirects ? ['statusCode'] : []),
|
||||||
|
].map(str => chalk.bold(str)),
|
||||||
|
...Object.entries(routes).map(([key, route]) => {
|
||||||
|
return [
|
||||||
|
route.source,
|
||||||
|
route.destination,
|
||||||
|
...(isRedirects
|
||||||
|
? [
|
||||||
|
((route as Redirect).statusCode ||
|
||||||
|
DEFAULT_REDIRECT_STATUS) + '',
|
||||||
|
]
|
||||||
|
: []),
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
{
|
||||||
|
align: ['l', 'l', 'l'],
|
||||||
|
stringLength: str => stripAnsi(str).length,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
console.log()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (redirects.length) {
|
||||||
|
printRoutes(redirects, 'Redirects')
|
||||||
|
}
|
||||||
|
if (rewrites.length) {
|
||||||
|
printRoutes(rewrites, 'Rewrites')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function getPageSizeInKb(
|
export async function getPageSizeInKb(
|
||||||
page: string,
|
page: string,
|
||||||
distPath: string,
|
distPath: string,
|
||||||
|
|
|
@ -44,12 +44,12 @@ const getCustomRouteMatcher = pathMatch(true)
|
||||||
|
|
||||||
type NextConfig = any
|
type NextConfig = any
|
||||||
|
|
||||||
type Rewrite = {
|
export type Rewrite = {
|
||||||
source: string
|
source: string
|
||||||
destination: string
|
destination: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Redirect = Rewrite & {
|
export type Redirect = Rewrite & {
|
||||||
statusCode?: number
|
statusCode?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/* eslint-env jest */
|
/* eslint-env jest */
|
||||||
/* global jasmine */
|
/* global jasmine */
|
||||||
import url from 'url'
|
import url from 'url'
|
||||||
|
import stripAnsi from 'strip-ansi'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import webdriver from 'next-webdriver'
|
import webdriver from 'next-webdriver'
|
||||||
|
@ -21,6 +22,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
|
||||||
let appDir = join(__dirname, '..')
|
let appDir = join(__dirname, '..')
|
||||||
const nextConfigPath = join(appDir, 'next.config.js')
|
const nextConfigPath = join(appDir, 'next.config.js')
|
||||||
let nextConfigContent
|
let nextConfigContent
|
||||||
|
let stdout = ''
|
||||||
let appPort
|
let appPort
|
||||||
let app
|
let app
|
||||||
|
|
||||||
|
@ -288,6 +290,22 @@ const runTests = (isDev = false) => {
|
||||||
dynamicRoutes: [],
|
dynamicRoutes: [],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should have redirects/rewrites in build output', async () => {
|
||||||
|
const manifest = await fs.readJSON(
|
||||||
|
join(appDir, '.next/routes-manifest.json')
|
||||||
|
)
|
||||||
|
const cleanStdout = stripAnsi(stdout)
|
||||||
|
expect(cleanStdout).toContain('Redirects')
|
||||||
|
expect(cleanStdout).toContain('Rewrites')
|
||||||
|
expect(cleanStdout).toMatch(/Source.*?Destination.*?statusCode/i)
|
||||||
|
|
||||||
|
for (const route of [...manifest.redirects, ...manifest.rewrites]) {
|
||||||
|
expect(cleanStdout).toMatch(
|
||||||
|
new RegExp(`${route.source}.*?${route.destination}`)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,7 +321,10 @@ describe('Custom routes', () => {
|
||||||
|
|
||||||
describe('production mode', () => {
|
describe('production mode', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await nextBuild(appDir)
|
const { stdout: buildStdout } = await nextBuild(appDir, [], {
|
||||||
|
stdout: true,
|
||||||
|
})
|
||||||
|
stdout = buildStdout
|
||||||
appPort = await findPort()
|
appPort = await findPort()
|
||||||
app = await nextStart(appDir, appPort)
|
app = await nextStart(appDir, appPort)
|
||||||
})
|
})
|
||||||
|
@ -319,9 +340,16 @@ describe('Custom routes', () => {
|
||||||
nextConfigContent.replace(/\/\/ target/, 'target'),
|
nextConfigContent.replace(/\/\/ target/, 'target'),
|
||||||
'utf8'
|
'utf8'
|
||||||
)
|
)
|
||||||
await nextBuild(appDir)
|
const { stdout: buildStdout } = await nextBuild(appDir, [], {
|
||||||
|
stdout: true,
|
||||||
|
})
|
||||||
|
stdout = buildStdout
|
||||||
appPort = await findPort()
|
appPort = await findPort()
|
||||||
app = await nextStart(appDir, appPort)
|
app = await nextStart(appDir, appPort, {
|
||||||
|
onStdout: msg => {
|
||||||
|
stdout += msg
|
||||||
|
},
|
||||||
|
})
|
||||||
})
|
})
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await killApp(app)
|
await killApp(app)
|
||||||
|
|
Loading…
Reference in a new issue