Unverified Commit 56c324fb authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Simplified ensure promise handling (#55562)

In JS, Promise's are used to help manage async tasks and control flows. When code calls methods on a promise like `.then()`, `.catch()`, or `.finally()` the results of the promise are forwarded to the callback as soon as they're resolved. This serves to make a change to the promise creation such that we do not await on the promise until we're within the try/finally block. This will ensure that the promise will always be added to the map before it's resolved or rejected and it's cleanup (removing it from the active promises) is also completed.

This additionally introduces a new `scheduleOnNextTick` method and polyfill for `Promise.withResolvers()`.

`scheduleOnNextTick` is based on the scheduling algorithm used by https://github.com/graphql/dataloader which utilizes a `Promise.resolve()` combined with `process.nextTick` in order to schedule an operation to occur after the promises have resolved (see [graphql/dataloader](https://github.com/graphql/dataloader/blob/d336bd15282664e0be4b4a657cb796f09bafbc6b/src/index.js#L213-L255))

The `Promise.withResolvers()` polyfill is an implementation of a soon-to-be-landed spec for inside-out promises. [Read the spec](https://tc39.es/proposal-promise-with-resolvers/)
parent 0fea4465
......@@ -1485,18 +1485,15 @@ export default class HotReloader implements NextJsHotReloaderInterface {
? this.clientError
: this.serverError || this.clientError
if (error) {
return Promise.reject(error)
throw error
}
const res = (await this.onDemandEntries?.ensurePage({
return this.onDemandEntries?.ensurePage({
page,
clientOnly,
appPaths,
match,
isApp,
})) as any
if (res && res.err) {
throw res.err
}
})
}
}
......@@ -34,9 +34,11 @@ import {
RSC_MODULE_TYPES,
} from '../../shared/lib/constants'
import { RouteMatch } from '../future/route-matches/route-match'
import { isAppPageRouteMatch } from '../future/route-matches/app-page-route-match'
import { HMR_ACTIONS_SENT_TO_BROWSER } from './hot-reloader-types'
import HotReloader from './hot-reloader-webpack'
import { isAppPageRouteDefinition } from '../future/route-definitions/app-page-route-definition'
import { scheduleOnNextTick } from '../lib/schedule-on-next-tick'
import { RouteDefinition } from '../future/route-definitions/route-definition'
const debug = origDebug('next:on-demand-entry-handler')
......@@ -359,6 +361,12 @@ function tryToNormalizePagePath(page: string) {
}
}
interface PagePathData {
filename: string
bundlePath: string
page: string
}
/**
* Attempts to find a page file path from the given pages absolute directory,
* a page and allowed extensions. If the page can't be found it will throw an
......@@ -375,7 +383,7 @@ async function findPagePathData(
extensions: string[],
pagesDir?: string,
appDir?: string
) {
): Promise<PagePathData> {
const normalizedPagePath = tryToNormalizePagePath(page)
let pagePath: string | null = null
......@@ -407,7 +415,7 @@ async function findPagePathData(
}
return {
absolutePagePath: join(rootDir, pagePath),
filename: join(rootDir, pagePath),
bundlePath: bundlePath.slice(1),
page: pageKey,
}
......@@ -425,7 +433,7 @@ async function findPagePathData(
)
return {
absolutePagePath: join(appDir, pagePath),
filename: join(appDir, pagePath),
bundlePath: posix.join('app', pageUrl),
page: posix.normalize(pageUrl),
}
......@@ -449,7 +457,7 @@ async function findPagePathData(
)
return {
absolutePagePath: join(pagesDir, pagePath),
filename: join(pagesDir, pagePath),
bundlePath: posix.join('pages', normalizePagePath(pageUrl)),
page: posix.normalize(pageUrl),
}
......@@ -457,9 +465,7 @@ async function findPagePathData(
if (page === '/not-found' && appDir) {
return {
absolutePagePath: require.resolve(
'next/dist/client/components/not-found-error'
),
filename: require.resolve('next/dist/client/components/not-found-error'),
bundlePath: 'app/not-found',
page: '/not-found',
}
......@@ -467,7 +473,7 @@ async function findPagePathData(
if (page === '/_error') {
return {
absolutePagePath: require.resolve('next/dist/pages/_error'),
filename: require.resolve('next/dist/pages/_error'),
bundlePath: page,
page: normalizePathSep(page),
}
......@@ -476,27 +482,6 @@ async function findPagePathData(
}
}
async function findRoutePathData(
rootDir: string,
page: string,
extensions: string[],
pagesDir?: string,
appDir?: string,
match?: RouteMatch
): ReturnType<typeof findPagePathData> {
if (match) {
// If the match is available, we don't have to discover the data from the
// filesystem.
return {
absolutePagePath: match.definition.filename,
page: match.definition.page,
bundlePath: match.definition.bundlePath,
}
}
return findPagePathData(rootDir, page, extensions, pagesDir, appDir)
}
export function onDemandEntryHandler({
hotReloader,
maxInactiveAge,
......@@ -690,15 +675,15 @@ export function onDemandEntryHandler({
async function ensurePageImpl({
page,
clientOnly,
appPaths = null,
appPaths,
match,
isApp,
}: {
page: string
clientOnly: boolean
appPaths?: ReadonlyArray<string> | null
match?: RouteMatch
isApp?: boolean
appPaths: ReadonlyArray<string> | null
match: Pick<RouteMatch, 'definition'> | undefined
isApp: boolean | undefined
}): Promise<void> {
const stalledTime = 60
const stalledEnsureTimeout = setTimeout(() => {
......@@ -707,35 +692,32 @@ export function onDemandEntryHandler({
)
}, stalledTime * 1000)
// If the route is actually an app page route, then we should have access
// to the app route match, and therefore, the appPaths from it.
if (!appPaths && match && isAppPageRouteMatch(match)) {
appPaths = match.definition.appPaths
}
try {
const pagePathData = await findRoutePathData(
let definition: Pick<RouteDefinition, 'filename' | 'bundlePath' | 'page'>
if (match?.definition) {
definition = match.definition
} else {
definition = await findPagePathData(
rootDir,
page,
nextConfig.pageExtensions,
pagesDir,
appDir,
match
appDir
)
}
const isInsideAppDir =
!!appDir && pagePathData.absolutePagePath.startsWith(appDir)
const isInsideAppDir = !!appDir && definition.filename.startsWith(appDir)
if (typeof isApp === 'boolean' && isApp !== isInsideAppDir) {
Error.stackTraceLimit = 15
throw new Error(
`Ensure bailed, found path "${
pagePathData.page
definition.page
}" does not match ensure type (${isApp ? 'app' : 'pages'})`
)
}
const pageBundleType = getPageBundleType(pagePathData.bundlePath)
const pageBundleType = getPageBundleType(definition.bundlePath)
const addEntry = (
compilerType: CompilerNameValues
): {
......@@ -746,7 +728,7 @@ export function onDemandEntryHandler({
const entryKey = getEntryKey(
compilerType,
pageBundleType,
pagePathData.page
definition.page
)
if (
curEntries[entryKey] &&
......@@ -775,9 +757,9 @@ export function onDemandEntryHandler({
curEntries[entryKey] = {
type: EntryTypes.ENTRY,
appPaths,
absolutePagePath: pagePathData.absolutePagePath,
request: pagePathData.absolutePagePath,
bundlePath: pagePathData.bundlePath,
absolutePagePath: definition.filename,
request: definition.filename,
bundlePath: definition.bundlePath,
dispose: false,
lastActiveTime: Date.now(),
status: ADDED,
......@@ -791,7 +773,7 @@ export function onDemandEntryHandler({
const staticInfo = await getStaticInfoIncludingLayouts({
page,
pageFilePath: pagePathData.absolutePagePath,
pageFilePath: definition.filename,
isInsideAppDir,
pageExtensions: nextConfig.pageExtensions,
isDev: true,
......@@ -804,7 +786,7 @@ export function onDemandEntryHandler({
isInsideAppDir && staticInfo.rsc !== RSC_MODULE_TYPES.client
runDependingOnPageType({
page: pagePathData.page,
page: definition.page,
pageRuntime: staticInfo.runtime,
pageType: pageBundleType,
onClient: () => {
......@@ -819,11 +801,11 @@ export function onDemandEntryHandler({
const edgeServerEntry = getEntryKey(
COMPILER_NAMES.edgeServer,
pageBundleType,
pagePathData.page
definition.page
)
if (
curEntries[edgeServerEntry] &&
!isInstrumentationHookFile(pagePathData.page)
!isInstrumentationHookFile(definition.page)
) {
// Runtime switched from edge to server
delete curEntries[edgeServerEntry]
......@@ -837,11 +819,11 @@ export function onDemandEntryHandler({
const serverEntry = getEntryKey(
COMPILER_NAMES.server,
pageBundleType,
pagePathData.page
definition.page
)
if (
curEntries[serverEntry] &&
!isInstrumentationHookFile(pagePathData.page)
!isInstrumentationHookFile(definition.page)
) {
// Runtime switched from server to edge
delete curEntries[serverEntry]
......@@ -857,9 +839,7 @@ export function onDemandEntryHandler({
if (hasNewEntry) {
reportTrigger(
!clientOnly && hasNewEntry
? `${pagePathData.page}`
: pagePathData.page
!clientOnly && hasNewEntry ? `${definition.page}` : definition.page
)
}
......@@ -899,7 +879,7 @@ export function onDemandEntryHandler({
}
// Make sure that we won't have multiple invalidations ongoing concurrently.
const curEnsurePage = new Map<string, Promise<void | { err: Error }>>()
const curEnsurePage = new Map<string, Promise<void>>()
return {
async ensurePage({
......@@ -915,34 +895,52 @@ export function onDemandEntryHandler({
match?: RouteMatch
isApp?: boolean
}) {
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
}
// If the route is actually an app page route, then we should have access
// to the app route match, and therefore, the appPaths from it.
if (
!appPaths &&
match?.definition &&
isAppPageRouteDefinition(match.definition)
) {
appPaths = match.definition.appPaths
}
const promise = ensurePageImpl({
// The cache key here is composed of the elements that affect the
// compilation, namely, the page, whether it's client only, and whether
// it's an app page. This ensures that we don't have multiple compilations
// for the same page happening concurrently.
//
// We don't include the whole match because it contains match specific
// parameters (like route params) that would just bust this cache. Any
// details that would possibly bust the cache should be listed here.
const key = JSON.stringify({
page,
clientOnly,
appPaths,
match,
definition: match?.definition,
isApp,
// we have to catch the error since we are memoizing
// otherwise we can hit random unhandledRejections
})
.catch((err) => {
return { err }
})
.finally(() => {
curEnsurePage.delete(ensureKey)
// See if we're already building this page.
const pending = curEnsurePage.get(key)
if (pending) return pending
const { promise, resolve, reject } = Promise.withResolvers<void>()
curEnsurePage.set(key, promise)
// Schedule the build to occur on the next tick, but don't wait and
// instead return the promise immediately.
scheduleOnNextTick(async () => {
try {
await ensurePageImpl({ page, clientOnly, appPaths, match, isApp })
resolve()
} catch (err) {
reject(err)
} finally {
curEnsurePage.delete(key)
}
})
curEnsurePage.set(ensureKey, promise)
return promise
},
onHMR(client: ws, getHmrServerError: () => Error | null) {
......
......@@ -5,3 +5,12 @@ export interface AppPageRouteDefinition
extends RouteDefinition<RouteKind.APP_PAGE> {
readonly appPaths: ReadonlyArray<string>
}
/**
* Returns true if the given definition is an App Page route definition.
*/
export function isAppPageRouteDefinition(
definition: RouteDefinition
): definition is AppPageRouteDefinition {
return definition.kind === RouteKind.APP_PAGE
}
import type { RouteMatch } from './route-match'
import type { AppPageRouteDefinition } from '../route-definitions/app-page-route-definition'
import { RouteKind } from '../route-kind'
export interface AppPageRouteMatch extends RouteMatch<AppPageRouteDefinition> {}
/**
* Checks if the given match is an App Page route match.
* @param match the match to check
* @returns true if the match is an App Page route match, false otherwise
*/
export function isAppPageRouteMatch(
match: RouteMatch
): match is AppPageRouteMatch {
return match.definition.kind === RouteKind.APP_PAGE
}
type ScheduledFn<T = void> = () => T | PromiseLike<T>
/**
* Schedules a function to be called on the next tick after the other promises
* have been resolved.
*/
export function scheduleOnNextTick<T = void>(cb: ScheduledFn<T>): void {
// We use Promise.resolve().then() here so that the operation is scheduled at
// the end of the promise job queue, we then add it to the next process tick
// to ensure it's evaluated afterwards.
//
// This was inspired by the implementation of the DataLoader interface: https://github.com/graphql/dataloader/blob/d336bd15282664e0be4b4a657cb796f09bafbc6b/src/index.js#L213-L255
//
Promise.resolve().then(() => {
process.nextTick(cb)
})
}
......@@ -14,3 +14,31 @@ if (typeof (globalThis as any).WebSocket !== 'function') {
},
})
}
// This adds a `Promise.withResolvers` polyfill. This will soon be adopted into
// the spec.
//
// TODO: remove this polyfill when it is adopted into the spec.
//
// https://tc39.es/proposal-promise-with-resolvers/
//
if (
!('withResolvers' in Promise) ||
typeof Promise.withResolvers !== 'function'
) {
Promise.withResolvers = <T>() => {
let resolvers: {
resolve: (value: T | PromiseLike<T>) => void
reject: (reason: any) => void
}
// Create the promise and assign the resolvers to the object.
const promise = new Promise<T>((resolve, reject) => {
resolvers = { resolve, reject }
})
// We know that resolvers is defined because the Promise constructor runs
// synchronously.
return { promise, resolve: resolvers!.resolve, reject: resolvers!.reject }
}
}
......@@ -318,6 +318,21 @@ declare global {
randomUUID(): string
}
// TODO: remove this polyfill when it is adopted into the spec.
interface PromiseConstructor {
/**
* Creates a new promise with exposed resolvers to resolve/reject. This will
* be adopted into the spec as `Promise.withResolvers`.
*
* @see https://tc39.es/proposal-promise-with-resolvers/
*/
withResolvers<T>(): {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: unknown) => void
}
}
var __NEXT_HTTP_AGENT_OPTIONS: { keepAlive?: boolean } | undefined
var __NEXT_UNDICI_AGENT_SET: boolean
var __NEXT_HTTP_AGENT: HttpAgent
......
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