Commit c15cba83 authored by Luke Sandberg's avatar Luke Sandberg

nonsense

parent 5703cb9e
......@@ -240,11 +240,8 @@ impl ClientReferenceManifest {
.await?;
let chunk_paths = ssr_chunks_paths
.filter_map(|(_, chunk_path)| {
node_root_ref
.get_path_to(&chunk_path)
.map(ToString::to_string)
node_root_ref.get_path_to(&chunk_path).map(RcStr::from)
})
.map(RcStr::from)
.collect::<Vec<_>>();
let is_async = async_modules.contains(&ResolvedVc::upcast(ssr_module));
......
......@@ -808,5 +808,6 @@
"807": "Expected a %s response header.",
"808": "Invalid binary HMR message: insufficient data (expected %s bytes, got %s)",
"809": "Invalid binary HMR message of type %s",
"810": "React debug channel stream error"
"810": "React debug channel stream error",
"811": "Unexpected content in client-reference-manifest file %s, cannot extract manifest"
}
......@@ -3,11 +3,39 @@ import { join } from 'node:path'
import { CLIENT_REFERENCE_MANIFEST } from '../shared/lib/constants'
import type { ClientReferenceManifest } from './webpack/plugins/flight-manifest-plugin'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
export interface AppBuildManifestFromClientReferences {
pages: Record<string, string[]>
}
// Cache the computed manifest to avoid re-scanning filesystem
let cachedManifest: AppBuildManifestFromClientReferences | undefined
let cachedDistPath: string | undefined
/**
* Normalizes chunk paths to handle differences between webpack and turbopack formats
* Turbopack includes a configurable asset prefix (ending with /_next/) while webpack uses relative paths
*/
function normalizeChunkPath(chunkPath: string): string {
// Strip asset prefix if present (turbopack format)
// The asset prefix is configurable but always ends with /_next/
let normalized = chunkPath
const nextIndex = normalized.indexOf('/_next/')
if (nextIndex !== -1) {
normalized = normalized.slice(nextIndex + '/_next/'.length)
}
// Handle URL decoding for any escaped characters
try {
normalized = decodeURIComponent(normalized)
} catch {
// If decoding fails, use the original normalized path
}
return normalized
}
/**
* Extracts route-based manifest data from client reference manifests
* This provides a more accurate representation of client-side bundle requirements
......@@ -16,14 +44,26 @@ export interface AppBuildManifestFromClientReferences {
export function computeAppBuildManifestFromClientReferences(
distPath: string
): AppBuildManifestFromClientReferences {
// Return cached result if available for the same distPath
if (cachedManifest && cachedDistPath === distPath) {
return cachedManifest
}
const serverAppPath = join(distPath, 'server', 'app')
if (!existsSync(serverAppPath)) {
return { pages: {} }
const emptyManifest = { pages: {} }
cachedManifest = emptyManifest
cachedDistPath = distPath
return emptyManifest
}
const manifest: AppBuildManifestFromClientReferences = { pages: {} }
const allFiles = new Set<string>()
console.log(
'[client-ref-manifest] Searching for manifests in:',
serverAppPath
)
// Recursively find all client reference manifest files
function findClientReferenceManifests(
......@@ -49,6 +89,11 @@ export function computeAppBuildManifestFromClientReferences(
const manifestFiles = findClientReferenceManifests(serverAppPath)
console.log(
'[client-ref-manifest] Found manifest files:',
manifestFiles.length
)
for (const manifestFile of manifestFiles) {
try {
// Read and parse the client reference manifest
......@@ -58,11 +103,16 @@ export function computeAppBuildManifestFromClientReferences(
const routeMatch = content.match(
/globalThis\.__RSC_MANIFEST\["([^"]+)"\]\s*=\s*({.*})/
)
if (!routeMatch) continue
if (!routeMatch)
throw new Error(
`Unexpected content in client-reference-manifest file ${manifestFile}, cannot extract manifest`
)
const route = routeMatch[1]
const manifestDataStr = routeMatch[2]
console.log('[client-ref-manifest] Processing route:', route)
// Parse the manifest data
const manifestData: ClientReferenceManifest = JSON.parse(manifestDataStr)
......@@ -73,8 +123,7 @@ export function computeAppBuildManifestFromClientReferences(
// This is the key data - what client-side JS actually needs to be loaded
for (const moduleInfo of Object.values(manifestData.clientModules)) {
for (const chunk of moduleInfo.chunks) {
chunks.add(chunk)
allFiles.add(chunk)
chunks.add(normalizeChunkPath(chunk))
}
}
......@@ -82,21 +131,29 @@ export function computeAppBuildManifestFromClientReferences(
if (manifestData.entryCSSFiles) {
for (const cssFiles of Object.values(manifestData.entryCSSFiles)) {
for (const cssFile of cssFiles) {
chunks.add(cssFile.path)
allFiles.add(cssFile.path)
chunks.add(normalizeChunkPath(cssFile.path))
}
}
}
// Store the route with all its required chunks
manifest.pages[route] = Array.from(chunks).sort()
// Add entry JS files (turbopack specific - these are shared chunks)
if (manifestData.entryJSFiles) {
for (const jsFiles of Object.values(manifestData.entryJSFiles)) {
for (const jsFile of jsFiles) {
chunks.add(normalizeChunkPath(jsFile))
}
}
}
// Normalize the route path and store it with all its required chunks
const normalizedRoute = normalizeAppPath(route)
manifest.pages[normalizedRoute] = Array.from(chunks).sort()
} catch (error) {
// Skip malformed manifest files
console.warn(
`Failed to parse client reference manifest: ${manifestFile}`,
error
)
continue
}
}
......@@ -109,6 +166,10 @@ export function computeAppBuildManifestFromClientReferences(
})
manifest.pages = sortedPages
// Cache the result
cachedManifest = manifest
cachedDistPath = distPath
return manifest
}
......
......@@ -1856,7 +1856,7 @@ export default async function build(
const fallbackModes = new Map<string, FallbackMode>()
const appDefaultConfigs = new Map<string, AppSegmentConfig>()
const pageInfos: PageInfos = new Map<string, PageInfo>()
let pagesManifest = await readManifest<PagesManifest>(pagesManifestPath)
const pagesManifest = await readManifest<PagesManifest>(pagesManifestPath)
const buildManifest = await readManifest<BuildManifest>(buildManifestPath)
const appPathRoutes: Record<string, string> = {}
......
......@@ -69,7 +69,6 @@ import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-pa
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { getRuntimeContext } from '../server/web/sandbox'
import { isClientReference } from '../lib/client-and-server-references'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { denormalizeAppPagePath } from '../shared/lib/page-path/denormalize-app-path'
import { RouteKind } from '../server/route-kind'
import type { PageExtensions } from './page-extensions-type'
......@@ -184,10 +183,8 @@ export async function computeFromManifest(
for (const file of manifest[key]) {
if (key === '/_app') {
map.set(file, Infinity)
} else if (map.has(file)) {
map.set(file, map.get(file)! + 1)
} else {
map.set(file, 1)
map.set(file, (map.get(file) ?? 0) + 1)
}
}
}
......@@ -931,18 +928,6 @@ export async function getJsPageSizeInKb(
? buildManifest
: computeAppBuildManifestFromClientReferences(distPath)
// Normalize app manifest keys
if (routerType === 'app') {
pageManifest.pages = Object.entries(pageManifest.pages).reduce(
(acc: Record<string, string[]>, [key, value]) => {
const newKey = normalizeAppPath(key)
acc[newKey] = value as string[]
return acc
},
{}
)
}
// If stats was not provided, then compute it again.
const stats =
cachedStats ??
......
......@@ -21,7 +21,7 @@ export function getLinkAndScriptTags(
const entryCSSFiles =
clientReferenceManifest.entryCSSFiles[filePathWithoutExt]
const entryJSFiles =
clientReferenceManifest.entryJSFiles?.[filePathWithoutExt] ?? []
clientReferenceManifest.entryJSFiles?.[filePathWithoutExt]
if (entryCSSFiles) {
for (const css of entryCSSFiles) {
......
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