Skip to content

Commit

Permalink
Skip building /500 in dev mode (#39828)
Browse files Browse the repository at this point in the history
Follow-up to #39826 this skips ensuring the /500 page when in development as it's not used since we use the error overlay instead. This also fixes the case where we don't clear the stalled timeout debug log when an error is thrown during ensuring.
  • Loading branch information
ijjk committed Aug 22, 2022
1 parent 4955da9 commit d2a2cc7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 99 deletions.
8 changes: 6 additions & 2 deletions packages/next/server/base-server.ts
Expand Up @@ -1705,14 +1705,18 @@ export default abstract class Server<ServerOptions extends Options = Options> {
let using404Page = false

// use static 404 page if available and is 404 response
if (is404) {
if (is404 && (await this.hasPage('/404'))) {
result = await this.findPageComponents('/404', query)
using404Page = result !== null
}
let statusPage = `/${res.statusCode}`

if (!result && STATIC_STATUS_PAGES.includes(statusPage)) {
result = await this.findPageComponents(statusPage, query)
// skip ensuring /500 in dev mode as it isn't used and the
// dev overlay is used instead
if (statusPage !== '/500' || !this.renderOpts.dev) {
result = await this.findPageComponents(statusPage, query)
}
}

if (!result) {
Expand Down
197 changes: 100 additions & 97 deletions packages/next/server/dev/on-demand-entry-handler.ts
Expand Up @@ -524,123 +524,126 @@ export function onDemandEntryHandler({
)
}, stalledTime * 1000)

const pagePathData = await findPagePathData(
rootDir,
pagesDir,
page,
nextConfig.pageExtensions,
appDir
)
try {
const pagePathData = await findPagePathData(
rootDir,
pagesDir,
page,
nextConfig.pageExtensions,
appDir
)

const isServerComponent = serverComponentRegex.test(
pagePathData.absolutePagePath
)
const isInsideAppDir =
appDir && pagePathData.absolutePagePath.startsWith(appDir)

const addEntry = (
compilerType: CompilerNameValues
): {
entryKey: string
newEntry: boolean
shouldInvalidate: boolean
} => {
const entryKey = `${compilerType}${pagePathData.page}`

if (entries[entryKey]) {
entries[entryKey].dispose = false
entries[entryKey].lastActiveTime = Date.now()
if (entries[entryKey].status === BUILT) {
return {
entryKey,
newEntry: false,
shouldInvalidate: false,
}
}

const isServerComponent = serverComponentRegex.test(
pagePathData.absolutePagePath
)
const isInsideAppDir =
appDir && pagePathData.absolutePagePath.startsWith(appDir)

const addEntry = (
compilerType: CompilerNameValues
): {
entryKey: string
newEntry: boolean
shouldInvalidate: boolean
} => {
const entryKey = `${compilerType}${pagePathData.page}`

if (entries[entryKey]) {
entries[entryKey].dispose = false
entries[entryKey].lastActiveTime = Date.now()
if (entries[entryKey].status === BUILT) {
return {
entryKey,
newEntry: false,
shouldInvalidate: false,
shouldInvalidate: true,
}
}

entries[entryKey] = {
type: EntryTypes.ENTRY,
absolutePagePath: pagePathData.absolutePagePath,
request: pagePathData.absolutePagePath,
bundlePath: pagePathData.bundlePath,
dispose: false,
lastActiveTime: Date.now(),
status: ADDED,
}

return {
entryKey,
newEntry: false,
entryKey: entryKey,
newEntry: true,
shouldInvalidate: true,
}
}

entries[entryKey] = {
type: EntryTypes.ENTRY,
absolutePagePath: pagePathData.absolutePagePath,
request: pagePathData.absolutePagePath,
bundlePath: pagePathData.bundlePath,
dispose: false,
lastActiveTime: Date.now(),
status: ADDED,
}
const staticInfo = await getPageStaticInfo({
pageFilePath: pagePathData.absolutePagePath,
nextConfig,
})

return {
entryKey: entryKey,
newEntry: true,
shouldInvalidate: true,
}
}
const added = new Map<CompilerNameValues, ReturnType<typeof addEntry>>()

const staticInfo = await getPageStaticInfo({
pageFilePath: pagePathData.absolutePagePath,
nextConfig,
})
await runDependingOnPageType({
page: pagePathData.page,
pageRuntime: staticInfo.runtime,
onClient: () => {
// Skip adding the client entry for app / Server Components.
if (isServerComponent || isInsideAppDir) {
return
}
added.set(COMPILER_NAMES.client, addEntry(COMPILER_NAMES.client))
},
onServer: () => {
added.set(COMPILER_NAMES.server, addEntry(COMPILER_NAMES.server))
},
onEdgeServer: () => {
added.set(
COMPILER_NAMES.edgeServer,
addEntry(COMPILER_NAMES.edgeServer)
)
},
})

const added = new Map<CompilerNameValues, ReturnType<typeof addEntry>>()
const addedValues = [...added.values()]
const entriesThatShouldBeInvalidated = addedValues.filter(
(entry) => entry.shouldInvalidate
)
const hasNewEntry = addedValues.some((entry) => entry.newEntry)

await runDependingOnPageType({
page: pagePathData.page,
pageRuntime: staticInfo.runtime,
onClient: () => {
// Skip adding the client entry for app / Server Components.
if (isServerComponent || isInsideAppDir) {
return
}
added.set(COMPILER_NAMES.client, addEntry(COMPILER_NAMES.client))
},
onServer: () => {
added.set(COMPILER_NAMES.server, addEntry(COMPILER_NAMES.server))
},
onEdgeServer: () => {
added.set(
COMPILER_NAMES.edgeServer,
addEntry(COMPILER_NAMES.edgeServer)
if (hasNewEntry) {
reportTrigger(
!clientOnly && hasNewEntry
? `${pagePathData.page} (client and server)`
: pagePathData.page
)
},
})

const addedValues = [...added.values()]
const entriesThatShouldBeInvalidated = addedValues.filter(
(entry) => entry.shouldInvalidate
)
const hasNewEntry = addedValues.some((entry) => entry.newEntry)

if (hasNewEntry) {
reportTrigger(
!clientOnly && hasNewEntry
? `${pagePathData.page} (client and server)`
: pagePathData.page
)
}
}

if (entriesThatShouldBeInvalidated.length > 0) {
const invalidatePromises = entriesThatShouldBeInvalidated.map(
({ entryKey }) => {
return new Promise<void>((resolve, reject) => {
doneCallbacks!.once(entryKey, (err: Error) => {
if (err) {
return reject(err)
}
resolve()
if (entriesThatShouldBeInvalidated.length > 0) {
const invalidatePromises = entriesThatShouldBeInvalidated.map(
({ entryKey }) => {
return new Promise<void>((resolve, reject) => {
doneCallbacks!.once(entryKey, (err: Error) => {
if (err) {
return reject(err)
}
resolve()
})
})
})
}
)
invalidator.invalidate([...added.keys()])
await Promise.all(invalidatePromises)
}
)
invalidator.invalidate([...added.keys()])
await Promise.all(invalidatePromises)
}
} finally {
clearTimeout(stalledEnsureTimeout)
}
clearTimeout(stalledEnsureTimeout)
},

onHMR(client: ws) {
Expand Down

0 comments on commit d2a2cc7

Please sign in to comment.