Skip to content

Commit c1f3462

Browse files
authored
Turbopack Build: Optimize instrumentation hook generation (#77832)
## For Maintainers ### What? - Add `compiled in` for middleware and instrumentation in Turbopack dev. - Fixed a bug where instrumentation.js was being compiled like a page file, local testing moves bootup from 2.5s to 700ms when you have a instrumentation file. ### How? - Added early returns in `hot-reloader-turbopack.ts` for middleware and instrumentation paths when there's no route definition, those cases are handled separately already. - Added build status indicators in `turbopack-utils.ts` for middleware and instrumentation using `startBuilding` and `finishBuilding` hooks - Added proper naming for Node.js and Edge instrumentation in the build status
1 parent 5ff57ea commit c1f3462

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Diff for: packages/next/src/server/dev/hot-reloader-turbopack.ts

+9
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,15 @@ export async function createHotReloaderTurbopack(
948948
isApp,
949949
url: requestUrl,
950950
}) {
951+
// When there is no route definition this is an internal file not a route the user added.
952+
// Middleware and instrumentation are handled in turbpack-utils.ts handleEntrypoints instead.
953+
if (!definition) {
954+
if (inputPage === '/middleware') return
955+
if (inputPage === '/src/middleware') return
956+
if (inputPage === '/instrumentation') return
957+
if (inputPage === '/src/instrumentation') return
958+
}
959+
951960
return hotReloaderSpan
952961
.traceChild('ensure-page', {
953962
inputPage,

Diff for: packages/next/src/server/dev/turbopack-utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,21 @@ export async function handleEntrypoints({
663663
name: string,
664664
prop: 'nodeJs' | 'edge'
665665
) => {
666+
const prettyName = {
667+
nodeJs: 'Node.js',
668+
edge: 'Edge',
669+
}
670+
const finishBuilding = dev.hooks.startBuilding(
671+
`instrumentation ${prettyName[prop]}`,
672+
undefined,
673+
true
674+
)
666675
const key = getEntryKey('root', 'server', name)
667676

668677
const writtenEndpoint = await instrumentation[prop].writeToDisk()
669678
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
670679
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
680+
finishBuilding()
671681
}
672682
await processInstrumentation('instrumentation.nodeJs', 'nodeJs')
673683
await processInstrumentation('instrumentation.edge', 'edge')
@@ -700,6 +710,11 @@ export async function handleEntrypoints({
700710
const endpoint = middleware.endpoint
701711

702712
async function processMiddleware() {
713+
const finishBuilding = dev.hooks.startBuilding(
714+
'middleware',
715+
undefined,
716+
true
717+
)
703718
const writtenEndpoint = await endpoint.writeToDisk()
704719
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
705720
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
@@ -714,6 +729,7 @@ export async function handleEntrypoints({
714729
matchers: middlewareConfig.matchers,
715730
}
716731
}
732+
finishBuilding()
717733
}
718734
await processMiddleware()
719735

0 commit comments

Comments
 (0)