Commit b45c24af authored by Luke Sandberg's avatar Luke Sandberg

Turbopack: Sort chunk items by module ID for deterministic content hashes

Different routes perform independent DFS traversals from different entry
points, producing different topological orderings of shared modules.
Since production chunk filenames are derived from content hashes of the
serialized JS bytes, this ordering instability causes the same set of
modules to produce chunks with different filenames across routes,
leading to duplicate downloads.

Fix by sorting chunk items by ModuleId before serialization in both
browser and Node.js chunk content emitters. This ensures identical
module sets always produce identical content hashes regardless of
traversal order.
parent 57381e7e
...@@ -115,13 +115,19 @@ impl EcmascriptBrowserChunkContent { ...@@ -115,13 +115,19 @@ impl EcmascriptBrowserChunkContent {
let content = this.content.await?; let content = this.content.await?;
let chunk_items = content.chunk_item_code_and_ids().await?; let chunk_items = content.chunk_item_code_and_ids().await?;
for item in chunk_items {
for (id, item_code) in item { // Sort chunk items by module ID to ensure deterministic output regardless of
write!(code, "\n{}, ", StringifyJs(&id))?; // the order modules were discovered during graph traversal. Without this,
// the same set of modules can produce different content hashes when reached
// from different entry points, causing duplicate chunk downloads.
let mut all_items: Vec<_> = chunk_items.iter().flat_map(|item| item.iter()).collect();
all_items.sort_by(|(id_a, _), (id_b, _)| id_a.cmp(id_b));
for (id, item_code) in all_items {
write!(code, "\n{}, ", StringifyJs(id))?;
code.push_code(item_code); code.push_code(item_code);
write!(code, ",")?; write!(code, ",")?;
} }
}
write!(code, "\n]);")?; write!(code, "\n]);")?;
......
...@@ -60,13 +60,19 @@ impl EcmascriptBuildNodeChunkContent { ...@@ -60,13 +60,19 @@ impl EcmascriptBuildNodeChunkContent {
let content = self.content.await?; let content = self.content.await?;
let chunk_items = content.chunk_item_code_and_ids().await?; let chunk_items = content.chunk_item_code_and_ids().await?;
for item in chunk_items {
for (id, item_code) in item { // Sort chunk items by module ID to ensure deterministic output regardless of
write!(code, "\n{}, ", StringifyJs(&id))?; // the order modules were discovered during graph traversal. Without this,
// the same set of modules can produce different content hashes when reached
// from different entry points, causing duplicate chunk downloads.
let mut all_items: Vec<_> = chunk_items.iter().flat_map(|item| item.iter()).collect();
all_items.sort_by(|(id_a, _), (id_b, _)| id_a.cmp(id_b));
for (id, item_code) in all_items {
write!(code, "\n{}, ", StringifyJs(id))?;
code.push_code(item_code); code.push_code(item_code);
write!(code, ",")?; write!(code, ",")?;
} }
}
write!(code, "\n];")?; write!(code, "\n];")?;
......
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