Commit c15cba83 authored by Luke Sandberg's avatar Luke Sandberg

nonsense

parent 5703cb9e
...@@ -240,11 +240,8 @@ impl ClientReferenceManifest { ...@@ -240,11 +240,8 @@ impl ClientReferenceManifest {
.await?; .await?;
let chunk_paths = ssr_chunks_paths let chunk_paths = ssr_chunks_paths
.filter_map(|(_, chunk_path)| { .filter_map(|(_, chunk_path)| {
node_root_ref node_root_ref.get_path_to(&chunk_path).map(RcStr::from)
.get_path_to(&chunk_path)
.map(ToString::to_string)
}) })
.map(RcStr::from)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let is_async = async_modules.contains(&ResolvedVc::upcast(ssr_module)); let is_async = async_modules.contains(&ResolvedVc::upcast(ssr_module));
......
...@@ -808,5 +808,6 @@ ...@@ -808,5 +808,6 @@
"807": "Expected a %s response header.", "807": "Expected a %s response header.",
"808": "Invalid binary HMR message: insufficient data (expected %s bytes, got %s)", "808": "Invalid binary HMR message: insufficient data (expected %s bytes, got %s)",
"809": "Invalid binary HMR message of type %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' ...@@ -3,11 +3,39 @@ import { join } from 'node:path'
import { CLIENT_REFERENCE_MANIFEST } from '../shared/lib/constants' import { CLIENT_REFERENCE_MANIFEST } from '../shared/lib/constants'
import type { ClientReferenceManifest } from './webpack/plugins/flight-manifest-plugin' import type { ClientReferenceManifest } from './webpack/plugins/flight-manifest-plugin'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
export interface AppBuildManifestFromClientReferences { export interface AppBuildManifestFromClientReferences {
pages: Record<string, string[]> 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 * Extracts route-based manifest data from client reference manifests
* This provides a more accurate representation of client-side bundle requirements * This provides a more accurate representation of client-side bundle requirements
...@@ -16,14 +44,26 @@ export interface AppBuildManifestFromClientReferences { ...@@ -16,14 +44,26 @@ export interface AppBuildManifestFromClientReferences {
export function computeAppBuildManifestFromClientReferences( export function computeAppBuildManifestFromClientReferences(
distPath: string distPath: string
): AppBuildManifestFromClientReferences { ): AppBuildManifestFromClientReferences {
// Return cached result if available for the same distPath
if (cachedManifest && cachedDistPath === distPath) {
return cachedManifest
}
const serverAppPath = join(distPath, 'server', 'app') const serverAppPath = join(distPath, 'server', 'app')
if (!existsSync(serverAppPath)) { if (!existsSync(serverAppPath)) {
return { pages: {} } const emptyManifest = { pages: {} }
cachedManifest = emptyManifest
cachedDistPath = distPath
return emptyManifest
} }
const manifest: AppBuildManifestFromClientReferences = { pages: {} } 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 // Recursively find all client reference manifest files
function findClientReferenceManifests( function findClientReferenceManifests(
...@@ -49,6 +89,11 @@ export function computeAppBuildManifestFromClientReferences( ...@@ -49,6 +89,11 @@ export function computeAppBuildManifestFromClientReferences(
const manifestFiles = findClientReferenceManifests(serverAppPath) const manifestFiles = findClientReferenceManifests(serverAppPath)
console.log(
'[client-ref-manifest] Found manifest files:',
manifestFiles.length
)
for (const manifestFile of manifestFiles) { for (const manifestFile of manifestFiles) {
try { try {
// Read and parse the client reference manifest // Read and parse the client reference manifest
...@@ -58,11 +103,16 @@ export function computeAppBuildManifestFromClientReferences( ...@@ -58,11 +103,16 @@ export function computeAppBuildManifestFromClientReferences(
const routeMatch = content.match( const routeMatch = content.match(
/globalThis\.__RSC_MANIFEST\["([^"]+)"\]\s*=\s*({.*})/ /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 route = routeMatch[1]
const manifestDataStr = routeMatch[2] const manifestDataStr = routeMatch[2]
console.log('[client-ref-manifest] Processing route:', route)
// Parse the manifest data // Parse the manifest data
const manifestData: ClientReferenceManifest = JSON.parse(manifestDataStr) const manifestData: ClientReferenceManifest = JSON.parse(manifestDataStr)
...@@ -73,8 +123,7 @@ export function computeAppBuildManifestFromClientReferences( ...@@ -73,8 +123,7 @@ export function computeAppBuildManifestFromClientReferences(
// This is the key data - what client-side JS actually needs to be loaded // This is the key data - what client-side JS actually needs to be loaded
for (const moduleInfo of Object.values(manifestData.clientModules)) { for (const moduleInfo of Object.values(manifestData.clientModules)) {
for (const chunk of moduleInfo.chunks) { for (const chunk of moduleInfo.chunks) {
chunks.add(chunk) chunks.add(normalizeChunkPath(chunk))
allFiles.add(chunk)
} }
} }
...@@ -82,21 +131,29 @@ export function computeAppBuildManifestFromClientReferences( ...@@ -82,21 +131,29 @@ export function computeAppBuildManifestFromClientReferences(
if (manifestData.entryCSSFiles) { if (manifestData.entryCSSFiles) {
for (const cssFiles of Object.values(manifestData.entryCSSFiles)) { for (const cssFiles of Object.values(manifestData.entryCSSFiles)) {
for (const cssFile of cssFiles) { for (const cssFile of cssFiles) {
chunks.add(cssFile.path) chunks.add(normalizeChunkPath(cssFile.path))
allFiles.add(cssFile.path) }
}
}
// 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))
} }
} }
} }
// Store the route with all its required chunks // Normalize the route path and store it with all its required chunks
manifest.pages[route] = Array.from(chunks).sort() const normalizedRoute = normalizeAppPath(route)
manifest.pages[normalizedRoute] = Array.from(chunks).sort()
} catch (error) { } catch (error) {
// Skip malformed manifest files // Skip malformed manifest files
console.warn( console.warn(
`Failed to parse client reference manifest: ${manifestFile}`, `Failed to parse client reference manifest: ${manifestFile}`,
error error
) )
continue
} }
} }
...@@ -109,6 +166,10 @@ export function computeAppBuildManifestFromClientReferences( ...@@ -109,6 +166,10 @@ export function computeAppBuildManifestFromClientReferences(
}) })
manifest.pages = sortedPages manifest.pages = sortedPages
// Cache the result
cachedManifest = manifest
cachedDistPath = distPath
return manifest return manifest
} }
......
...@@ -1856,7 +1856,7 @@ export default async function build( ...@@ -1856,7 +1856,7 @@ export default async function build(
const fallbackModes = new Map<string, FallbackMode>() const fallbackModes = new Map<string, FallbackMode>()
const appDefaultConfigs = new Map<string, AppSegmentConfig>() const appDefaultConfigs = new Map<string, AppSegmentConfig>()
const pageInfos: PageInfos = new Map<string, PageInfo>() 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 buildManifest = await readManifest<BuildManifest>(buildManifestPath)
const appPathRoutes: Record<string, string> = {} const appPathRoutes: Record<string, string> = {}
......
...@@ -69,7 +69,6 @@ import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-pa ...@@ -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 { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { getRuntimeContext } from '../server/web/sandbox' import { getRuntimeContext } from '../server/web/sandbox'
import { isClientReference } from '../lib/client-and-server-references' 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 { denormalizeAppPagePath } from '../shared/lib/page-path/denormalize-app-path'
import { RouteKind } from '../server/route-kind' import { RouteKind } from '../server/route-kind'
import type { PageExtensions } from './page-extensions-type' import type { PageExtensions } from './page-extensions-type'
...@@ -184,10 +183,8 @@ export async function computeFromManifest( ...@@ -184,10 +183,8 @@ export async function computeFromManifest(
for (const file of manifest[key]) { for (const file of manifest[key]) {
if (key === '/_app') { if (key === '/_app') {
map.set(file, Infinity) map.set(file, Infinity)
} else if (map.has(file)) {
map.set(file, map.get(file)! + 1)
} else { } else {
map.set(file, 1) map.set(file, (map.get(file) ?? 0) + 1)
} }
} }
} }
...@@ -931,18 +928,6 @@ export async function getJsPageSizeInKb( ...@@ -931,18 +928,6 @@ export async function getJsPageSizeInKb(
? buildManifest ? buildManifest
: computeAppBuildManifestFromClientReferences(distPath) : 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. // If stats was not provided, then compute it again.
const stats = const stats =
cachedStats ?? cachedStats ??
......
...@@ -21,7 +21,7 @@ export function getLinkAndScriptTags( ...@@ -21,7 +21,7 @@ export function getLinkAndScriptTags(
const entryCSSFiles = const entryCSSFiles =
clientReferenceManifest.entryCSSFiles[filePathWithoutExt] clientReferenceManifest.entryCSSFiles[filePathWithoutExt]
const entryJSFiles = const entryJSFiles =
clientReferenceManifest.entryJSFiles?.[filePathWithoutExt] ?? [] clientReferenceManifest.entryJSFiles?.[filePathWithoutExt]
if (entryCSSFiles) { if (entryCSSFiles) {
for (const css of 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