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

Optimize Next.js bootup compilation #50379

Merged
merged 17 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface PageStaticInfo {
ssr?: boolean
rsc?: RSCModuleType
middleware?: Partial<MiddlewareConfig>
amp?: boolean | 'hybrid'
}

const CLIENT_MODULE_LABEL =
Expand Down Expand Up @@ -488,6 +489,7 @@ export async function getPageStaticInfo(params: {
ssr,
ssg,
rsc,
amp: config.amp || false,
...(middlewareConfig && { middleware: middlewareConfig }),
...(resolvedRuntime && { runtime: resolvedRuntime }),
preferredRegion,
Expand All @@ -498,6 +500,7 @@ export async function getPageStaticInfo(params: {
ssr: false,
ssg: false,
rsc: RSC_MODULE_TYPES.server,
amp: false,
runtime: undefined,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ export class ClientReferenceEntryPlugin {
continue
}

if (clientImports.length === 0 && actionImports.length === 0) {
continue
}

const relativeRequest = isAbsoluteRequest
? path.relative(compilation.options.context, entryRequest)
: entryRequest
Expand Down
39 changes: 38 additions & 1 deletion packages/next/src/server/dev/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ function erroredPages(compilation: webpack.Compilation) {
}

export default class HotReloader {
private hasAmpEntrypoints: boolean
private hasAppRouterEntrypoints: boolean
private hasPagesRouterEntrypoints: boolean
private dir: string
private buildId: string
private interceptors: any[]
Expand Down Expand Up @@ -222,6 +225,9 @@ export default class HotReloader {
telemetry: Telemetry
}
) {
this.hasAmpEntrypoints = false
this.hasAppRouterEntrypoints = false
this.hasPagesRouterEntrypoints = false
this.buildId = buildId
this.dir = dir
this.interceptors = []
Expand Down Expand Up @@ -718,6 +724,11 @@ export default class HotReloader {
}
}

// Ensure _error is considered a `pages` page.
if (page === '/_error') {
this.hasPagesRouterEntrypoints = true
}

const hasAppDir = !!this.appDir
const isAppPath = hasAppDir && bundlePath.startsWith('app/')
const staticInfo = isEntry
Expand All @@ -731,6 +742,10 @@ export default class HotReloader {
page,
})
: {}

if (staticInfo.amp === true || staticInfo.amp === 'hybrid') {
this.hasAmpEntrypoints = true
}
const isServerComponent =
isAppPath && staticInfo.rsc !== RSC_MODULE_TYPES.client

Expand All @@ -739,6 +754,14 @@ export default class HotReloader {
: entryData.bundlePath.startsWith('app/')
? 'app'
: 'root'

if (pageType === 'pages') {
this.hasPagesRouterEntrypoints = true
}
if (pageType === 'app') {
this.hasAppRouterEntrypoints = true
}

await runDependingOnPageType({
page,
pageRuntime: staticInfo.runtime,
Expand Down Expand Up @@ -877,6 +900,21 @@ export default class HotReloader {
})
})
)

if (!this.hasAmpEntrypoints) {
delete entrypoints.amp
}
if (!this.hasPagesRouterEntrypoints) {
delete entrypoints.main
delete entrypoints['pages/_app']
delete entrypoints['pages/_error']
delete entrypoints['/_error']
delete entrypoints['pages/_document']
}
if (!this.hasAppRouterEntrypoints) {
delete entrypoints['main-app']
}

return entrypoints
}
}
Expand Down Expand Up @@ -1080,7 +1118,6 @@ export default class HotReloader {
const documentChunk = compilation.namedChunks.get('pages/_document')
// If the document chunk can't be found we do nothing
if (!documentChunk) {
console.warn('_document.js chunk not found')
return
}

Expand Down