Skip to content

Commit f121717

Browse files
authored
add types for __next_app__ module loading functions (#74566)
`__next_app__.require` and `__next_app__.loadChunk` were typed as `any` for no good reason. i made them more strict + adjusted callsites as needed. also contains a drive-by fix for `wrapClientComponentLoader` -- it was using a `try ... finally` without awaiting, so it wasn't measuring anything (because `loadChunk`, being async, would always return immediately)
1 parent 71f837f commit f121717

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

packages/next/src/build/templates/app-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export { tree, pages }
2121
export { default as GlobalError } from 'VAR_MODULE_GLOBAL_ERROR' with { 'turbopack-transition': 'next-server-utility' }
2222

2323
// These are injected by the loader afterwards.
24-
declare const __next_app_require__: any
25-
declare const __next_app_load_chunk__: any
24+
declare const __next_app_require__: (id: string | number) => unknown
25+
declare const __next_app_load_chunk__: (id: string | number) => Promise<unknown>
2626

2727
// INJECT:__next_app_require__
2828
// INJECT:__next_app_load_chunk__

packages/next/src/server/app-render/action-handler.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -912,12 +912,14 @@ export async function handleAction({
912912
}
913913
}
914914

915-
const actionHandler = (
916-
await ComponentMod.__next_app__.require(actionModId)
917-
)[
918-
// `actionId` must exist if we got here, as otherwise we would have thrown an error above
919-
actionId!
920-
]
915+
const actionMod = (await ComponentMod.__next_app__.require(
916+
actionModId
917+
)) as Record<string, (...args: unknown[]) => Promise<unknown>>
918+
const actionHandler =
919+
actionMod[
920+
// `actionId` must exist if we got here, as otherwise we would have thrown an error above
921+
actionId!
922+
]
921923

922924
const returnVal = await workUnitAsyncStorage.run(requestStore, () =>
923925
actionHandler.apply(null, boundActionArguments)

packages/next/src/server/app-render/app-render.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,13 @@ async function renderToHTMLOrFlightImpl(
11891189
// cache reads we wrap the loadChunk in this tracking. This allows us
11901190
// to treat chunk loading with similar semantics as cache reads to avoid
11911191
// async loading chunks from causing a prerender to abort too early.
1192-
// @ts-ignore
1193-
globalThis.__next_chunk_load__ = (...args: Array<any>) => {
1192+
const __next_chunk_load__: typeof instrumented.loadChunk = (...args) => {
11941193
const loadingChunk = instrumented.loadChunk(...args)
11951194
trackChunkLoading(loadingChunk)
11961195
return loadingChunk
11971196
}
1197+
// @ts-expect-error
1198+
globalThis.__next_chunk_load__ = __next_chunk_load__
11981199
}
11991200

12001201
if (process.env.NODE_ENV === 'development') {

packages/next/src/server/client-component-renderer-logger.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
import type { AppPageModule } from './route-modules/app-page/module'
2+
13
// Combined load times for loading client components
24
let clientComponentLoadStart = 0
35
let clientComponentLoadTimes = 0
46
let clientComponentLoadCount = 0
57

6-
export function wrapClientComponentLoader(ComponentMod: any) {
8+
export function wrapClientComponentLoader(
9+
ComponentMod: AppPageModule
10+
): AppPageModule['__next_app__'] {
711
if (!('performance' in globalThis)) {
812
return ComponentMod.__next_app__
913
}
1014

1115
return {
12-
require: (...args: any[]) => {
16+
require: (...args) => {
1317
const startTime = performance.now()
1418

1519
if (clientComponentLoadStart === 0) {
@@ -23,13 +27,15 @@ export function wrapClientComponentLoader(ComponentMod: any) {
2327
clientComponentLoadTimes += performance.now() - startTime
2428
}
2529
},
26-
loadChunk: (...args: any[]) => {
30+
loadChunk: (...args) => {
2731
const startTime = performance.now()
28-
try {
29-
return ComponentMod.__next_app__.loadChunk(...args)
30-
} finally {
32+
const result = ComponentMod.__next_app__.loadChunk(...args)
33+
// Avoid wrapping `loadChunk`'s result in an extra promise in case something like React depends on its identity.
34+
// We only need to know when it's settled.
35+
result.finally(() => {
3136
clientComponentLoadTimes += performance.now() - startTime
32-
}
37+
})
38+
return result
3339
},
3440
}
3541
}

0 commit comments

Comments
 (0)