Skip to content

Commit bb65e78

Browse files
authored
Turbopack: avoid embedding deployment ID into the turbopack runtime (#86370)
### What? * add support for multiple options for chunk suffix * Use FromScriptSrc chunk suffix to avoid depending on deployment id * fix passing chunk suffix to WebWorkers initial chunks
1 parent f0af7cd commit bb65e78

17 files changed

+96
-55
lines changed

crates/next-api/src/project.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,6 @@ impl Project {
11441144
client_root: self.client_relative_path().owned().await?,
11451145
client_root_to_root_path: rcstr!("/ROOT"),
11461146
asset_prefix: self.next_config().computed_asset_prefix(),
1147-
chunk_suffix_path: self.next_config().chunk_suffix_path(),
11481147
environment: self.client_compile_time_info().environment(),
11491148
module_id_strategy: self.module_ids(),
11501149
export_usage: self.export_usage(),

crates/next-core/src/next_client/context.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use turbopack::{
1414
resolve_options_context::ResolveOptionsContext,
1515
};
1616
use turbopack_browser::{
17-
BrowserChunkingContext, ContentHashing, CurrentChunkMethod,
17+
BrowserChunkingContext, ChunkSuffix, ContentHashing, CurrentChunkMethod,
1818
react_refresh::assert_can_resolve_react_refresh,
1919
};
2020
use turbopack_core::{
@@ -414,7 +414,6 @@ pub struct ClientChunkingContextOptions {
414414
pub client_root: FileSystemPath,
415415
pub client_root_to_root_path: RcStr,
416416
pub asset_prefix: Vc<RcStr>,
417-
pub chunk_suffix_path: Vc<Option<RcStr>>,
418417
pub environment: Vc<Environment>,
419418
pub module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
420419
pub export_usage: Vc<OptionExportUsageInfo>,
@@ -437,7 +436,6 @@ pub async fn get_client_chunking_context(
437436
client_root,
438437
client_root_to_root_path,
439438
asset_prefix,
440-
chunk_suffix_path,
441439
environment,
442440
module_id_strategy,
443441
export_usage,
@@ -452,7 +450,6 @@ pub async fn get_client_chunking_context(
452450

453451
let next_mode = mode.await?;
454452
let asset_prefix = asset_prefix.owned().await?;
455-
let chunk_suffix_path = chunk_suffix_path.to_resolved().await?;
456453
let mut builder = BrowserChunkingContext::builder(
457454
root_path,
458455
client_root.clone(),
@@ -464,7 +461,7 @@ pub async fn get_client_chunking_context(
464461
next_mode.runtime_type(),
465462
)
466463
.chunk_base_path(Some(asset_prefix.clone()))
467-
.chunk_suffix_path(chunk_suffix_path)
464+
.chunk_suffix(ChunkSuffix::FromScriptSrc.resolved_cell())
468465
.minify_type(if *minify.await? {
469466
MinifyType::Minify {
470467
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),

turbopack/crates/turbopack-browser/src/chunking_context.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use turbopack_ecmascript::{
3333
chunk::EcmascriptChunk,
3434
manifest::{chunk_asset::ManifestAsyncModule, loader_item::ManifestLoaderChunkItem},
3535
};
36-
use turbopack_ecmascript_runtime::RuntimeType;
36+
use turbopack_ecmascript_runtime::{ChunkSuffix, RuntimeType};
3737

3838
use crate::ecmascript::{
3939
chunk::EcmascriptBrowserChunk,
@@ -130,8 +130,8 @@ impl BrowserChunkingContextBuilder {
130130
self
131131
}
132132

133-
pub fn chunk_suffix_path(mut self, chunk_suffix_path: ResolvedVc<Option<RcStr>>) -> Self {
134-
self.chunking_context.chunk_suffix_path = Some(chunk_suffix_path);
133+
pub fn chunk_suffix(mut self, chunk_suffix: ResolvedVc<ChunkSuffix>) -> Self {
134+
self.chunking_context.chunk_suffix = Some(chunk_suffix);
135135
self
136136
}
137137

@@ -253,9 +253,9 @@ pub struct BrowserChunkingContext {
253253
/// Base path that will be prepended to all chunk URLs when loading them.
254254
/// This path will not appear in chunk paths or chunk data.
255255
chunk_base_path: Option<RcStr>,
256-
/// Suffix path that will be appended to all chunk URLs when loading them.
256+
/// Suffix that will be appended to all chunk URLs when loading them.
257257
/// This path will not appear in chunk paths or chunk data.
258-
chunk_suffix_path: Option<ResolvedVc<Option<RcStr>>>,
258+
chunk_suffix: Option<ResolvedVc<ChunkSuffix>>,
259259
/// URL prefix that will be prepended to all static asset URLs when loading
260260
/// them.
261261
asset_base_path: Option<RcStr>,
@@ -322,7 +322,7 @@ impl BrowserChunkingContext {
322322
asset_root_path,
323323
asset_root_paths: Default::default(),
324324
chunk_base_path: None,
325-
chunk_suffix_path: None,
325+
chunk_suffix: None,
326326
asset_base_path: None,
327327
asset_base_paths: Default::default(),
328328
enable_hot_module_replacement: false,
@@ -425,11 +425,11 @@ impl BrowserChunkingContext {
425425

426426
/// Returns the asset suffix path.
427427
#[turbo_tasks::function]
428-
pub fn chunk_suffix_path(&self) -> Vc<Option<RcStr>> {
429-
if let Some(chunk_suffix_path) = self.chunk_suffix_path {
430-
*chunk_suffix_path
428+
pub fn chunk_suffix(&self) -> Vc<ChunkSuffix> {
429+
if let Some(chunk_suffix) = self.chunk_suffix {
430+
*chunk_suffix
431431
} else {
432-
Vc::cell(None)
432+
ChunkSuffix::None.cell()
433433
}
434434
}
435435

turbopack/crates/turbopack-browser/src/ecmascript/evaluate/chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl EcmascriptBrowserEvaluateChunk {
173173
let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code(
174174
environment,
175175
this.chunking_context.chunk_base_path(),
176-
this.chunking_context.chunk_suffix_path(),
176+
this.chunking_context.chunk_suffix(),
177177
runtime_type,
178178
output_root_to_root_path,
179179
source_maps,

turbopack/crates/turbopack-browser/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pub mod react_refresh;
1010
pub use chunking_context::{
1111
BrowserChunkingContext, BrowserChunkingContextBuilder, ContentHashing, CurrentChunkMethod,
1212
};
13+
pub use turbopack_ecmascript_runtime::ChunkSuffix;

turbopack/crates/turbopack-ecmascript-runtime/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ turbo-tasks-fs = { workspace = true }
2929
turbopack = { workspace = true }
3030
turbopack-core = { workspace = true }
3131
turbopack-ecmascript = { workspace = true }
32-

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/runtime-base.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
// Used in WebWorkers to tell the runtime about the chunk base path
1515
declare var TURBOPACK_WORKER_LOCATION: string
16+
// Used in WebWorkers to tell the runtime about the chunk suffix
17+
declare var TURBOPACK_CHUNK_SUFFIX: string
1618
// Used in WebWorkers to tell the runtime about the current chunk url since it can't be detected via document.currentScript
1719
// Note it's stored in reversed order to use push and pop
1820
declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined
1921

2022
// Injected by rust code
2123
declare var CHUNK_BASE_PATH: string
22-
declare var CHUNK_SUFFIX_PATH: string
24+
declare var CHUNK_SUFFIX: string
2325

2426
interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
2527
R: ResolvePathFromModule
@@ -322,8 +324,9 @@ function getWorkerBlobURL(chunks: ChunkPath[]): string {
322324
// It is important to reverse the array so when bootstrapping we can infer what chunk is being
323325
// evaluated by poping urls off of this array. See `getPathFromScript`
324326
let bootstrap = `self.TURBOPACK_WORKER_LOCATION = ${JSON.stringify(location.origin)};
327+
self.TURBOPACK_CHUNK_SUFFIX = ${JSON.stringify(CHUNK_SUFFIX)};
325328
self.TURBOPACK_NEXT_CHUNK_URLS = ${JSON.stringify(chunks.reverse().map(getChunkRelativeUrl), null, 2)};
326-
importScripts(...self.TURBOPACK_NEXT_CHUNK_URLS.map(c => self.TURBOPACK_WORKER_LOCATION + c).reverse());`
329+
importScripts(...self.TURBOPACK_NEXT_CHUNK_URLS.map(c => self.TURBOPACK_WORKER_LOCATION + c + self.TURBOPACK_CHUNK_SUFFIX).reverse());`
327330
let blob = new Blob([bootstrap], { type: 'text/javascript' })
328331
return URL.createObjectURL(blob)
329332
}
@@ -345,7 +348,7 @@ function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl {
345348
return `${CHUNK_BASE_PATH}${chunkPath
346349
.split('/')
347350
.map((p) => encodeURIComponent(p))
348-
.join('/')}${CHUNK_SUFFIX_PATH}` as ChunkUrl
351+
.join('/')}${CHUNK_SUFFIX}` as ChunkUrl
349352
}
350353

351354
/**

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/dev-backend-dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ let DEV_BACKEND: DevRuntimeBackend
111111

112112
function _eval({ code, url, map }: EcmascriptModuleEntry): ModuleFactory {
113113
code += `\n\n//# sourceURL=${encodeURI(
114-
location.origin + CHUNK_BASE_PATH + url + CHUNK_SUFFIX_PATH
114+
location.origin + CHUNK_BASE_PATH + url + CHUNK_SUFFIX
115115
)}`
116116
if (map) {
117117
code += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(

turbopack/crates/turbopack-ecmascript-runtime/src/browser_runtime.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ use turbopack_core::{
1111
};
1212
use turbopack_ecmascript::utils::StringifyJs;
1313

14-
use crate::{RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code};
14+
use crate::{
15+
ChunkSuffix, RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code,
16+
};
1517

1618
/// Returns the code for the ECMAScript runtime.
1719
#[turbo_tasks::function]
1820
pub async fn get_browser_runtime_code(
1921
environment: ResolvedVc<Environment>,
2022
chunk_base_path: Vc<Option<RcStr>>,
21-
chunk_suffix_path: Vc<Option<RcStr>>,
23+
chunk_suffix: Vc<ChunkSuffix>,
2224
runtime_type: RuntimeType,
2325
output_root_to_root_path: RcStr,
2426
generate_source_map: bool,
@@ -80,10 +82,7 @@ pub async fn get_browser_runtime_code(
8082
let relative_root_path = output_root_to_root_path;
8183
let chunk_base_path = chunk_base_path.await?;
8284
let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
83-
let chunk_suffix_path = chunk_suffix_path.await?;
84-
let chunk_suffix_path = chunk_suffix_path
85-
.as_ref()
86-
.map_or_else(|| "", |f| f.as_str());
85+
let chunk_suffix = chunk_suffix.await?;
8786

8887
writedoc!(
8988
code,
@@ -94,16 +93,42 @@ pub async fn get_browser_runtime_code(
9493
}}
9594
9695
const CHUNK_BASE_PATH = {};
97-
const CHUNK_SUFFIX_PATH = {};
9896
const RELATIVE_ROOT_PATH = {};
9997
const RUNTIME_PUBLIC_PATH = {};
10098
"#,
10199
StringifyJs(chunk_base_path),
102-
StringifyJs(chunk_suffix_path),
103100
StringifyJs(relative_root_path.as_str()),
104101
StringifyJs(chunk_base_path),
105102
)?;
106103

104+
match &*chunk_suffix {
105+
ChunkSuffix::None => {
106+
writedoc!(
107+
code,
108+
r#"
109+
const CHUNK_SUFFIX = "";
110+
"#
111+
)?;
112+
}
113+
ChunkSuffix::Constant(suffix) => {
114+
writedoc!(
115+
code,
116+
r#"
117+
const CHUNK_SUFFIX = {};
118+
"#,
119+
StringifyJs(suffix.as_str())
120+
)?;
121+
}
122+
ChunkSuffix::FromScriptSrc => {
123+
writedoc!(
124+
code,
125+
r#"
126+
const CHUNK_SUFFIX = (self.TURBOPACK_CHUNK_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, "")) || "";
127+
"#
128+
)?;
129+
}
130+
}
131+
107132
code.push_code(&*shared_runtime_utils_code.await?);
108133
for runtime_code in runtime_base_code {
109134
code.push_code(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use turbo_rcstr::RcStr;
2+
3+
#[turbo_tasks::value(shared)]
4+
pub enum ChunkSuffix {
5+
/// No suffix.
6+
None,
7+
/// A constant suffix to append to chunk URLs.
8+
Constant(RcStr),
9+
/// Use the query string of the `src` attribute of the current script tag as a suffix for chunk
10+
/// loading.
11+
FromScriptSrc,
12+
}

0 commit comments

Comments
 (0)