Commit 2ffcc59e authored by Alex Kirszenberg's avatar Alex Kirszenberg Committed by GitHub

Merge `EcmascriptChunkUpdate`s before sending them to the client (vercel/turbo#3975)

This diff:
* introduces the `VersionedContentMerger` trait, which allows for
merging the updates of versioned contents within the same chunk group;
* implements this for `EcmascriptChunkContent`/`EcmascriptChunkUpdate`,
turning them into
`EcmascriptMergedChunkContent`/`EcmascriptMergedChunkUpdate`;
* creates a new `ChunkList` asset which is capable of merging chunk
updates of chunks within the same chunk group, and create such an asset
for dynamic chunks (through manifest/loader_item.rs) and chunk group
files assets.

This fixes a bunch of edge cases related to HMR:
* HMR of dynamic imports now works;
* Chunks getting added/deleted/renamed now also works with HMR, since
we're listening to updates at the chunk group level;
* CSS chunks get reloaded in the right order, with respect for
precedence.

There are still known edge cases with HMR:
* CSS chunks added through HMR are not inserted at the right position to
respect precedence (WEB-652).
* Update aggregation is disabled because we don't want a critical issue
to stop all HMR (WEB-582) . This would be fixed by applying aggregated
updates when dismissing the error modal, but there are some edge cases
with this too (e.g. what happens when an HMR update also causes an error
on top of an existing error).
parent e6103ae6
import type { import type {
ChunkListUpdate,
ChunkUpdate,
ClientMessage, ClientMessage,
HmrUpdateEntry, EcmascriptMergedChunkUpdate,
EcmascriptMergedUpdate,
EcmascriptModuleEntry,
Issue, Issue,
ResourceIdentifier, ResourceIdentifier,
ServerMessage, ServerMessage,
...@@ -56,8 +60,6 @@ export function connect({ assetPrefix }: ClientOptions) { ...@@ -56,8 +60,6 @@ export function connect({ assetPrefix }: ClientOptions) {
subscribeToChunkUpdate(chunkPath, callback); subscribeToChunkUpdate(chunkPath, callback);
} }
} }
subscribeToInitialCssChunksUpdates(assetPrefix);
} }
type UpdateCallbackSet = { type UpdateCallbackSet = {
...@@ -100,114 +102,332 @@ function handleSocketConnected() { ...@@ -100,114 +102,332 @@ function handleSocketConnected() {
} }
} }
type AggregatedUpdates = { // we aggregate all pending updates until the issues are resolved
added: Record<ModuleId, HmrUpdateEntry>; const chunkListsWithPendingUpdates: Map<
modified: Record<ModuleId, HmrUpdateEntry>; ResourceKey,
deleted: Set<ModuleId>; { update: ChunkListUpdate; resource: ResourceIdentifier }
}; > = new Map();
// we aggregate all updates until the issues are resolved
const chunksWithUpdates: Map<ResourceKey, AggregatedUpdates> = new Map();
function aggregateUpdates( function aggregateUpdates(
msg: ServerMessage, msg: ServerMessage,
hasCriticalIssues: boolean aggregate: boolean
): ServerMessage { ): ServerMessage {
const key = resourceKey(msg.resource); const key = resourceKey(msg.resource);
const aggregated = chunksWithUpdates.get(key); let aggregated = chunkListsWithPendingUpdates.get(key);
if (msg.type === "issues" && aggregated == null && hasCriticalIssues) {
// add an empty record to make sure we don't call `onBuildOk`
chunksWithUpdates.set(key, {
added: {},
modified: {},
deleted: new Set(),
});
}
if (msg.type === "issues" && aggregated != null) { if (msg.type === "issues" && aggregated != null) {
if (!hasCriticalIssues) { if (!aggregate) {
chunksWithUpdates.delete(key); chunkListsWithPendingUpdates.delete(key);
} }
return { return {
...msg, ...msg,
type: "partial", type: "partial",
instruction: { instruction: aggregated.update,
type: "EcmascriptChunkUpdate",
added: aggregated.added,
modified: aggregated.modified,
deleted: Array.from(aggregated.deleted),
},
}; };
} }
if (msg.type !== "partial") return msg; if (msg.type !== "partial") return msg;
if (aggregated == null) { if (aggregated == null) {
if (hasCriticalIssues) { if (aggregate) {
chunksWithUpdates.set(key, { chunkListsWithPendingUpdates.set(key, {
added: msg.instruction.added, resource: msg.resource,
modified: msg.instruction.modified, update: msg.instruction,
deleted: new Set(msg.instruction.deleted),
}); });
} }
return msg; return msg;
} }
for (const [moduleId, entry] of Object.entries(msg.instruction.added)) { aggregated = {
const removedDeleted = aggregated.deleted.delete(moduleId); resource: msg.resource,
if (aggregated.modified[moduleId] != null) { update: mergeChunkListUpdates(aggregated.update, msg.instruction),
console.error( };
`impossible state aggregating updates: module "${moduleId}" was added, but previously modified`
); if (aggregate) {
location.reload(); chunkListsWithPendingUpdates.set(key, aggregated);
} else {
// Once we receive a partial update with no critical issues, we can stop aggregating updates.
// The aggregated update will be applied.
chunkListsWithPendingUpdates.delete(key);
} }
if (removedDeleted) { return {
aggregated.modified[moduleId] = entry; ...msg,
instruction: aggregated.update,
};
}
function mergeChunkListUpdates(
updateA: ChunkListUpdate,
updateB: ChunkListUpdate
): ChunkListUpdate {
let chunks;
if (updateA.chunks != null) {
if (updateB.chunks == null) {
chunks = updateA.chunks;
} else { } else {
aggregated.added[moduleId] = entry; chunks = mergeChunkListChunks(updateA.chunks, updateB.chunks);
} }
} else if (updateB.chunks != null) {
chunks = updateB.chunks;
} }
for (const [moduleId, entry] of Object.entries(msg.instruction.modified)) { let merged;
if (aggregated.added[moduleId] != null) { if (updateA.merged != null) {
aggregated.added[moduleId] = entry; if (updateB.merged == null) {
merged = updateA.merged;
} else { } else {
aggregated.modified[moduleId] = entry; // Since `merged` is an array of updates, we need to merge them all into
// one, consistent update.
// Since there can only be `EcmascriptMergeUpdates` in the array, there is
// no need to key on the `type` field.
let update = updateA.merged[0];
for (let i = 1; i < updateA.merged.length; i++) {
update = mergeChunkListEcmascriptMergedUpdates(
update,
updateA.merged[i]
);
} }
if (aggregated.deleted.has(moduleId)) { for (let i = 0; i < updateB.merged.length; i++) {
console.error( update = mergeChunkListEcmascriptMergedUpdates(
`impossible state aggregating updates: module "${moduleId}" was modified, but previously deleted` update,
updateB.merged[i]
); );
location.reload();
} }
merged = [update];
}
} else if (updateB.merged != null) {
merged = updateB.merged;
}
return {
type: "ChunkListUpdate",
chunks,
merged,
};
}
function mergeChunkListChunks(
chunksA: Record<ChunkPath, ChunkUpdate>,
chunksB: Record<ChunkPath, ChunkUpdate>
): Record<ChunkPath, ChunkUpdate> {
const chunks: Record<ChunkPath, ChunkUpdate> = {};
for (const [chunkPath, chunkUpdateA] of Object.entries(chunksA)) {
const chunkUpdateB = chunksB[chunkPath];
if (chunkUpdateB != null) {
const mergedUpdate = mergeChunkUpdates(chunkUpdateA, chunkUpdateB);
if (mergedUpdate != null) {
chunks[chunkPath] = mergedUpdate;
}
} else {
chunks[chunkPath] = chunkUpdateA;
}
}
for (const [chunkPath, chunkUpdateB] of Object.entries(chunksB)) {
if (chunks[chunkPath] == null) {
chunks[chunkPath] = chunkUpdateB;
}
}
return chunks;
}
function mergeChunkUpdates(
updateA: ChunkUpdate,
updateB: ChunkUpdate
): ChunkUpdate | undefined {
if (
(updateA.type === "added" && updateB.type === "deleted") ||
(updateA.type === "deleted" && updateB.type === "added")
) {
return undefined;
}
if (updateA.type === "partial") {
invariant(updateA.instruction, "Partial updates are unsupported");
}
if (updateB.type === "partial") {
invariant(updateB.instruction, "Partial updates are unsupported");
}
return undefined;
}
function mergeChunkListEcmascriptMergedUpdates(
mergedA: EcmascriptMergedUpdate,
mergedB: EcmascriptMergedUpdate
): EcmascriptMergedUpdate {
const entries = mergeEcmascriptChunkEntries(mergedA.entries, mergedB.entries);
const chunks = mergeEcmascriptChunksUpdates(mergedA.chunks, mergedB.chunks);
return {
type: "EcmascriptMergedUpdate",
entries,
chunks,
};
}
function mergeEcmascriptChunkEntries(
entriesA: Record<ModuleId, EcmascriptModuleEntry> | undefined,
entriesB: Record<ModuleId, EcmascriptModuleEntry> | undefined
): Record<ModuleId, EcmascriptModuleEntry> {
return { ...entriesA, ...entriesB };
}
function mergeEcmascriptChunksUpdates(
chunksA: Record<ChunkPath, EcmascriptMergedChunkUpdate> | undefined,
chunksB: Record<ChunkPath, EcmascriptMergedChunkUpdate> | undefined
): Record<ChunkPath, EcmascriptMergedChunkUpdate> | undefined {
if (chunksA == null) {
return chunksB;
} }
for (const moduleId of msg.instruction.deleted) { if (chunksB == null) {
delete aggregated.added[moduleId]; return chunksA;
delete aggregated.modified[moduleId];
aggregated.deleted.add(moduleId);
} }
if (!hasCriticalIssues) { const chunks: Record<ChunkPath, EcmascriptMergedChunkUpdate> = {};
chunksWithUpdates.delete(key);
for (const [chunkPath, chunkUpdateA] of Object.entries(chunksA)) {
const chunkUpdateB = chunksB[chunkPath];
if (chunkUpdateB != null) {
const mergedUpdate = mergeEcmascriptChunkUpdates(
chunkUpdateA,
chunkUpdateB
);
if (mergedUpdate != null) {
chunks[chunkPath] = mergedUpdate;
}
} else { } else {
chunksWithUpdates.set(key, aggregated); chunks[chunkPath] = chunkUpdateA;
}
}
for (const [chunkPath, chunkUpdateB] of Object.entries(chunksB)) {
if (chunks[chunkPath] == null) {
chunks[chunkPath] = chunkUpdateB;
}
}
if (Object.keys(chunks).length === 0) {
return undefined;
}
return chunks;
}
function mergeEcmascriptChunkUpdates(
updateA: EcmascriptMergedChunkUpdate,
updateB: EcmascriptMergedChunkUpdate
): EcmascriptMergedChunkUpdate | undefined {
if (updateA.type === "added" && updateB.type === "deleted") {
// These two completely cancel each other out.
return undefined;
}
if (updateA.type === "deleted" && updateB.type === "added") {
const added = [];
const deleted = [];
const deletedModules = new Set(updateA.modules ?? []);
const addedModules = new Set(updateB.modules ?? []);
for (const moduleId of addedModules) {
if (!deletedModules.has(moduleId)) {
added.push(moduleId);
}
}
for (const moduleId of deletedModules) {
if (!addedModules.has(moduleId)) {
deleted.push(moduleId);
}
}
if (added.length === 0 && deleted.length === 0) {
return undefined;
} }
return { return {
...msg, type: "partial",
instruction: { added,
type: "EcmascriptChunkUpdate", deleted,
added: aggregated.added, };
modified: aggregated.modified, }
deleted: Array.from(aggregated.deleted),
}, if (updateA.type === "partial" && updateB.type === "partial") {
const added = new Set([...(updateA.added ?? []), ...(updateB.added ?? [])]);
const deleted = new Set([
...(updateA.deleted ?? []),
...(updateB.deleted ?? []),
]);
if (updateB.added != null) {
for (const moduleId of updateB.added) {
deleted.delete(moduleId);
}
}
if (updateB.deleted != null) {
for (const moduleId of updateB.deleted) {
added.delete(moduleId);
}
}
return {
type: "partial",
added: [...added],
deleted: [...deleted],
}; };
}
if (updateA.type === "added" && updateB.type === "partial") {
const modules = new Set([
...(updateA.modules ?? []),
...(updateB.added ?? []),
]);
for (const moduleId of updateB.deleted ?? []) {
modules.delete(moduleId);
}
return {
type: "added",
modules: [...modules],
};
}
if (updateA.type === "partial" && updateB.type === "deleted") {
// We could eagerly return `updateB` here, but this would potentially be
// incorrect if `updateA` has added modules.
const modules = new Set(updateB.modules ?? []);
if (updateA.added != null) {
for (const moduleId of updateA.added) {
modules.delete(moduleId);
}
}
return {
type: "deleted",
modules: [...modules],
};
}
// Any other update combination is invalid.
return undefined;
}
function invariant(never: never, message: string): never {
throw new Error(`Invariant: ${message}`);
} }
const CRITICAL = ["bug", "error", "fatal"]; const CRITICAL = ["bug", "error", "fatal"];
...@@ -282,9 +502,14 @@ function handleSocketMessage(msg: ServerMessage) { ...@@ -282,9 +502,14 @@ function handleSocketMessage(msg: ServerMessage) {
sortIssues(msg.issues); sortIssues(msg.issues);
const hasCriticalIssues = handleIssues(msg); const hasCriticalIssues = handleIssues(msg);
const aggregatedMsg = aggregateUpdates(msg, hasCriticalIssues);
const runHooks = chunksWithUpdates.size === 0; // TODO(WEB-582) Disable update aggregation for now.
const aggregate = /* hasCriticalIssues */ false;
const aggregatedMsg = aggregateUpdates(msg, aggregate);
if (aggregate) return;
const runHooks = chunkListsWithPendingUpdates.size === 0;
if (aggregatedMsg.type !== "issues") { if (aggregatedMsg.type !== "issues") {
if (runHooks) onBeforeRefresh(); if (runHooks) onBeforeRefresh();
...@@ -354,6 +579,16 @@ function triggerUpdate(msg: ServerMessage) { ...@@ -354,6 +579,16 @@ function triggerUpdate(msg: ServerMessage) {
for (const callback of callbackSet.callbacks) { for (const callback of callbackSet.callbacks) {
callback(msg); callback(msg);
} }
if (msg.type === "notFound") {
// This indicates that the resource which we subscribed to either does not exist or
// has been deleted. In either case, we should clear all update callbacks, so if a
// new subscription is created for the same resource, it will send a new "subscribe"
// message to the server.
// No need to send an "unsubscribe" message to the server, it will have already
// dropped the update stream before sending the "notFound" message.
updateCallbackSets.delete(key);
}
} catch (err) { } catch (err) {
console.error( console.error(
`An error occurred during the update of resource \`${msg.resource.path}\``, `An error occurred during the update of resource \`${msg.resource.path}\``,
...@@ -362,46 +597,3 @@ function triggerUpdate(msg: ServerMessage) { ...@@ -362,46 +597,3 @@ function triggerUpdate(msg: ServerMessage) {
location.reload(); location.reload();
} }
} }
// Unlike ES chunks, CSS chunks cannot contain the logic to accept updates.
// They must be reloaded here instead.
function subscribeToInitialCssChunksUpdates(assetPrefix: string) {
const initialCssChunkLinks: NodeListOf<HTMLLinkElement> =
document.head.querySelectorAll(`link[rel="stylesheet"]`);
initialCssChunkLinks.forEach((link) => {
subscribeToCssChunkUpdates(assetPrefix, link);
});
}
export function subscribeToCssChunkUpdates(
assetPrefix: string,
link: HTMLLinkElement
) {
const cssChunkPrefix = `${assetPrefix}/`;
const href = link.href;
if (href == null) {
return;
}
const { pathname, origin } = new URL(href);
if (origin !== location.origin || !pathname.startsWith(cssChunkPrefix)) {
return;
}
const chunkPath = pathname.slice(cssChunkPrefix.length);
subscribeToChunkUpdate(chunkPath, (update) => {
switch (update.type) {
case "restart": {
console.info(`Reloading CSS chunk \`${chunkPath}\``);
link.replaceWith(link);
break;
}
case "partial":
throw new Error(`partial CSS chunk updates are not supported`);
default:
throw new Error(`unknown update type \`${update}\``);
}
});
}
...@@ -37,7 +37,7 @@ import { headersFromEntries } from "@vercel/turbopack-next/internal/headers"; ...@@ -37,7 +37,7 @@ import { headersFromEntries } from "@vercel/turbopack-next/internal/headers";
import { parse, ParsedUrlQuery } from "node:querystring"; import { parse, ParsedUrlQuery } from "node:querystring";
globalThis.__next_require__ = (data) => { globalThis.__next_require__ = (data) => {
const [, , ssr_id] = JSON.parse(data); const [, , , ssr_id] = JSON.parse(data);
return __turbopack_require__(ssr_id); return __turbopack_require__(ssr_id);
}; };
globalThis.__next_chunk_load__ = () => Promise.resolve(); globalThis.__next_chunk_load__ = () => Promise.resolve();
......
...@@ -19,7 +19,8 @@ window.next = { ...@@ -19,7 +19,8 @@ window.next = {
}; };
globalThis.__next_require__ = (data) => { globalThis.__next_require__ = (data) => {
const [client_id] = JSON.parse(data); const [client_id, chunks, chunkListPath] = JSON.parse(data);
__turbopack_register_chunk_list__(chunkListPath, chunks);
return __turbopack_require__(client_id); return __turbopack_require__(client_id);
}; };
globalThis.__next_chunk_load__ = __turbopack_load__; globalThis.__next_chunk_load__ = __turbopack_load__;
......
...@@ -5,3 +5,4 @@ export = Anything; ...@@ -5,3 +5,4 @@ export = Anything;
export const __turbopack_module_id__: string | number; export const __turbopack_module_id__: string | number;
export const chunks: string[]; export const chunks: string[];
export const chunkListPath: string;
...@@ -5,6 +5,8 @@ import { createProxy } from "next/dist/build/webpack/loaders/next-flight-loader/ ...@@ -5,6 +5,8 @@ import { createProxy } from "next/dist/build/webpack/loaders/next-flight-loader/
import { __turbopack_module_id__ as id } from "CLIENT_MODULE"; import { __turbopack_module_id__ as id } from "CLIENT_MODULE";
// @ts-expect-error CLIENT_CHUNKS is provided by rust // @ts-expect-error CLIENT_CHUNKS is provided by rust
import client_id, { chunks } from "CLIENT_CHUNKS"; import client_id, { chunks, chunkListPath } from "CLIENT_CHUNKS";
export default createProxy(JSON.stringify([client_id, chunks, id])); export default createProxy(
JSON.stringify([client_id, chunks, chunkListPath, id])
);
import { createProxy } from "next/dist/build/webpack/loaders/next-flight-loader/module-proxy"; import { createProxy } from "next/dist/build/webpack/loaders/next-flight-loader/module-proxy";
// @ts-expect-error CLIENT_CHUNKS is provided by rust // @ts-expect-error CLIENT_CHUNKS is provided by rust
import client_id, { chunks } from "CLIENT_CHUNKS"; import client_id, { chunks, chunkListPath } from "CLIENT_CHUNKS";
export default createProxy(JSON.stringify([client_id, chunks])); export default createProxy(JSON.stringify([client_id, chunks, chunkListPath]));
...@@ -18,7 +18,7 @@ subscribeToUpdate( ...@@ -18,7 +18,7 @@ subscribeToUpdate(
}, },
}, },
(update) => { (update) => {
if (update.type === "restart") { if (update.type === "restart" || update.type === "notFound") {
location.reload(); location.reload();
} }
} }
......
...@@ -97,7 +97,7 @@ function subscribeToPageManifest({ assetPrefix }: { assetPrefix: string }) { ...@@ -97,7 +97,7 @@ function subscribeToPageManifest({ assetPrefix }: { assetPrefix: string }) {
path: "_next/static/development/_devPagesManifest.json", path: "_next/static/development/_devPagesManifest.json",
}, },
(update) => { (update) => {
if (["restart", "partial"].includes(update.type)) { if (["restart", "notFound", "partial"].includes(update.type)) {
return; return;
} }
......
declare global { declare global {
function __turbopack_require__(name: any): any; function __turbopack_require__(name: any): any;
function __turbopack_load__(path: string): any; function __turbopack_load__(path: string): any;
function __turbopack_register_chunk_list__(
chunkListPath: string,
chunksPaths: string[]
): any;
function __webpack_require__(name: any): any; function __webpack_require__(name: any): any;
var __webpack_public_path__: string | undefined; var __webpack_public_path__: string | undefined;
var __DEV_MIDDLEWARE_MATCHERS: any[]; var __DEV_MIDDLEWARE_MATCHERS: any[];
......
...@@ -411,7 +411,7 @@ async fn create_app_source_for_directory( ...@@ -411,7 +411,7 @@ async fn create_app_source_for_directory(
segments: layouts, segments: layouts,
} => { } => {
let LayoutSegment { target, .. } = *segment.await?; let LayoutSegment { target, .. } = *segment.await?;
let pathname = pathname_for_path(server_root, url, false); let pathname = pathname_for_path(server_root, url, false, false);
let params_matcher = NextParamsMatcherVc::new(pathname); let params_matcher = NextParamsMatcherVc::new(pathname);
sources.push(create_node_rendered_source( sources.push(create_node_rendered_source(
...@@ -443,7 +443,7 @@ async fn create_app_source_for_directory( ...@@ -443,7 +443,7 @@ async fn create_app_source_for_directory(
route, route,
.. ..
} => { } => {
let pathname = pathname_for_path(server_root, url, false); let pathname = pathname_for_path(server_root, url, false, false);
let params_matcher = NextParamsMatcherVc::new(pathname); let params_matcher = NextParamsMatcherVc::new(pathname);
sources.push(create_node_api_source( sources.push(create_node_api_source(
......
...@@ -95,6 +95,7 @@ impl Transition for NextClientTransition { ...@@ -95,6 +95,7 @@ impl Transition for NextClientTransition {
asset: asset.into(), asset: asset.into(),
chunking_context: self.client_chunking_context, chunking_context: self.client_chunking_context,
base_path: self.server_root.join("_next"), base_path: self.server_root.join("_next"),
server_root: self.server_root,
runtime_entries: Some(runtime_entries), runtime_entries: Some(runtime_entries),
}; };
......
use anyhow::Result; use anyhow::{bail, Result};
use indoc::formatdoc;
use serde_json::Value; use serde_json::Value;
use turbo_tasks::{primitives::StringVc, TryJoinIterExt, ValueToString, ValueToStringVc}; use turbo_tasks::{primitives::StringVc, TryJoinIterExt, ValueToString, ValueToStringVc};
use turbo_tasks_fs::FileSystemPathVc; use turbo_tasks_fs::FileSystemPathVc;
...@@ -13,14 +14,15 @@ use turbopack::ecmascript::{ ...@@ -13,14 +14,15 @@ use turbopack::ecmascript::{
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc}, asset::{Asset, AssetContentVc, AssetVc},
chunk::{ chunk::{
Chunk, ChunkGroupVc, ChunkItem, ChunkItemVc, ChunkVc, ChunkableAsset, Chunk, ChunkGroupVc, ChunkItem, ChunkItemVc, ChunkListReferenceVc, ChunkVc, ChunkableAsset,
ChunkableAssetReference, ChunkableAssetReferenceVc, ChunkableAssetVc, ChunkingContextVc, ChunkableAssetReference, ChunkableAssetReferenceVc, ChunkableAssetVc, ChunkingContext,
ChunkingType, ChunkingTypeOptionVc, ChunkingContextVc, ChunkingType, ChunkingTypeOptionVc,
}, },
ident::AssetIdentVc, ident::AssetIdentVc,
reference::{AssetReference, AssetReferenceVc, AssetReferencesVc}, reference::{AssetReference, AssetReferenceVc, AssetReferencesVc},
resolve::{ResolveResult, ResolveResultVc}, resolve::{ResolveResult, ResolveResultVc},
}; };
use turbopack_ecmascript::utils::stringify_js_pretty;
use super::in_chunking_context_asset::InChunkingContextAsset; use super::in_chunking_context_asset::InChunkingContextAsset;
...@@ -93,6 +95,25 @@ struct WithChunksChunkItem { ...@@ -93,6 +95,25 @@ struct WithChunksChunkItem {
inner: WithChunksAssetVc, inner: WithChunksAssetVc,
} }
#[turbo_tasks::value_impl]
impl WithChunksChunkItemVc {
#[turbo_tasks::function]
async fn chunk_list_path(self) -> Result<FileSystemPathVc> {
let this = self.await?;
Ok(this.inner_context.chunk_list_path(this.inner.ident()))
}
#[turbo_tasks::function]
async fn chunk_group(self) -> Result<ChunkGroupVc> {
let this = self.await?;
let inner = this.inner.await?;
Ok(ChunkGroupVc::from_asset(
inner.asset.into(),
this.inner_context,
))
}
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl EcmascriptChunkItem for WithChunksChunkItem { impl EcmascriptChunkItem for WithChunksChunkItem {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -101,29 +122,40 @@ impl EcmascriptChunkItem for WithChunksChunkItem { ...@@ -101,29 +122,40 @@ impl EcmascriptChunkItem for WithChunksChunkItem {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(&self) -> Result<EcmascriptChunkItemContentVc> { async fn content(self_vc: WithChunksChunkItemVc) -> Result<EcmascriptChunkItemContentVc> {
let inner = self.inner.await?; let this = self_vc.await?;
let group = ChunkGroupVc::from_asset(inner.asset.into(), self.inner_context); let inner = this.inner.await?;
let group = self_vc.chunk_group();
let chunks = group.chunks().await?; let chunks = group.chunks().await?;
let server_root = inner.server_root.await?; let server_root = inner.server_root.await?;
let mut client_chunks = Vec::new(); let mut client_chunks = Vec::new();
let chunk_list_path = self_vc.chunk_list_path().await?;
let chunk_list_path = if let Some(path) = server_root.get_path_to(&chunk_list_path) {
path
} else {
bail!("could not get path to chunk list");
};
for chunk_path in chunks.iter().map(|c| c.path()).try_join().await? { for chunk_path in chunks.iter().map(|c| c.path()).try_join().await? {
if let Some(path) = server_root.get_path_to(&chunk_path) { if let Some(path) = server_root.get_path_to(&chunk_path) {
client_chunks.push(Value::String(path.to_string())); client_chunks.push(Value::String(path.to_string()));
} }
} }
let module_id = stringify_js(&*inner.asset.as_chunk_item(self.inner_context).id().await?); let module_id = stringify_js(&*inner.asset.as_chunk_item(this.inner_context).id().await?);
Ok(EcmascriptChunkItemContent { Ok(EcmascriptChunkItemContent {
inner_code: format!( inner_code: formatdoc! {
"__turbopack_esm__({{ r#"
__turbopack_esm__({{
default: () => {}, default: () => {},
chunks: () => chunks chunks: () => {},
}}); chunkListPath: () => {},
const chunks = {}; }});
", "#,
module_id, module_id,
Value::Array(client_chunks) stringify_js_pretty(&client_chunks),
) stringify_js(&chunk_list_path),
}
.into(), .into(),
..Default::default() ..Default::default()
} }
...@@ -139,18 +171,27 @@ impl ChunkItem for WithChunksChunkItem { ...@@ -139,18 +171,27 @@ impl ChunkItem for WithChunksChunkItem {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn references(&self) -> Result<AssetReferencesVc> { async fn references(self_vc: WithChunksChunkItemVc) -> Result<AssetReferencesVc> {
let inner = self.inner.await?; let this = self_vc.await?;
Ok(AssetReferencesVc::cell(vec![WithChunksAssetReference { let inner = this.inner.await?;
Ok(AssetReferencesVc::cell(vec![
WithChunksAssetReference {
asset: InChunkingContextAsset { asset: InChunkingContextAsset {
asset: inner.asset, asset: inner.asset,
chunking_context: self.inner_context, chunking_context: this.inner_context,
} }
.cell() .cell()
.into(), .into(),
} }
.cell() .cell()
.into()])) .into(),
ChunkListReferenceVc::new(
inner.server_root,
self_vc.chunk_group(),
self_vc.chunk_list_path(),
)
.into(),
]))
} }
} }
......
...@@ -21,6 +21,7 @@ use turbopack_core::{ ...@@ -21,6 +21,7 @@ use turbopack_core::{
reference::{AssetReference, AssetReferenceVc, AssetReferencesVc}, reference::{AssetReference, AssetReferenceVc, AssetReferencesVc},
resolve::{ResolveResult, ResolveResultVc}, resolve::{ResolveResult, ResolveResultVc},
}; };
use turbopack_ecmascript::utils::stringify_js_pretty;
use crate::next_client_chunks::in_chunking_context_asset::InChunkingContextAsset; use crate::next_client_chunks::in_chunking_context_asset::InChunkingContextAsset;
...@@ -129,15 +130,17 @@ impl EcmascriptChunkItem for WithClientChunksChunkItem { ...@@ -129,15 +130,17 @@ impl EcmascriptChunkItem for WithClientChunksChunkItem {
let module_id = stringify_js(&*inner.asset.as_chunk_item(self.context).id().await?); let module_id = stringify_js(&*inner.asset.as_chunk_item(self.context).id().await?);
Ok(EcmascriptChunkItemContent { Ok(EcmascriptChunkItemContent {
inner_code: formatdoc!( inner_code: formatdoc!(
// We store the chunks in a binding, otherwise a new array would be created every
// time the export binding is read.
r#" r#"
__turbopack_esm__({{ __turbopack_esm__({{
default: () => __turbopack_import__({}), default: () => __turbopack_import__({}),
chunks: () => chunks chunks: () => chunks,
}}); }});
const chunks = {}; const chunks = {};
"#, "#,
module_id, module_id,
stringify_js(&client_chunks) stringify_js_pretty(&client_chunks),
) )
.into(), .into(),
..Default::default() ..Default::default()
...@@ -154,13 +157,14 @@ impl ChunkItem for WithClientChunksChunkItem { ...@@ -154,13 +157,14 @@ impl ChunkItem for WithClientChunksChunkItem {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn references(&self) -> Result<AssetReferencesVc> { async fn references(self_vc: WithClientChunksChunkItemVc) -> Result<AssetReferencesVc> {
let inner = self.inner.await?; let this = self_vc.await?;
let inner = this.inner.await?;
Ok(AssetReferencesVc::cell(vec![ Ok(AssetReferencesVc::cell(vec![
WithClientChunksAssetReference { WithClientChunksAssetReference {
asset: InChunkingContextAsset { asset: InChunkingContextAsset {
asset: inner.asset, asset: inner.asset,
chunking_context: self.context, chunking_context: this.context,
} }
.cell() .cell()
.into(), .into(),
......
...@@ -112,6 +112,7 @@ impl Transition for NextEdgeTransition { ...@@ -112,6 +112,7 @@ impl Transition for NextEdgeTransition {
asset: new_asset.into(), asset: new_asset.into(),
chunking_context: self.edge_chunking_context, chunking_context: self.edge_chunking_context,
base_path: self.output_path, base_path: self.output_path,
server_root: self.output_path,
runtime_entries: None, runtime_entries: None,
}; };
......
...@@ -2,7 +2,7 @@ use std::io::Write; ...@@ -2,7 +2,7 @@ use std::io::Write;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::indexmap; use indexmap::indexmap;
use turbo_tasks::{primitives::StringVc, Value}; use turbo_tasks::{primitives::StringVc, TryJoinIterExt, Value};
use turbo_tasks_fs::{rope::RopeBuilder, File, FileContent, FileSystemPathVc}; use turbo_tasks_fs::{rope::RopeBuilder, File, FileContent, FileSystemPathVc};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc}, asset::{Asset, AssetContentVc, AssetVc},
...@@ -116,17 +116,28 @@ impl Asset for PageLoaderAsset { ...@@ -116,17 +116,28 @@ impl Asset for PageLoaderAsset {
let this = &*self_vc.await?; let this = &*self_vc.await?;
let chunks = self_vc.get_page_chunks().await?; let chunks = self_vc.get_page_chunks().await?;
let server_root = this.server_root.await?;
let mut data = Vec::with_capacity(chunks.len());
for chunk in chunks.iter() { let chunk_paths: Vec<_> = chunks
let path = chunk.path().await?; .iter()
data.push(serde_json::Value::String(path.path.clone())); .map(|chunk| {
let server_root = server_root.clone();
async move {
Ok(server_root
.get_path_to(&*chunk.path().await?)
.map(|path| path.to_string()))
} }
})
.try_join()
.await?
.into_iter()
.flatten()
.collect();
let content = format!( let content = format!(
"__turbopack_load_page_chunks__({}, {})\n", "__turbopack_load_page_chunks__({}, {})\n",
stringify_js(&this.pathname.await?), stringify_js(&this.pathname.await?),
serde_json::Value::Array(data) stringify_js(&chunk_paths)
); );
Ok(AssetContentVc::from(File::from(content))) Ok(AssetContentVc::from(File::from(content)))
......
...@@ -346,7 +346,7 @@ async fn create_page_source_for_file( ...@@ -346,7 +346,7 @@ async fn create_page_source_for_file(
Value::new(ClientContextType::Pages { pages_dir }), Value::new(ClientContextType::Pages { pages_dir }),
); );
let pathname = pathname_for_path(server_root, server_path, true); let pathname = pathname_for_path(server_root, server_path, true, false);
let route_matcher = NextParamsMatcherVc::new(pathname); let route_matcher = NextParamsMatcherVc::new(pathname);
Ok(if is_api_path { Ok(if is_api_path {
...@@ -370,8 +370,9 @@ async fn create_page_source_for_file( ...@@ -370,8 +370,9 @@ async fn create_page_source_for_file(
runtime_entries, runtime_entries,
) )
} else { } else {
let data_pathname = pathname_for_path(server_root, server_path, true, true);
let data_route_matcher = let data_route_matcher =
NextPrefixSuffixParamsMatcherVc::new(pathname, "_next/data/development/", ".json"); NextPrefixSuffixParamsMatcherVc::new(data_pathname, "_next/data/development/", ".json");
let ssr_entry = SsrEntry { let ssr_entry = SsrEntry {
context: server_context, context: server_context,
......
...@@ -28,6 +28,7 @@ pub async fn pathname_for_path( ...@@ -28,6 +28,7 @@ pub async fn pathname_for_path(
server_root: FileSystemPathVc, server_root: FileSystemPathVc,
server_path: FileSystemPathVc, server_path: FileSystemPathVc,
has_extension: bool, has_extension: bool,
data: bool,
) -> Result<StringVc> { ) -> Result<StringVc> {
let server_path_value = &*server_path.await?; let server_path_value = &*server_path.await?;
let path = if let Some(path) = server_root.await?.get_path_to(server_path_value) { let path = if let Some(path) = server_root.await?.get_path_to(server_path_value) {
...@@ -46,10 +47,14 @@ pub async fn pathname_for_path( ...@@ -46,10 +47,14 @@ pub async fn pathname_for_path(
} else { } else {
path path
}; };
let path = if path == "index" { let path = if data {
path
} else {
if path == "index" {
"" ""
} else { } else {
path.strip_suffix("/index").unwrap_or(path) path.strip_suffix("/index").unwrap_or(path)
}
}; };
Ok(StringVc::cell(path.to_string())) Ok(StringVc::cell(path.to_string()))
......
...@@ -66,18 +66,23 @@ pub async fn create_web_entry_source( ...@@ -66,18 +66,23 @@ pub async fn create_web_entry_source(
}) })
.try_join() .try_join()
.await?; .await?;
let chunks: Vec<_> = entries
let chunk_groups: Vec<_> = entries
.into_iter() .into_iter()
.flatten() .flatten()
.enumerate() .enumerate()
.map(|(i, module)| async move { .map(|(i, module)| async move {
if let Some(ecmascript) = EcmascriptModuleAssetVc::resolve_from(module).await? { if let Some(ecmascript) = EcmascriptModuleAssetVc::resolve_from(module).await? {
Ok(ecmascript let chunk = ecmascript
.as_evaluated_chunk(chunking_context, (i == 0).then_some(runtime_entries))) .as_evaluated_chunk(chunking_context, (i == 0).then_some(runtime_entries));
let chunk_group = ChunkGroupVc::from_chunk(chunk);
Ok(chunk_group)
} else if let Some(chunkable) = ChunkableAssetVc::resolve_from(module).await? { } else if let Some(chunkable) = ChunkableAssetVc::resolve_from(module).await? {
// TODO this is missing runtime code, so it's probably broken and we should also // TODO this is missing runtime code, so it's probably broken and we should also
// add an ecmascript chunk with the runtime code // add an ecmascript chunk with the runtime code
Ok(chunkable.as_chunk(chunking_context)) Ok(ChunkGroupVc::from_chunk(
chunkable.as_chunk(chunking_context),
))
} else { } else {
// TODO convert into a serve-able asset // TODO convert into a serve-able asset
Err(anyhow!( Err(anyhow!(
...@@ -89,11 +94,7 @@ pub async fn create_web_entry_source( ...@@ -89,11 +94,7 @@ pub async fn create_web_entry_source(
.try_join() .try_join()
.await?; .await?;
let entry_asset = DevHtmlAssetVc::new( let entry_asset = DevHtmlAssetVc::new(server_root.join("index.html"), chunk_groups).into();
server_root.join("index.html"),
chunks.into_iter().map(ChunkGroupVc::from_chunk).collect(),
)
.into();
let graph = if eager_compile { let graph = if eager_compile {
AssetGraphContentSourceVc::new_eager(server_root, entry_asset) AssetGraphContentSourceVc::new_eager(server_root, entry_asset)
......
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