Skip to content

Commit 35c7738

Browse files
committed
Generate required-server-files before prerendering
1 parent 551b32d commit 35c7738

File tree

1 file changed

+171
-169
lines changed

1 file changed

+171
-169
lines changed

packages/next/src/build/index.ts

Lines changed: 171 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,177 @@ export default async function build(
18271827
traceMemoryUsage('Finished type checking', nextBuildSpan)
18281828
}
18291829

1830+
const requiredServerFilesManifest = await nextBuildSpan
1831+
.traceChild('generate-required-server-files')
1832+
.traceAsyncFn(async () => {
1833+
let runtimeConfig = getNextConfigRuntime(config)
1834+
1835+
const normalizedCacheHandlers: Record<string, string> = {}
1836+
for (const [key, value] of Object.entries(
1837+
runtimeConfig.cacheHandlers || {}
1838+
)) {
1839+
if (key && value) {
1840+
normalizedCacheHandlers[key] = path.relative(distDir, value)
1841+
}
1842+
}
1843+
1844+
const serverFilesManifest: RequiredServerFilesManifest = {
1845+
version: 1,
1846+
config: {
1847+
...runtimeConfig,
1848+
1849+
...(ciEnvironment.hasNextSupport
1850+
? {
1851+
compress: false,
1852+
}
1853+
: {}),
1854+
cacheHandler: runtimeConfig.cacheHandler
1855+
? path.relative(distDir, runtimeConfig.cacheHandler)
1856+
: runtimeConfig.cacheHandler,
1857+
cacheHandlers: normalizedCacheHandlers,
1858+
experimental: {
1859+
...runtimeConfig.experimental,
1860+
trustHostHeader: ciEnvironment.hasNextSupport,
1861+
isExperimentalCompile: isCompileMode,
1862+
},
1863+
},
1864+
appDir: dir,
1865+
relativeAppDir: path.relative(outputFileTracingRoot, dir),
1866+
files: [
1867+
ROUTES_MANIFEST,
1868+
path.relative(distDir, pagesManifestPath),
1869+
BUILD_MANIFEST,
1870+
PRERENDER_MANIFEST,
1871+
path.join(SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST),
1872+
path.join(SERVER_DIRECTORY, MIDDLEWARE_MANIFEST),
1873+
path.join(SERVER_DIRECTORY, MIDDLEWARE_BUILD_MANIFEST + '.js'),
1874+
...(bundler !== Bundler.Turbopack
1875+
? [
1876+
path.join(
1877+
SERVER_DIRECTORY,
1878+
MIDDLEWARE_REACT_LOADABLE_MANIFEST + '.js'
1879+
),
1880+
REACT_LOADABLE_MANIFEST,
1881+
]
1882+
: []),
1883+
...(appDir
1884+
? [
1885+
...(config.experimental.sri
1886+
? [
1887+
path.join(
1888+
SERVER_DIRECTORY,
1889+
SUBRESOURCE_INTEGRITY_MANIFEST + '.js'
1890+
),
1891+
path.join(
1892+
SERVER_DIRECTORY,
1893+
SUBRESOURCE_INTEGRITY_MANIFEST + '.json'
1894+
),
1895+
]
1896+
: []),
1897+
path.join(SERVER_DIRECTORY, APP_PATHS_MANIFEST),
1898+
path.join(APP_PATH_ROUTES_MANIFEST),
1899+
path.join(
1900+
SERVER_DIRECTORY,
1901+
SERVER_REFERENCE_MANIFEST + '.js'
1902+
),
1903+
path.join(
1904+
SERVER_DIRECTORY,
1905+
SERVER_REFERENCE_MANIFEST + '.json'
1906+
),
1907+
]
1908+
: []),
1909+
...(pagesDir && bundler !== Bundler.Turbopack
1910+
? [
1911+
DYNAMIC_CSS_MANIFEST + '.json',
1912+
path.join(SERVER_DIRECTORY, DYNAMIC_CSS_MANIFEST + '.js'),
1913+
]
1914+
: []),
1915+
BUILD_ID_FILE,
1916+
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.js'),
1917+
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.json'),
1918+
SERVER_FILES_MANIFEST,
1919+
]
1920+
.filter(nonNullable)
1921+
.map((file) => path.join(config.distDir, file)),
1922+
ignore: [] as string[],
1923+
}
1924+
1925+
if (hasInstrumentationHook) {
1926+
serverFilesManifest.files.push(
1927+
path.join(SERVER_DIRECTORY, `${INSTRUMENTATION_HOOK_FILENAME}.js`)
1928+
)
1929+
// If there are edge routes, append the edge instrumentation hook
1930+
// Turbopack generates this chunk with a hashed name and references it in middleware-manifest.
1931+
let edgeInstrumentationHook = path.join(
1932+
SERVER_DIRECTORY,
1933+
`edge-${INSTRUMENTATION_HOOK_FILENAME}.js`
1934+
)
1935+
if (
1936+
bundler !== Bundler.Turbopack &&
1937+
existsSync(path.join(distDir, edgeInstrumentationHook))
1938+
) {
1939+
serverFilesManifest.files.push(edgeInstrumentationHook)
1940+
}
1941+
}
1942+
1943+
if (config.experimental.optimizeCss) {
1944+
const globOrig =
1945+
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob')
1946+
1947+
const cssFilePaths = await new Promise<string[]>(
1948+
(resolve, reject) => {
1949+
globOrig(
1950+
'**/*.css',
1951+
{ cwd: path.join(distDir, 'static') },
1952+
(err, files) => {
1953+
if (err) {
1954+
return reject(err)
1955+
}
1956+
resolve(files)
1957+
}
1958+
)
1959+
}
1960+
)
1961+
1962+
serverFilesManifest.files.push(
1963+
...cssFilePaths.map((filePath) =>
1964+
path.join(config.distDir, 'static', filePath)
1965+
)
1966+
)
1967+
}
1968+
1969+
// Under standalone mode, we need to ensure that the cache entry debug
1970+
// handler is copied so that it can be used in the test. This is required
1971+
// for the turbopack test to run as it's more strict about the build
1972+
// directories. This is only used for testing and is not used in
1973+
// production.
1974+
if (
1975+
process.env.__NEXT_TEST_MODE &&
1976+
process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
1977+
) {
1978+
serverFilesManifest.files.push(
1979+
path.relative(
1980+
dir,
1981+
path.isAbsolute(
1982+
process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
1983+
)
1984+
? process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
1985+
: path.join(
1986+
dir,
1987+
process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
1988+
)
1989+
)
1990+
)
1991+
}
1992+
1993+
return serverFilesManifest
1994+
})
1995+
1996+
await writeRequiredServerFilesManifest(
1997+
distDir,
1998+
requiredServerFilesManifest
1999+
)
2000+
18302001
const numberOfWorkers = getNumberOfWorkers(config)
18312002
const collectingPageDataStart = process.hrtime()
18322003
const postCompileSpinner = createSpinner(
@@ -2416,124 +2587,6 @@ export default async function build(
24162587
)
24172588
}
24182589

