Unverified Commit ab09c1f4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Change `loadManifest` to return undefined with `handleMissing` (#96530)

Previously, this returned `{} as T` which was unsafe. Now it properly
returns `undefined` instead and the caller can decide what the fallback
value should be
parent bcea67d4
......@@ -34,17 +34,16 @@ export function loadManifest<T extends object>(
cache?: Map<string, unknown>,
skipParse?: boolean,
handleMissing?: boolean
): DeepReadonly<T>
): DeepReadonly<T | undefined>
export function loadManifest<T extends object>(
path: string,
shouldCache: boolean = true,
cache = sharedCache,
skipParse = false,
handleMissing?: boolean
): T {
const cached = shouldCache && cache.get(path)
if (cached) {
return cached as T
): T | undefined {
if (shouldCache && cache.has(path)) {
return cache.get(path) as T
}
let manifest: any
......@@ -53,7 +52,7 @@ export function loadManifest<T extends object>(
try {
manifest = readFileSync(/* turbopackIgnore: true */ path, 'utf8')
} catch (err) {
let result = {} as any
let result = undefined
cache.set(path, result)
return result
}
......@@ -81,26 +80,25 @@ export function evalManifest<T extends object>(
path: string,
shouldCache: false
): T
export function evalManifest<T extends object>(
path: string,
shouldCache?: boolean,
cache?: Map<string, unknown>,
handleMissing?: boolean
): DeepReadonly<T>
export function evalManifest<T extends object>(
path: string,
shouldCache?: true,
cache?: Map<string, unknown>
): DeepReadonly<T>
export function evalManifest<T extends object>(
path: string,
shouldCache?: boolean,
cache?: Map<string, unknown>,
handleMissing?: boolean
): DeepReadonly<T | undefined>
export function evalManifest<T extends object>(
path: string,
shouldCache: boolean = true,
cache = sharedCache,
handleMissing?: boolean
): T {
const cached = shouldCache && cache.get(path)
if (cached) {
return cached as T
): T | undefined {
if (shouldCache && cache.has(path)) {
return cache.get(path) as T
}
let content: any
......@@ -108,7 +106,7 @@ export function evalManifest<T extends object>(
try {
content = readFileSync(/* turbopackIgnore: true */ path, 'utf8')
} catch (err) {
let result = {} as any
let result = undefined
cache.set(path, result)
return result
}
......@@ -137,6 +135,64 @@ export function evalManifest<T extends object>(
return contextObject as T
}
export function evalManifestFromRelativePath<T extends object>({
projectDir,
distDir,
manifest,
shouldCache,
cache,
handleMissing,
}: {
projectDir: string
distDir: string
manifest: string
shouldCache: boolean
cache?: Map<string, unknown>
skipParse?: boolean
handleMissing?: boolean
}): DeepReadonly<T | undefined> {
const manifestPath = join(
/* turbopackIgnore: true */ projectDir,
distDir,
manifest
)
return evalManifest<T>(manifestPath, shouldCache, cache, handleMissing)
}
export function loadManifestFromRelativePath<T extends object>({
projectDir,
distDir,
manifest,
shouldCache,
cache,
skipParse,
handleMissing,
}: {
projectDir: string
distDir: string
manifest: string
shouldCache: boolean
cache?: Map<string, unknown>
skipParse?: boolean
handleMissing?: false | undefined
}): DeepReadonly<T>
export function loadManifestFromRelativePath<T extends object>({
projectDir,
distDir,
manifest,
shouldCache,
cache,
skipParse,
handleMissing,
}: {
projectDir: string
distDir: string
manifest: string
shouldCache: boolean
cache?: Map<string, unknown>
skipParse?: boolean
handleMissing: true
}): DeepReadonly<T | undefined>
export function loadManifestFromRelativePath<T extends object>({
projectDir,
distDir,
......@@ -145,7 +201,6 @@ export function loadManifestFromRelativePath<T extends object>({
cache,
skipParse,
handleMissing,
useEval,
}: {
projectDir: string
distDir: string
......@@ -154,17 +209,12 @@ export function loadManifestFromRelativePath<T extends object>({
cache?: Map<string, unknown>
skipParse?: boolean
handleMissing?: boolean
useEval?: boolean
}): DeepReadonly<T> {
}): DeepReadonly<T | undefined> {
const manifestPath = join(
/* turbopackIgnore: true */ projectDir,
distDir,
manifest
)
if (useEval) {
return evalManifest<T>(manifestPath, shouldCache, cache, handleMissing)
}
return loadManifest<T>(
manifestPath,
shouldCache,
......
......@@ -132,6 +132,7 @@ import {
} from './lib/router-utils/router-server-context'
import { installGlobalBehaviors } from './node-environment-extensions/global-behaviors'
import { installProcessErrorHandlers } from './node-environment-extensions/process-error-handlers'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
export * from './base-server'
......@@ -918,14 +919,14 @@ export default class NextNodeServer extends BaseServer<
return null
}
protected getNextFontManifest(): NextFontManifest | undefined {
return loadManifest(
protected getNextFontManifest(): DeepReadonly<NextFontManifest> | undefined {
return loadManifest<NextFontManifest>(
join(
/* turbopackIgnore: true */ this.distDir,
'server',
NEXT_FONT_MANIFEST + '.json'
)
) as NextFontManifest
)
}
protected handleNextImageRequest: NodeRouteHandler = async (
......@@ -1923,15 +1924,15 @@ export default class NextNodeServer extends BaseServer<
return result.finished
}
private _cachedPreviewManifest: PrerenderManifest | undefined
protected getPrerenderManifest(): PrerenderManifest {
private _cachedPreviewManifest: DeepReadonly<PrerenderManifest> | undefined
protected getPrerenderManifest(): DeepReadonly<PrerenderManifest> {
if (this._cachedPreviewManifest) {
return this._cachedPreviewManifest
}
this._cachedPreviewManifest = loadManifest(
this._cachedPreviewManifest = loadManifest<PrerenderManifest>(
join(/* turbopackIgnore: true */ this.distDir, PRERENDER_MANIFEST)
) as PrerenderManifest
)
return this._cachedPreviewManifest
}
......@@ -1943,7 +1944,7 @@ export default class NextNodeServer extends BaseServer<
}
this._cachedPrefetchHints =
(loadManifest(
loadManifest<Record<string, PrefetchHints>>(
join(
/* turbopackIgnore: true */ this.distDir,
SERVER_DIRECTORY,
......@@ -1953,7 +1954,7 @@ export default class NextNodeServer extends BaseServer<
undefined,
false,
true // handleMissing: don't crash if the file doesn't exist
) as Record<string, PrefetchHints>) ?? {}
) ?? {}
return this._cachedPrefetchHints
}
......
......@@ -278,7 +278,7 @@ export abstract class RouteModule<
if (!projectDir) {
throw new Error('Invariant: projectDir is required for node runtime')
}
const { loadManifestFromRelativePath } =
const { loadManifestFromRelativePath, evalManifestFromRelativePath } =
require('../load-manifest.external') as typeof import('../load-manifest.external')
const normalizedPagePath = normalizePagePath(srcPage)
......@@ -322,14 +322,16 @@ export abstract class RouteModule<
shouldCache: !this.isDev,
}),
srcPage === '/_error'
? loadManifestFromRelativePath<BuildManifest>({
? (loadManifestFromRelativePath<BuildManifest>({
projectDir,
distDir: this.distDir,
manifest: `fallback-${BUILD_MANIFEST}`,
shouldCache: !this.isDev,
handleMissing: true,
})
: ({} as BuildManifest),
// TODO this cast is unsafe
}) ?? ({} as BuildManifest))
: // TODO this cast is unsafe
({} as BuildManifest),
loadManifestFromRelativePath<ReactLoadableManifest>({
projectDir,
distDir: this.distDir,
......@@ -338,7 +340,7 @@ export abstract class RouteModule<
: REACT_LOADABLE_MANIFEST,
handleMissing: true,
shouldCache: !this.isDev,
}),
}) ?? ({} satisfies ReactLoadableManifest),
loadManifestFromRelativePath<NextFontManifest>({
projectDir,
distDir: this.distDir,
......@@ -346,10 +348,9 @@ export abstract class RouteModule<
shouldCache: !this.isDev,
}),
router === 'app' && !isStaticMetadataRoute(srcPage)
? loadManifestFromRelativePath({
? evalManifestFromRelativePath({
distDir: this.distDir,
projectDir,
useEval: true,
handleMissing: true,
manifest: `server/app${srcPage.replace(/%5F/g, '_') + '_' + CLIENT_REFERENCE_MANIFEST}.js`,
shouldCache: !this.isDev,
......
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