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

Redesign custom-routes output and show headers in output #10444

Merged
merged 1 commit into from
Feb 7, 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
2 changes: 1 addition & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
isModern: config.experimental.modern,
}
)
printCustomRoutes({ redirects, rewrites })
printCustomRoutes({ redirects, rewrites, headers })

if (tracer) {
const parsedResults = await tracer.profiler.stopProfiling()
Expand Down
80 changes: 49 additions & 31 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import textTable from 'next/dist/compiled/text-table'
import path from 'path'
import { isValidElementType } from 'react-is'
import stripAnsi from 'strip-ansi'
import {
Redirect,
Rewrite,
getRedirectStatus,
} from '../lib/check-custom-routes'
import { Redirect, Rewrite, Header } from '../lib/check-custom-routes'
import {
SSG_GET_INITIAL_PROPS_CONFLICT,
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
Expand Down Expand Up @@ -232,43 +228,62 @@ export async function printTreeView(
export function printCustomRoutes({
redirects,
rewrites,
headers,
}: {
redirects: Redirect[]
rewrites: Rewrite[]
headers: Header[]
}) {
const printRoutes = (
routes: Redirect[] | Rewrite[],
type: 'Redirects' | 'Rewrites'
routes: Redirect[] | Rewrite[] | Header[],
type: 'Redirects' | 'Rewrites' | 'Headers'
) => {
const isRedirects = type === 'Redirects'
const isHeaders = type === 'Headers'
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
? [getRedirectStatus(route as Redirect) + '']
: []),
]
}),
],
{
align: ['l', 'l', 'l'],
stringLength: str => stripAnsi(str).length,
/*
┌ source
├ permanent/statusCode
└ destination
*/
const routesStr = (routes as any[])
.map((route: { source: string }) => {
let routeStr = `┌ source: ${route.source}\n`

if (!isHeaders) {
const r = route as Rewrite
routeStr += `${isRedirects ? '├' : '└'} destination: ${
r.destination
}\n`
}
)
)
console.log()
if (isRedirects) {
const r = route as Redirect
routeStr += `└ ${
r.statusCode
? `status: ${r.statusCode}`
: `permanent: ${r.permanent}`
}\n`
}

if (isHeaders) {
const r = route as Header
routeStr += `└ headers:\n`

for (let i = 0; i < r.headers.length; i++) {
const header = r.headers[i]
const last = i === headers.length - 1

routeStr += ` ${last ? '└' : '├'} ${header.key}: ${header.value}\n`
}
}

return routeStr
})
.join('\n')

console.log(routesStr, '\n')
}

if (redirects.length) {
Expand All @@ -277,6 +292,9 @@ export function printCustomRoutes({
if (rewrites.length) {
printRoutes(rewrites, 'Rewrites')
}
if (headers.length) {
printRoutes(headers, 'Headers')
}
}

type BuildManifestShape = { pages: { [k: string]: string[] } }
Expand Down
21 changes: 14 additions & 7 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp'
import {
launchApp,
killApp,
Expand Down Expand Up @@ -548,14 +547,22 @@ const runTests = (isDev = false) => {
const cleanStdout = stripAnsi(stdout)
expect(cleanStdout).toContain('Redirects')
expect(cleanStdout).toContain('Rewrites')
expect(cleanStdout).toMatch(/Source.*?Destination.*?statusCode/i)
expect(cleanStdout).toContain('Headers')
expect(cleanStdout).toMatch(/source.*?/i)
expect(cleanStdout).toMatch(/destination.*?/i)

for (const route of [...manifest.redirects, ...manifest.rewrites]) {
expect(cleanStdout).toMatch(
new RegExp(
`${escapeRegex(route.source)}.*?${escapeRegex(route.destination)}`
)
)
expect(cleanStdout).toContain(route.source)
expect(cleanStdout).toContain(route.destination)
}

for (const route of manifest.headers) {
expect(cleanStdout).toContain(route.source)

for (const header of route.headers) {
expect(cleanStdout).toContain(header.key)
expect(cleanStdout).toContain(header.value)
}
}
})
} else {
Expand Down