Unverified Commit 6123a970 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix missing trace file and unhandledRejection in ensurePage (#55553)

Seems we occasionally have unhandledRejections with `ensurePage` due to
the our memoize handling not attaching `.catch` quick enough. This
updates to ensure `.catch()` is always present for that promise and
re-throwing separately.

Also, ensures the necessary `module.compiled` files for `route-modules`
are included in our build traces.

x-ref: [slack
thread](https://vercel.slack.com/archives/C04KC8A53T7/p1695072157528389?thread_ts=1695060035.024789&cid=C04KC8A53T7)
parent b5beb3a9
......@@ -2269,12 +2269,13 @@ export default async function build(
const moduleTypes = ['app-page', 'pages']
for (const type of moduleTypes) {
const modulePath = require.resolve(
`next/dist/server/future/route-modules/${type}/module.compiled`
)
const relativeModulePath = path.relative(root, modulePath)
const contextDir = path.join(
path.dirname(
require.resolve(
`next/dist/server/future/route-modules/${type}/module`
)
),
path.dirname(modulePath),
'vendored',
'contexts'
)
......@@ -2287,6 +2288,8 @@ export default async function build(
addToTracedFiles(root, itemPath, tracedFiles)
addToTracedFiles(root, itemPath, minimalTracedFiles)
}
addToTracedFiles(root, relativeModulePath, tracedFiles)
addToTracedFiles(root, relativeModulePath, minimalTracedFiles)
}
await Promise.all([
......
......@@ -1487,12 +1487,16 @@ export default class HotReloader implements NextJsHotReloaderInterface {
if (error) {
return Promise.reject(error)
}
return this.onDemandEntries?.ensurePage({
const res = (await this.onDemandEntries?.ensurePage({
page,
clientOnly,
appPaths,
match,
isApp,
}) as any
})) as any
if (res && res.err) {
throw res.err
}
}
}
......@@ -726,9 +726,12 @@ export function onDemandEntryHandler({
const isInsideAppDir =
!!appDir && pagePathData.absolutePagePath.startsWith(appDir)
if (typeof isApp === 'boolean' && !(isApp === isInsideAppDir)) {
if (typeof isApp === 'boolean' && isApp !== isInsideAppDir) {
Error.stackTraceLimit = 15
throw new Error(
'Ensure bailed, found path does not match ensure type (pages/app)'
`Ensure bailed, found path "${
pagePathData.page
}" does not match ensure type (${isApp ? 'app' : 'pages'})`
)
}
......@@ -896,7 +899,7 @@ export function onDemandEntryHandler({
}
// Make sure that we won't have multiple invalidations ongoing concurrently.
const curEnsurePage = new Map<string, Promise<void>>()
const curEnsurePage = new Map<string, Promise<void | { err: Error }>>()
return {
async ensurePage({
......@@ -912,21 +915,35 @@ export function onDemandEntryHandler({
match?: RouteMatch
isApp?: boolean
}) {
if (curEnsurePage.has(page)) {
return curEnsurePage.get(page)
const ensureKey = JSON.stringify({ page, clientOnly, match, appPaths })
const pendingEnsure = curEnsurePage.get(ensureKey)
if (pendingEnsure) {
const res = await pendingEnsure
if (res && res.err) {
throw res.err
}
}
const promise = ensurePageImpl({
page,
clientOnly,
appPaths,
match,
isApp,
}).finally(() => {
curEnsurePage.delete(page)
// we have to catch the error since we are memoizing
// otherwise we can hit random unhandledRejections
})
curEnsurePage.set(page, promise)
.catch((err) => {
return { err }
})
.finally(() => {
curEnsurePage.delete(ensureKey)
})
await promise
curEnsurePage.set(ensureKey, promise)
return promise
},
onHMR(client: ws, getHmrServerError: () => Error | null) {
let bufferedHmrServerError: Error | null = null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment