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

Improve Build Output for SSG #9719

Merged
merged 7 commits into from
Dec 12, 2019
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
18 changes: 12 additions & 6 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
)

let isStatic = false
let isSsg = false
let ssgPageRoutes: string[] | null = null

pagesManifest[page] = bundleRelative.replace(/\\/g, '/')

Expand Down Expand Up @@ -432,25 +434,29 @@ export default async function build(dir: string, conf = null): Promise<void> {

if (result.prerender) {
sprPages.add(page)
isSsg = true

if (result.prerenderRoutes) {
additionalSprPaths.set(page, result.prerenderRoutes)
ssgPageRoutes = result.prerenderRoutes
}
}

if (result.static && customAppGetInitialProps === false) {
} else if (result.static && customAppGetInitialProps === false) {
staticPages.add(page)
isStatic = true
} else if (result.prerender) {
sprPages.add(page)
}
} catch (err) {
if (err.message !== 'INVALID_DEFAULT_EXPORT') throw err
invalidPages.add(page)
}
}

pageInfos.set(page, { size, serverBundle, static: isStatic })
pageInfos.set(page, {
size,
serverBundle,
static: isStatic,
isSsg,
ssgPageRoutes,
})
})
)
staticCheckWorkers.end()
Expand Down
72 changes: 47 additions & 25 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export function collectPages(
export interface PageInfo {
isAmp?: boolean
size: number
static?: boolean
static: boolean
isSsg: boolean
ssgPageRoutes: string[] | null
serverBundle: string
}

Expand All @@ -56,8 +58,8 @@ export function printTreeView(
return chalk.red.bold(size)
}

const messages: string[][] = [
['Page', 'Size'].map(entry => chalk.underline(entry)),
const messages: [string, string][] = [
['Page', 'Size'].map(entry => chalk.underline(entry)) as [string, string],
]

list
Expand All @@ -79,10 +81,10 @@ export function printTreeView(
item.startsWith('/_')
? ' '
: pageInfo && pageInfo.static
? '*'
: serverless
? 'λ'
: 'σ'
? ''
: pageInfo && pageInfo.isSsg
? ''
: 'λ'
} ${item}`,
pageInfo
? pageInfo.isAmp
Expand All @@ -92,11 +94,28 @@ export function printTreeView(
: ''
: '',
])

if (pageInfo && pageInfo.ssgPageRoutes && pageInfo.ssgPageRoutes.length) {
const totalRoutes = pageInfo.ssgPageRoutes.length
const previewPages = totalRoutes === 4 ? 4 : 3
const contSymbol = i === list.length - 1 ? ' ' : '├'

const routes = pageInfo.ssgPageRoutes.slice(0, previewPages)
if (totalRoutes > previewPages) {
const remaining = totalRoutes - previewPages
routes.push(`[+${remaining} more paths]`)
}

routes.forEach((slug, index, { length }) => {
const innerSymbol = index === length - 1 ? '└' : '├'
messages.push([`${contSymbol} ${innerSymbol} ${slug}`, ''])
})
}
})

console.log(
textTable(messages, {
align: ['l', 'l', 'r', 'r'],
align: ['l', 'l'],
stringLength: str => stripAnsi(str).length,
})
)
Expand All @@ -105,23 +124,26 @@ export function printTreeView(
console.log(
textTable(
[
serverless
? [
'λ',
'(Lambda)',
`page was emitted as a lambda (i.e. ${chalk.cyan(
'getInitialProps'
)})`,
]
: [
'σ',
'(Server)',
`page will be server rendered (i.e. ${chalk.cyan(
'getInitialProps'
)})`,
],
['*', '(Static File)', 'page was prerendered as static HTML'],
],
[
'λ',
serverless ? '(Lambda)' : '(Server)',
`server-side renders at runtime (uses ${chalk.cyan(
'getInitialProps'
)} or ${chalk.cyan('getServerProps')})`,
],
[
'○',
'(Static)',
'automatically rendered as static HTML (uses no initial props)',
],
[
'●',
'(SSG)',
`automatically generated as static HTML + JSON (uses ${chalk.cyan(
'getStaticProps'
)})`,
],
] as [string, string, string][],
{
align: ['l', 'l', 'l'],
stringLength: str => stripAnsi(str).length,
Expand Down
1 change: 1 addition & 0 deletions test/integration/prerender/pages/blog/[post]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function unstable_getStaticPaths() {
'/blog/post-1',
{ params: { post: 'post-2' } },
'/blog/[post3]',
'/blog/post-4',
'/blog/post.1',
]
}
Expand Down
16 changes: 15 additions & 1 deletion test/integration/prerender/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const expectedManifestRoutes = () => ({
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/blog/post-4': {
dataRoute: `/_next/data/${buildId}/blog/post-4.json`,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/blog/post-1/comment-1': {
dataRoute: `/_next/data/${buildId}/blog/post-1/comment-1.json`,
initialRevalidateSeconds: 2,
Expand Down Expand Up @@ -478,9 +483,12 @@ describe('SPR Prerender', () => {
})

describe('production mode', () => {
let buildOutput = ''
beforeAll(async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
const { stdout } = await nextBuild(appDir, [], { stdout: true })
buildOutput = stdout

stderr = ''
appPort = await findPort()
app = await nextStart(appDir, appPort, {
Expand All @@ -493,6 +501,12 @@ describe('SPR Prerender', () => {
})
afterAll(() => killApp(app))

it('should of formatted build output correctly', () => {
expect(buildOutput).toMatch(/○ \/normal/)
expect(buildOutput).toMatch(/● \/blog\/\[post\]/)
expect(buildOutput).toMatch(/\+2 more paths/)
})

runTests()
})

Expand Down