Unverified Commit baf4401c authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix client reference order for shared references (#84760)

At first, we performed one naive traversal per page to collect all client references. That had the problem that references used by multiple layout segments were attributed only to the first layout segment (e.g. only on `not-found` and not on `page`). Because the traversal doesn't revisit nodes.

That particular problem was fixed by https://github.com/vercel/next.js/pull/81704. But it didn't consider the fact that the order of client references isn't correctly modelled by having a `client reference -> vec<server components>` mapping to fixup whatever the graph traversal missed.

In this particular case, layout segment B had a unique client reference and one shared with layout segment A. So the fixup from above would then make B have `unique reference, shared reference` (because they were just added at the end), even though the real order (according to the graph is the reverse).

Instead, traverse once per layout segment, which doesn't have this problem.
parent e4e6df78
...@@ -3,21 +3,13 @@ use next_core::{ ...@@ -3,21 +3,13 @@ use next_core::{
next_client_reference::{CssClientReferenceModule, EcmascriptClientReferenceModule}, next_client_reference::{CssClientReferenceModule, EcmascriptClientReferenceModule},
next_server_component::server_component_module::NextServerComponentModule, next_server_component::server_component_module::NextServerComponentModule,
}; };
use roaring::RoaringBitmap;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Vc, debug::ValueDebugFormat, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
trace::TraceRawVcs,
}; };
use turbopack::css::chunk::CssChunkPlaceable; use turbopack::css::chunk::CssChunkPlaceable;
use turbopack_core::{ use turbopack_core::{module::Module, module_graph::SingleModuleGraph};
module::Module,
module_graph::{
GraphTraversalAction, SingleModuleGraph, SingleModuleGraphModuleNode,
chunk_group_info::RoaringBitmapWrapper,
},
};
#[derive( #[derive(
Copy, Clone, Serialize, Deserialize, Eq, PartialEq, TraceRawVcs, ValueDebugFormat, NonLocalValue, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, TraceRawVcs, ValueDebugFormat, NonLocalValue,
...@@ -31,42 +23,14 @@ pub enum ClientManifestEntryType { ...@@ -31,42 +23,14 @@ pub enum ClientManifestEntryType {
ServerComponent(ResolvedVc<NextServerComponentModule>), ServerComponent(ResolvedVc<NextServerComponentModule>),
} }
/// Tracks information about all the css and js client references in the graph as well as how server /// Tracks information about all the css and js client references in the graph.
/// components depend on them. #[turbo_tasks::value(transparent)]
#[turbo_tasks::value] pub struct ClientReferenceData(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientManifestEntryType>);
pub struct ClientReferenceManifest {
pub manifest: FxHashMap<ResolvedVc<Box<dyn Module>>, ClientManifestEntryType>,
// All the server components in the graph.
server_components: FxIndexSet<ResolvedVc<NextServerComponentModule>>,
// All the server components that depend on each module
// This only includes mappings for modules with client references and the bitmaps reference
// indices into `[server_components]`
server_components_for_client_references:
FxHashMap<ResolvedVc<Box<dyn Module>>, RoaringBitmapWrapper>,
}
impl ClientReferenceManifest {
/// Returns all the server components that depend on the given client reference
pub fn server_components_for_client_reference(
&self,
module: ResolvedVc<Box<dyn Module>>,
) -> impl Iterator<Item = ResolvedVc<NextServerComponentModule>> {
let bitmap = &self
.server_components_for_client_references
.get(&module)
.expect("Module should be a client reference module")
.0;
bitmap
.iter()
.map(|index| *self.server_components.get_index(index as usize).unwrap())
}
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn map_client_references( pub async fn map_client_references(
graph: Vc<SingleModuleGraph>, graph: Vc<SingleModuleGraph>,
) -> Result<Vc<ClientReferenceManifest>> { ) -> Result<Vc<ClientReferenceData>> {
let graph = graph.await?; let graph = graph.await?;
let manifest = graph let manifest = graph
.iter_nodes() .iter_nodes()
...@@ -108,96 +72,5 @@ pub async fn map_client_references( ...@@ -108,96 +72,5 @@ pub async fn map_client_references(
.into_iter() .into_iter()
.collect::<FxHashMap<_, _>>(); .collect::<FxHashMap<_, _>>();
let mut server_components = FxIndexSet::default(); Ok(Vc::cell(manifest))
let mut module_to_server_component_bits = FxHashMap::default();
if !manifest.is_empty() {
graph.traverse_edges_from_entries_fixed_point(
graph.entry_modules(),
|parent_info, node| {
let module = node.module();
let module_type = manifest.get(&module);
let mut should_visit_children = match module_to_server_component_bits.entry(module)
{
std::collections::hash_map::Entry::Occupied(_) => false,
std::collections::hash_map::Entry::Vacant(vacant_entry) => {
// only do this the first time we visit the node.
let bits = vacant_entry.insert(RoaringBitmap::new());
if let Some(ClientManifestEntryType::ServerComponent(
server_component_module,
)) = module_type
{
let index = server_components.insert_full(*server_component_module).0;
bits.insert(index.try_into().unwrap());
}
true
}
};
if let Some((SingleModuleGraphModuleNode{module: parent_module}, _)) = parent_info
// Skip self cycles such as in
// test/e2e/app-dir/dynamic-import/app/page.tsx where a very-dynamic import induces a
// self cycle. They don't introduce new bits anyway.
&& module != *parent_module
{
// Copy parent bits down. `traverse_edges_from_entries_fixed_point` always
// visits parents before children so we can simply assert
// that the parent it set.
let [Some(current), Some(parent)] =
module_to_server_component_bits.get_disjoint_mut([&module, parent_module])
else {
unreachable!()
};
// Check if we are adding new bits and thus need to revisit children unless we
// are already planning to because this is a new node.
if !should_visit_children {
let len = current.len();
*current |= &*parent;
// did we find new bits? If so visit the children again
should_visit_children = len != current.len();
} else {
*current |= &*parent;
}
}
Ok(match module_type {
Some(
ClientManifestEntryType::EcmascriptClientReference { .. }
| ClientManifestEntryType::CssClientReference { .. },
) => {
// No need to explore these subgraphs ever, these are the leaves in the
// server component graph
GraphTraversalAction::Skip
}
// Continue on server components and through graphs of non-ClientReference
// modules, but only if our set of parent components has changed.
_ => {
if should_visit_children {
GraphTraversalAction::Continue
} else {
GraphTraversalAction::Skip
}
}
})
},
)?;
}
// Filter down to just the client reference modules to reduce datastructure size
let server_components_for_client_references = module_to_server_component_bits
.into_iter()
.filter_map(|(k, v)| match manifest.get(&k) {
Some(
ClientManifestEntryType::CssClientReference(_)
| ClientManifestEntryType::EcmascriptClientReference { .. },
) => Some((k, RoaringBitmapWrapper(v))),
_ => None,
})
.collect();
Ok(ClientReferenceManifest {
manifest,
server_components,
server_components_for_client_references,
}
.cell())
} }
This diff is collapsed.
...@@ -53,6 +53,9 @@ impl ClientReference { ...@@ -53,6 +53,9 @@ impl ClientReference {
} }
} }
#[turbo_tasks::value(transparent)]
pub struct ClientReferences(Vec<ClientReference>);
#[derive( #[derive(
Copy, Copy,
Clone, Clone,
......
import './red.css'
export default function GlobalNotFound() {
return (
<html lang="en" suppressHydrationWarning>
<body>
<span>not found</span>
</body>
</html>
)
}
import './red.css'
import './green.css'
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body style={{ fontFamily: 'var(--font-sans)' }}>{children}</body>
</html>
)
}
export default function Page() {
return 'abc'
}
import { nextTestSetup } from 'e2e-utils'
describe('initial-css-order', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should serve styles in the correct order for the page', async () => {
const browser = await next.browser('/')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
)
).toBe('rgb(0, 128, 0)')
})
it('should serve styles in the correct order for global-not-found', async () => {
const browser = await next.browser('/404')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
)
).toBe('rgb(255, 0, 0)')
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
globalNotFound: true,
},
}
module.exports = nextConfig
...@@ -443,6 +443,58 @@ impl SingleModuleGraph { ...@@ -443,6 +443,58 @@ impl SingleModuleGraph {
Ok(()) Ok(())
} }
/// Traverses all reachable nodes once
pub fn traverse_nodes_from_entries<'a, S>(
&'a self,
entries: impl IntoIterator<Item = ResolvedVc<Box<dyn Module>>>,
state: &mut S,
visit_preorder: impl Fn(&'a SingleModuleGraphModuleNode, &mut S) -> Result<GraphTraversalAction>,
mut visit_postorder: impl FnMut(&'a SingleModuleGraphModuleNode, &mut S) -> Result<()>,
) -> Result<()> {
let graph = &self.graph;
let entries = entries.into_iter().map(|e| self.get_module(e).unwrap());
enum Pass {
Visit,
ExpandAndVisit,
}
let mut stack: Vec<(Pass, NodeIndex)> =
entries.map(|e| (Pass::ExpandAndVisit, e)).collect();
let mut expanded = FxHashSet::default();
while let Some((pass, current)) = stack.pop() {
match pass {
Pass::Visit => {
if let SingleModuleGraphNode::Module(current_node) =
graph.node_weight(current).unwrap()
{
visit_postorder(current_node, state)?;
}
}
Pass::ExpandAndVisit => {
if expanded.insert(current)
&& let SingleModuleGraphNode::Module(current_node) =
graph.node_weight(current).unwrap()
{
let action = visit_preorder(current_node, state)?;
if action == GraphTraversalAction::Exclude {
continue;
}
stack.push((Pass::Visit, current));
if action == GraphTraversalAction::Continue {
stack.extend(
iter_neighbors_rev(graph, current)
.map(|(_, child)| (Pass::ExpandAndVisit, child)),
);
}
}
}
}
}
Ok(())
}
/// Traverses all reachable edges exactly once and calls the visitor with the edge source and /// Traverses all reachable edges exactly once and calls the visitor with the edge source and
/// target. /// target.
/// ///
......
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