Skip to content

Commit

Permalink
Refactor handling of addPageEntry promise (#39547)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
timneutkens committed Aug 12, 2022
1 parent 3c9ad33 commit 2b2c2ac
Showing 1 changed file with 68 additions and 48 deletions.
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

0 comments on commit 2b2c2ac

Please sign in to comment.