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

Refactor handling of addPageEntry promise #39547

Merged
merged 5 commits into from Aug 12, 2022
Merged
Changes from 2 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
116 changes: 68 additions & 48 deletions packages/next/server/dev/on-demand-entry-handler.ts
Expand Up @@ -392,9 +392,6 @@ export function onDemandEntryHandler({
appDir
)

let entryAdded = false
const added = new Map<CompilerNameValues, Promise<void>>()

const isServerComponent = serverComponentRegex.test(
pagePathData.absolutePagePath
)
Expand All @@ -403,86 +400,109 @@ export function onDemandEntryHandler({

const addPageEntry = (
compilerType: CompilerNameValues
): Promise<void> => {
let resolve: (value: void | PromiseLike<void>) => void
let reject: (reason?: any) => void
const promise = new Promise<void>((res, rej) => {
resolve = res
reject = rej
})

): {
pageKey: string
newEntry: boolean
shouldInvalidate: boolean
} => {
const pageKey = `${compilerType}${pagePathData.page}`

if (entries[pageKey]) {
added.set(compilerType, promise)

entries[pageKey].dispose = false
entries[pageKey].lastActiveTime = Date.now()
if (entries[pageKey].status === BUILT) {
resolve!()
return promise
}
} else {
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,
return {
pageKey,
newEntry: false,
shouldInvalidate: false,
}
}
}

doneCallbacks!.once(pageKey, (err: Error) => {
if (err) {
return reject(err)
return {
pageKey,
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({
pageFilePath: pagePathData.absolutePagePath,
nextConfig,
})

const added = new Map<
CompilerNameValues,
ReturnType<typeof addPageEntry>
>()

await runDependingOnPageType({
page: pagePathData.page,
pageRuntime: staticInfo.runtime,
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: () => {
addPageEntry(COMPILER_NAMES.server)
added.set(COMPILER_NAMES.server, addPageEntry(COMPILER_NAMES.server))
},
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(
!clientOnly && added.size > 1
!clientOnly && hasNewEntry
? `${pagePathData.page} (client and server)`
: 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) {
Expand Down