Unverified Commit 2b2c2acf authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Refactor handling of addPageEntry promise (#39547)

Follow-up to #39162. Refactors `addPageEntry` to no longer add into the `added` map.


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 3c9ad33c
...@@ -392,9 +392,6 @@ export function onDemandEntryHandler({ ...@@ -392,9 +392,6 @@ export function onDemandEntryHandler({
appDir appDir
) )
let entryAdded = false
const added = new Map<CompilerNameValues, Promise<void>>()
const isServerComponent = serverComponentRegex.test( const isServerComponent = serverComponentRegex.test(
pagePathData.absolutePagePath pagePathData.absolutePagePath
) )
...@@ -403,55 +400,46 @@ export function onDemandEntryHandler({ ...@@ -403,55 +400,46 @@ export function onDemandEntryHandler({
const addPageEntry = ( const addPageEntry = (
compilerType: CompilerNameValues compilerType: CompilerNameValues
): Promise<void> => { ): {
let resolve: (value: void | PromiseLike<void>) => void pageKey: string
let reject: (reason?: any) => void newEntry: boolean
const promise = new Promise<void>((res, rej) => { shouldInvalidate: boolean
resolve = res } => {
reject = rej
})
const pageKey = `${compilerType}${pagePathData.page}` const pageKey = `${compilerType}${pagePathData.page}`
if (entries[pageKey]) { if (entries[pageKey]) {
added.set(compilerType, promise)
entries[pageKey].dispose = false entries[pageKey].dispose = false
entries[pageKey].lastActiveTime = Date.now() entries[pageKey].lastActiveTime = Date.now()
if (entries[pageKey].status === BUILT) { if (entries[pageKey].status === BUILT) {
resolve!() return {
return promise pageKey,
} newEntry: false,
} else { shouldInvalidate: false,
if (
compilerType === COMPILER_NAMES.client &&
(isServerComponent || isInsideAppDir)
) {
// Skip adding the client entry here.
} else {
added.set(compilerType, promise)
entryAdded = true
entries[pageKey] = {
type: EntryTypes.ENTRY,
absolutePagePath: pagePathData.absolutePagePath,
request: pagePathData.absolutePagePath,
bundlePath: pagePathData.bundlePath,
dispose: false,
lastActiveTime: Date.now(),
status: ADDED,
} }
} }
}
doneCallbacks!.once(pageKey, (err: Error) => { return {
if (err) { pageKey,
return reject(err) newEntry: false,
shouldInvalidate: true,
} }
resolve() }
})
entries[pageKey] = {
type: EntryTypes.ENTRY,
absolutePagePath: pagePathData.absolutePagePath,
request: pagePathData.absolutePagePath,
bundlePath: pagePathData.bundlePath,
dispose: false,
lastActiveTime: Date.now(),
status: ADDED,
}
return promise return {
pageKey,
newEntry: true,
shouldInvalidate: true,
}
} }
const staticInfo = await getPageStaticInfo({ const staticInfo = await getPageStaticInfo({
...@@ -459,30 +447,62 @@ export function onDemandEntryHandler({ ...@@ -459,30 +447,62 @@ export function onDemandEntryHandler({
nextConfig, nextConfig,
}) })
const added = new Map<
CompilerNameValues,
ReturnType<typeof addPageEntry>
>()
await runDependingOnPageType({ await runDependingOnPageType({
page: pagePathData.page, page: pagePathData.page,
pageRuntime: staticInfo.runtime, pageRuntime: staticInfo.runtime,
onClient: () => { onClient: () => {
addPageEntry(COMPILER_NAMES.client) // Skip adding the client entry for app / Server Components.
if (isServerComponent || isInsideAppDir) {
return
}
added.set(COMPILER_NAMES.client, addPageEntry(COMPILER_NAMES.client))
}, },
onServer: () => { onServer: () => {
addPageEntry(COMPILER_NAMES.server) added.set(COMPILER_NAMES.server, addPageEntry(COMPILER_NAMES.server))
}, },
onEdgeServer: () => { onEdgeServer: () => {
addPageEntry(COMPILER_NAMES.edgeServer) added.set(
COMPILER_NAMES.edgeServer,
addPageEntry(COMPILER_NAMES.edgeServer)
)
}, },
}) })
if (entryAdded) { const addedValues = [...added.values()]
const entriesThatShouldBeInvalidated = addedValues.filter(
(entry) => entry.shouldInvalidate
)
const hasNewEntry = addedValues.some((entry) => entry.newEntry)
if (hasNewEntry) {
reportTrigger( reportTrigger(
!clientOnly && added.size > 1 !clientOnly && hasNewEntry
? `${pagePathData.page} (client and server)` ? `${pagePathData.page} (client and server)`
: pagePathData.page : pagePathData.page
) )
invalidator.invalidate([...added.keys()])
} }
await Promise.all(added.values()) if (entriesThatShouldBeInvalidated.length > 0) {
const invalidatePromises = entriesThatShouldBeInvalidated.map(
({ pageKey }) => {
return new Promise<void>((resolve, reject) => {
doneCallbacks!.once(pageKey, (err: Error) => {
if (err) {
return reject(err)
}
resolve()
})
})
}
)
invalidator.invalidate([...added.keys()])
await Promise.all(invalidatePromises)
}
}, },
onHMR(client: ws) { onHMR(client: ws) {
......
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