2419-
const instrumentationHookEntryFiles: string[] = []
2420-
if (hasInstrumentationHook) {
2421-
instrumentationHookEntryFiles.push(
2422-
path.join(SERVER_DIRECTORY, `${INSTRUMENTATION_HOOK_FILENAME}.js`)
2423-
)
2424-
// If there's edge routes, append the edge instrumentation hook
2425-
// Turbopack generates this chunk with a hashed name and references it in middleware-manifest.
2426-
if (
2427-
bundler !== Bundler.Turbopack &&
2428-
(edgeRuntimeAppCount || edgeRuntimePagesCount)
2429-
) {
2430-
instrumentationHookEntryFiles.push(
2431-
path.join(
2432-
SERVER_DIRECTORY,
2433-
`edge-${INSTRUMENTATION_HOOK_FILENAME}.js`
2434-
)
2435-
)
2436-
}
2437-
}
2438-
2439-
const requiredServerFilesManifest = nextBuildSpan
2440-
.traceChild('generate-required-server-files')
2441-
.traceFn(() => {
2442-
let runtimeConfig = getNextConfigRuntime(config)
2443-
2444-
const normalizedCacheHandlers: Record<string, string> = {}
2445-
for (const [key, value] of Object.entries(
2446-
runtimeConfig.cacheHandlers || {}
2447-
)) {
2448-
if (key && value) {
2449-
normalizedCacheHandlers[key] = path.relative(distDir, value)
2450-
}
2451-
}
2452-
2453-
const serverFilesManifest: RequiredServerFilesManifest = {
2454-
version: 1,
2455-
config: {
2456-
...runtimeConfig,
2457-
...(ciEnvironment.hasNextSupport
2458-
? {
2459-
compress: false,
2460-
}
2461-
: {}),
2462-
cacheHandler: runtimeConfig.cacheHandler
2463-
? path.relative(distDir, runtimeConfig.cacheHandler)
2464-
: runtimeConfig.cacheHandler,
2465-
cacheHandlers: normalizedCacheHandlers,
2466-
experimental: {
2467-
...runtimeConfig.experimental,
2468-
trustHostHeader: ciEnvironment.hasNextSupport,
2469-
isExperimentalCompile: isCompileMode,
2470-
},
2471-
},
2472-
appDir: dir,
2473-
relativeAppDir: path.relative(outputFileTracingRoot, dir),
2474-
files: [
2475-
ROUTES_MANIFEST,
2476-
path.relative(distDir, pagesManifestPath),
2477-
BUILD_MANIFEST,
2478-
PRERENDER_MANIFEST,
2479-
path.join(SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST),
2480-
path.join(SERVER_DIRECTORY, MIDDLEWARE_MANIFEST),
2481-
path.join(SERVER_DIRECTORY, MIDDLEWARE_BUILD_MANIFEST + '.js'),
2482-
...(bundler !== Bundler.Turbopack
2483-
? [
2484-
path.join(
2485-
SERVER_DIRECTORY,
2486-
MIDDLEWARE_REACT_LOADABLE_MANIFEST + '.js'
2487-
),
2488-
REACT_LOADABLE_MANIFEST,
2489-
]
2490-
: []),
2491-
...(appDir
2492-
? [
2493-
...(config.experimental.sri
2494-
? [
2495-
path.join(
2496-
SERVER_DIRECTORY,
2497-
SUBRESOURCE_INTEGRITY_MANIFEST + '.js'
2498-
),
2499-
path.join(
2500-
SERVER_DIRECTORY,
2501-
SUBRESOURCE_INTEGRITY_MANIFEST + '.json'
2502-
),
2503-
]
2504-
: []),
2505-
path.join(SERVER_DIRECTORY, APP_PATHS_MANIFEST),
2506-
path.join(APP_PATH_ROUTES_MANIFEST),
2507-
path.join(
2508-
SERVER_DIRECTORY,
2509-
SERVER_REFERENCE_MANIFEST + '.js'
2510-
),
2511-
path.join(
2512-
SERVER_DIRECTORY,
2513-
SERVER_REFERENCE_MANIFEST + '.json'
2514-
),
2515-
]
2516-
: []),
2517-
...(pagesDir && bundler !== Bundler.Turbopack
2518-
? [
2519-
DYNAMIC_CSS_MANIFEST + '.json',
2520-
path.join(SERVER_DIRECTORY, DYNAMIC_CSS_MANIFEST + '.js'),
2521-
]
2522-
: []),
2523-
BUILD_ID_FILE,
2524-
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.js'),
2525-
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.json'),
2526-
SERVER_FILES_MANIFEST,
2527-
...instrumentationHookEntryFiles,
2528-
]
2529-
.filter(nonNullable)
2530-
.map((file) => path.join(config.distDir, file)),
2531-
ignore: [] as string[],
2532-
}
2533-
2534-
return serverFilesManifest
2535-
})
2536-
25372590
const middlewareFile = normalizePathSep(
25382591
proxyFilePath || middlewareFilePath || ''
25392592
)
@@ -2648,52 +2701,6 @@ export default async function build(
26482701

26492702
await writeBuildId(distDir, buildId)
26502703

2651-
if (config.experimental.optimizeCss) {
2652-
const globOrig =
2653-
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob')
2654-
2655-
const cssFilePaths = await new Promise<string[]>((resolve, reject) => {
2656-
globOrig(
2657-
'**/*.css',
2658-
{ cwd: path.join(distDir, 'static') },
2659-
(err, files) => {
2660-
if (err) {
2661-
return reject(err)
2662-
}
2663-
resolve(files)
2664-
}
2665-
)
2666-
})
2667-
2668-
requiredServerFilesManifest.files.push(
2669-
...cssFilePaths.map((filePath) =>
2670-
path.join(config.distDir, 'static', filePath)
2671-
)
2672-
)
2673-
}
2674-
2675-
// Under standalone mode, we need to ensure that the cache entry debug
2676-
// handler is copied so that it can be used in the test. This is required
2677-
// for the turbopack test to run as it's more strict about the build
2678-
// directories. This is only used for testing and is not used in
2679-
// production.
2680-
if (
2681-
process.env.__NEXT_TEST_MODE &&
2682-
process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
2683-
) {
2684-
requiredServerFilesManifest.files.push(
2685-
path.relative(
2686-
dir,
2687-
path.isAbsolute(process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS)
2688-
? process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
2689-
: path.join(
2690-
dir,
2691-
process.env.NEXT_PRIVATE_DEBUG_CACHE_ENTRY_HANDLERS
2692-
)
2693-
)
2694-
)
2695-
}
2696-
26972704
const features: EventBuildFeatureUsage[] = [
26982705
{
26992706
featureName: 'experimental/cacheComponents',
@@ -2729,11 +2736,6 @@ export default async function build(
27292736
})
27302737
)
27312738

2732-
await writeRequiredServerFilesManifest(
2733-
distDir,
2734-
requiredServerFilesManifest
2735-
)
2736-
27372739
// we don't need to inline for turbopack build as
27382740
// it will handle it's own caching separate of compile
27392741
if (isGenerateMode && bundler !== Bundler.Turbopack) {

0 commit comments

Comments
 (0)