Skip to content

Commit

Permalink
Cache concurrent ensurePage requests for the same page (#52360)
Browse files Browse the repository at this point in the history
This should resolve part of the `MaxListenersExceededWarning` problem mentioned in #50909. When there are too many concurrent requests sent to the dev server, we only need to handle the first.

Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
  • Loading branch information
shuding and timneutkens committed Jul 7, 2023
1 parent b8589df commit e50c918
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 192 deletions.
26 changes: 15 additions & 11 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,34 +416,35 @@ export function getClientEntry(opts: {
: pageLoader
}

export async function runDependingOnPageType<T>(params: {
export function runDependingOnPageType<T>(params: {
onClient: () => T
onEdgeServer: () => T
onServer: () => T
page: string
pageRuntime: ServerRuntime
pageType?: 'app' | 'pages' | 'root'
}): Promise<void> {
}): void {
if (params.pageType === 'root' && isInstrumentationHookFile(params.page)) {
await Promise.all([params.onServer(), params.onEdgeServer()])
params.onServer()
params.onEdgeServer()
return
}

if (isMiddlewareFile(params.page)) {
await params.onEdgeServer()
params.onEdgeServer()
return
}
if (isAPIRoute(params.page)) {
if (isEdgeRuntime(params.pageRuntime)) {
await params.onEdgeServer()
params.onEdgeServer()
return
}

await params.onServer()
params.onServer()
return
}
if (params.page === '/_document') {
await params.onServer()
params.onServer()
return
}
if (
Expand All @@ -452,15 +453,18 @@ export async function runDependingOnPageType<T>(params: {
params.page === '/404' ||
params.page === '/500'
) {
await Promise.all([params.onClient(), params.onServer()])
params.onClient()
params.onServer()
return
}
if (isEdgeRuntime(params.pageRuntime)) {
await Promise.all([params.onClient(), params.onEdgeServer()])
params.onClient()
params.onEdgeServer()
return
}

await Promise.all([params.onClient(), params.onServer()])
params.onClient()
params.onServer()
return
}

Expand Down Expand Up @@ -556,7 +560,7 @@ export async function createEntrypoints(
]
}

await runDependingOnPageType({
runDependingOnPageType({
page,
pageRuntime: staticInfo.runtime,
pageType: pagesType,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export default class HotReloader {
this.hasAppRouterEntrypoints = true
}

await runDependingOnPageType({
runDependingOnPageType({
page,
pageRuntime: staticInfo.runtime,
pageType,
Expand Down

0 comments on commit e50c918

Please sign in to comment.