Unverified Commit 16d009ef authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

separate chunking per layout parts (#60569)

### What?

* creates a separate chunk group for client components per layout
segment (before it was per page)

### Why?

This fixes a bug with css loading on client transitions as next.js
assumes that the chunk group for the unchanged layout segments are
already loaded (fixes PACK-2163)


Closes PACK-2232
parent 4597ff35
...@@ -19,9 +19,7 @@ use next_core::{ ...@@ -19,9 +19,7 @@ use next_core::{
get_client_module_options_context, get_client_resolve_options_context, get_client_module_options_context, get_client_resolve_options_context,
get_client_runtime_entries, ClientContextType, RuntimeEntries, get_client_runtime_entries, ClientContextType, RuntimeEntries,
}, },
next_client_reference::{ next_client_reference::{ClientReferenceGraph, NextEcmascriptClientReferenceTransition},
ClientReferenceGraph, ClientReferenceType, NextEcmascriptClientReferenceTransition,
},
next_dynamic::NextDynamicTransition, next_dynamic::NextDynamicTransition,
next_edge::route_regex::get_named_middleware_regex, next_edge::route_regex::get_named_middleware_regex,
next_manifests::{ next_manifests::{
...@@ -611,10 +609,6 @@ impl AppEndpoint { ...@@ -611,10 +609,6 @@ impl AppEndpoint {
// )) // ))
// .await?; // .await?;
let app_entry_client_references = client_reference_graph
.entry(Vc::upcast(app_entry.rsc_entry))
.await?;
let runtime = app_entry.config.await?.runtime.unwrap_or_default(); let runtime = app_entry.config.await?.runtime.unwrap_or_default();
if ssr_and_client { if ssr_and_client {
...@@ -626,8 +620,7 @@ impl AppEndpoint { ...@@ -626,8 +620,7 @@ impl AppEndpoint {
}; };
let client_references_chunks = get_app_client_references_chunks( let client_references_chunks = get_app_client_references_chunks(
app_entry.rsc_entry.ident(), client_references,
client_reference_types,
this.app_project.project().client_chunking_context(), this.app_project.project().client_chunking_context(),
ssr_chunking_context, ssr_chunking_context,
); );
...@@ -636,13 +629,23 @@ impl AppEndpoint { ...@@ -636,13 +629,23 @@ impl AppEndpoint {
let mut entry_client_chunks = IndexSet::new(); let mut entry_client_chunks = IndexSet::new();
// TODO(alexkirsz) In which manifest does this go? // TODO(alexkirsz) In which manifest does this go?
let mut entry_ssr_chunks = IndexSet::new(); let mut entry_ssr_chunks = IndexSet::new();
for client_reference in app_entry_client_references.iter() { for chunks in client_references_chunks_ref
let client_reference_chunks = client_references_chunks_ref .layout_segment_client_chunks
.get(client_reference.ty()) .values()
.context("client reference should have corresponding chunks")?; {
entry_client_chunks entry_client_chunks.extend(chunks.await?.iter().copied());
.extend(client_reference_chunks.client_chunks.await?.iter().copied()); }
entry_ssr_chunks.extend(client_reference_chunks.ssr_chunks.await?.iter().copied()); for chunks in client_references_chunks_ref
.client_component_client_chunks
.values()
{
client_assets.extend(chunks.await?.iter().copied());
}
for chunks in client_references_chunks_ref
.client_component_ssr_chunks
.values()
{
entry_ssr_chunks.extend(chunks.await?.iter().copied());
} }
client_assets.extend(entry_client_chunks.iter().copied()); client_assets.extend(entry_client_chunks.iter().copied());
...@@ -718,15 +721,16 @@ impl AppEndpoint { ...@@ -718,15 +721,16 @@ impl AppEndpoint {
// initialization // initialization
let client_references_chunks = &*client_references_chunks.await?; let client_references_chunks = &*client_references_chunks.await?;
for (ty, chunks) in client_references_chunks { for &ssr_chunks in client_references_chunks
if matches!(ty, ClientReferenceType::EcmascriptClientReference(..)) { .client_component_ssr_chunks
let ssr_chunks = &*chunks.ssr_chunks.await?; .values()
{
let ssr_chunks = ssr_chunks.await?;
middleware_assets.extend(ssr_chunks); middleware_assets.extend(ssr_chunks);
} }
} }
} }
}
fn create_app_paths_manifest( fn create_app_paths_manifest(
node_root: Vc<FileSystemPath>, node_root: Vc<FileSystemPath>,
......
...@@ -5,7 +5,7 @@ use turbopack_binding::turbopack::{ ...@@ -5,7 +5,7 @@ use turbopack_binding::turbopack::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkableModuleReference, ChunkingContext}, chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkableModuleReference, ChunkingContext},
ident::AssetIdent, ident::AssetIdent,
module::{Module, Modules}, module::Module,
reference::{ModuleReference, ModuleReferences}, reference::{ModuleReference, ModuleReferences},
resolve::ModuleResolveResult, resolve::ModuleResolveResult,
}, },
...@@ -20,13 +20,13 @@ use turbopack_binding::turbopack::{ ...@@ -20,13 +20,13 @@ use turbopack_binding::turbopack::{
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct IncludeModulesModule { pub struct IncludeModulesModule {
ident: Vc<AssetIdent>, ident: Vc<AssetIdent>,
modules: Vc<Modules>, modules: Vec<Vc<Box<dyn Module>>>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl IncludeModulesModule { impl IncludeModulesModule {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new(ident: Vc<AssetIdent>, modules: Vc<Modules>) -> Vc<Self> { pub fn new(ident: Vc<AssetIdent>, modules: Vec<Vc<Box<dyn Module>>>) -> Vc<Self> {
Self { ident, modules }.cell() Self { ident, modules }.cell()
} }
} }
...@@ -48,7 +48,6 @@ impl Module for IncludeModulesModule { ...@@ -48,7 +48,6 @@ impl Module for IncludeModulesModule {
async fn references(&self) -> Result<Vc<ModuleReferences>> { async fn references(&self) -> Result<Vc<ModuleReferences>> {
Ok(Vc::cell( Ok(Vc::cell(
self.modules self.modules
.await?
.iter() .iter()
.map(|&module| async move { .map(|&module| async move {
Ok(Vc::upcast( Ok(Vc::upcast(
......
...@@ -16,9 +16,7 @@ use serde::{Deserialize, Serialize}; ...@@ -16,9 +16,7 @@ use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput}; use turbo_tasks::{trace::TraceRawVcs, TaskInput};
pub use crate::next_app::{ pub use crate::next_app::{
app_client_references_chunks::{ app_client_references_chunks::{get_app_client_references_chunks, ClientReferencesChunks},
get_app_client_references_chunks, ClientReferenceChunks, ClientReferencesChunks,
},
app_client_shared_chunks::get_app_client_shared_chunks, app_client_shared_chunks::get_app_client_shared_chunks,
app_entry::AppEntry, app_entry::AppEntry,
app_page_entry::get_app_page_entry, app_page_entry::get_app_page_entry,
......
...@@ -8,6 +8,6 @@ pub use ecmascript_client_reference::{ ...@@ -8,6 +8,6 @@ pub use ecmascript_client_reference::{
ecmascript_client_reference_transition::NextEcmascriptClientReferenceTransition, ecmascript_client_reference_transition::NextEcmascriptClientReferenceTransition,
}; };
pub use visit_client_reference::{ pub use visit_client_reference::{
ClientReference, ClientReferenceGraph, ClientReferenceType, ClientReferenceTypes, ClientReference, ClientReferenceGraph, ClientReferenceGraphResult, ClientReferenceType,
ClientReferences, ClientReferenceTypes,
}; };
...@@ -30,12 +30,12 @@ pub struct ClientReference { ...@@ -30,12 +30,12 @@ pub struct ClientReference {
} }
impl ClientReference { impl ClientReference {
pub fn server_component(&self) -> Option<&Vc<NextServerComponentModule>> { pub fn server_component(&self) -> Option<Vc<NextServerComponentModule>> {
self.server_component.as_ref() self.server_component
} }
pub fn ty(&self) -> &ClientReferenceType { pub fn ty(&self) -> ClientReferenceType {
&self.ty self.ty
} }
} }
...@@ -47,8 +47,11 @@ pub enum ClientReferenceType { ...@@ -47,8 +47,11 @@ pub enum ClientReferenceType {
CssClientReference(Vc<CssClientReferenceModule>), CssClientReference(Vc<CssClientReferenceModule>),
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value]
pub struct ClientReferences(Vec<ClientReference>); pub struct ClientReferenceGraphResult {
pub client_references: Vec<ClientReference>,
pub server_component_entries: Vec<Vc<NextServerComponentModule>>,
}
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct ClientReferenceTypes(IndexSet<ClientReferenceType>); pub struct ClientReferenceTypes(IndexSet<ClientReferenceType>);
...@@ -101,12 +104,13 @@ impl ClientReferenceGraph { ...@@ -101,12 +104,13 @@ impl ClientReferenceGraph {
for node in this.graph.reverse_topological() { for node in this.graph.reverse_topological() {
match &node.ty { match &node.ty {
VisitClientReferenceNodeType::Internal(_asset, _) => { VisitClientReferenceNodeType::Internal(..)
| VisitClientReferenceNodeType::ServerComponentEntry(..) => {
// No-op. These nodes are only useful during graph // No-op. These nodes are only useful during graph
// traversal. // traversal.
} }
VisitClientReferenceNodeType::ClientReference(client_reference, _) => { VisitClientReferenceNodeType::ClientReference(client_reference, _) => {
client_reference_types.insert(*client_reference.ty()); client_reference_types.insert(client_reference.ty());
} }
} }
} }
...@@ -115,9 +119,13 @@ impl ClientReferenceGraph { ...@@ -115,9 +119,13 @@ impl ClientReferenceGraph {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn entry(self: Vc<Self>, entry: Vc<Box<dyn Module>>) -> Result<Vc<ClientReferences>> { pub async fn entry(
self: Vc<Self>,
entry: Vc<Box<dyn Module>>,
) -> Result<Vc<ClientReferenceGraphResult>> {
let this = self.await?; let this = self.await?;
let mut entry_client_references = vec![]; let mut client_references = vec![];
let mut server_component_entries = vec![];
for node in this for node in this
.graph .graph
...@@ -132,12 +140,19 @@ impl ClientReferenceGraph { ...@@ -132,12 +140,19 @@ impl ClientReferenceGraph {
// traversal. // traversal.
} }
VisitClientReferenceNodeType::ClientReference(client_reference, _) => { VisitClientReferenceNodeType::ClientReference(client_reference, _) => {
entry_client_references.push(*client_reference); client_references.push(*client_reference);
}
VisitClientReferenceNodeType::ServerComponentEntry(server_component, _) => {
server_component_entries.push(*server_component);
} }
} }
} }
Ok(Vc::cell(entry_client_references)) Ok(ClientReferenceGraphResult {
client_references,
server_component_entries,
}
.cell())
} }
} }
...@@ -156,6 +171,7 @@ struct VisitClientReferenceNode { ...@@ -156,6 +171,7 @@ struct VisitClientReferenceNode {
)] )]
enum VisitClientReferenceNodeType { enum VisitClientReferenceNodeType {
ClientReference(ClientReference, ReadRef<String>), ClientReference(ClientReference, ReadRef<String>),
ServerComponentEntry(Vc<NextServerComponentModule>, ReadRef<String>),
Internal(Vc<Box<dyn Module>>, ReadRef<String>), Internal(Vc<Box<dyn Module>>, ReadRef<String>),
} }
...@@ -168,24 +184,29 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference { ...@@ -168,24 +184,29 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
match edge.ty { match edge.ty {
VisitClientReferenceNodeType::ClientReference(..) => VisitControlFlow::Skip(edge), VisitClientReferenceNodeType::ClientReference(..) => VisitControlFlow::Skip(edge),
VisitClientReferenceNodeType::Internal(..) => VisitControlFlow::Continue(edge), VisitClientReferenceNodeType::Internal(..) => VisitControlFlow::Continue(edge),
VisitClientReferenceNodeType::ServerComponentEntry(..) => {
VisitControlFlow::Continue(edge)
}
} }
} }
fn edges(&mut self, node: &VisitClientReferenceNode) -> Self::EdgesFuture { fn edges(&mut self, node: &VisitClientReferenceNode) -> Self::EdgesFuture {
let node = node.clone(); let node = node.clone();
async move { async move {
match node.ty { let module = match node.ty {
// This should never occur since we always skip visiting these // This should never occur since we always skip visiting these
// nodes' edges. // nodes' edges.
VisitClientReferenceNodeType::ClientReference(..) => Ok(vec![]), VisitClientReferenceNodeType::ClientReference(..) => return Ok(vec![]),
VisitClientReferenceNodeType::Internal(module, _) => { VisitClientReferenceNodeType::Internal(module, _) => module,
VisitClientReferenceNodeType::ServerComponentEntry(module, _) => Vc::upcast(module),
};
let referenced_modules = primary_referenced_modules(module).await?; let referenced_modules = primary_referenced_modules(module).await?;
let referenced_modules = referenced_modules.iter().map(|module| async move { let referenced_modules = referenced_modules.iter().map(|module| async move {
let module = module.resolve().await?; let module = module.resolve().await?;
if let Some(client_reference_module) = if let Some(client_reference_module) =
Vc::try_resolve_downcast_type::<EcmascriptClientReferenceModule>(module) Vc::try_resolve_downcast_type::<EcmascriptClientReferenceModule>(module).await?
.await?
{ {
return Ok(VisitClientReferenceNode { return Ok(VisitClientReferenceNode {
server_component: node.server_component, server_component: node.server_component,
...@@ -202,8 +223,7 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference { ...@@ -202,8 +223,7 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
} }
if let Some(css_client_reference_asset) = if let Some(css_client_reference_asset) =
Vc::try_resolve_downcast_type::<CssClientReferenceModule>(module) Vc::try_resolve_downcast_type::<CssClientReferenceModule>(module).await?
.await?
{ {
return Ok(VisitClientReferenceNode { return Ok(VisitClientReferenceNode {
server_component: node.server_component, server_component: node.server_component,
...@@ -220,14 +240,13 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference { ...@@ -220,14 +240,13 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
} }
if let Some(server_component_asset) = if let Some(server_component_asset) =
Vc::try_resolve_downcast_type::<NextServerComponentModule>(module) Vc::try_resolve_downcast_type::<NextServerComponentModule>(module).await?
.await?
{ {
return Ok(VisitClientReferenceNode { return Ok(VisitClientReferenceNode {
server_component: Some(server_component_asset), server_component: Some(server_component_asset),
ty: VisitClientReferenceNodeType::Internal( ty: VisitClientReferenceNodeType::ServerComponentEntry(
module, server_component_asset,
module.ident().to_string().await?, server_component_asset.ident().to_string().await?,
), ),
}); });
} }
...@@ -246,8 +265,6 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference { ...@@ -246,8 +265,6 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
Ok(assets) Ok(assets)
} }
} }
}
}
fn span(&mut self, node: &VisitClientReferenceNode) -> tracing::Span { fn span(&mut self, node: &VisitClientReferenceNode) -> tracing::Span {
match &node.ty { match &node.ty {
...@@ -257,6 +274,9 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference { ...@@ -257,6 +274,9 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
VisitClientReferenceNodeType::Internal(_, name) => { VisitClientReferenceNodeType::Internal(_, name) => {
tracing::info_span!("module", name = **name) tracing::info_span!("module", name = **name)
} }
VisitClientReferenceNodeType::ServerComponentEntry(_, name) => {
tracing::info_span!("layout segment", name = **name)
}
} }
} }
} }
...@@ -4,6 +4,7 @@ pub(crate) mod client_reference_manifest; ...@@ -4,6 +4,7 @@ pub(crate) mod client_reference_manifest;
use std::collections::HashMap; use std::collections::HashMap;
use indexmap::IndexSet;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput}; use turbo_tasks::{trace::TraceRawVcs, TaskInput};
...@@ -236,10 +237,10 @@ pub struct ClientReferenceManifest { ...@@ -236,10 +237,10 @@ pub struct ClientReferenceManifest {
pub edge_ssr_module_mapping: HashMap<ModuleId, ManifestNode>, pub edge_ssr_module_mapping: HashMap<ModuleId, ManifestNode>,
/// Mapping of server component path to required CSS client chunks. /// Mapping of server component path to required CSS client chunks.
#[serde(rename = "entryCSSFiles")] #[serde(rename = "entryCSSFiles")]
pub entry_css_files: HashMap<String, Vec<String>>, pub entry_css_files: HashMap<String, IndexSet<String>>,
/// Mapping of server component path to required JS client chunks. /// Mapping of server component path to required JS client chunks.
#[serde(rename = "entryJSFiles")] #[serde(rename = "entryJSFiles")]
pub entry_js_files: HashMap<String, Vec<String>>, pub entry_js_files: HashMap<String, IndexSet<String>>,
} }
#[derive(Serialize, Default, Debug)] #[derive(Serialize, Default, Debug)]
......
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