Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
19fc57d9
Commit
19fc57d9
authored
Oct 21, 2025
by
Tim Neutkens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Turbopack: Implement SharedWorker
parent
899b473e
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
184 additions
and
8 deletions
+184
-8
page.js
test/e2e/app-dir/worker/app/shared-bundled/page.js
+26
-0
page.js
test/e2e/app-dir/worker/app/shared-unbundled/page.js
+23
-0
shared-worker.ts
test/e2e/app-dir/worker/app/shared-worker.ts
+16
-0
unbundled-shared-worker.js
test/e2e/app-dir/worker/public/unbundled-shared-worker.js
+6
-0
worker.test.ts
test/e2e/app-dir/worker/worker.test.ts
+30
-0
imports.rs
...opack/crates/turbopack-ecmascript/src/analyzer/imports.rs
+3
-1
mod.rs
turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs
+11
-0
mod.rs
turbopack/crates/turbopack-ecmascript/src/references/mod.rs
+44
-1
worker.rs
...pack/crates/turbopack-ecmascript/src/references/worker.rs
+12
-4
chunk_item.rs
...rates/turbopack-ecmascript/src/worker_chunk/chunk_item.rs
+3
-0
module.rs
...ck/crates/turbopack-ecmascript/src/worker_chunk/module.rs
+10
-2
No files found.
test/e2e/app-dir/worker/app/shared-bundled/page.js
0 → 100644
View file @
19fc57d9
'use client'
import
{
useState
}
from
'react'
export
default
function
SharedWorkerBundledPage
()
{
const
[
state
,
setState
]
=
useState
(
'default'
)
return
(
<
div
>
<
button
onClick
=
{()
=>
{
// This should be bundled by turbopack since it's a relative import
const
worker
=
new
SharedWorker
(
new
URL
(
'../shared-worker'
,
import
.
meta
.
url
)
)
worker
.
port
.
addEventListener
(
'message'
,
(
event
)
=>
{
setState
(
event
.
data
)
})
worker
.
port
.
start
()
}}
>
Get
shared
worker
data
(
bundled
)
<
/button
>
<
p
>
SharedWorker
bundled
state
:
<
/p
>
<
p
id
=
"shared-worker-bundled-state"
>
{
state
}
<
/p
>
<
/div
>
)
}
test/e2e/app-dir/worker/app/shared-unbundled/page.js
0 → 100644
View file @
19fc57d9
'use client'
import
{
useState
}
from
'react'
export
default
function
SharedWorkerPage
()
{
const
[
state
,
setState
]
=
useState
(
'default'
)
return
(
<
div
>
<
button
onClick
=
{()
=>
{
const
worker
=
new
SharedWorker
(
'/unbundled-shared-worker.js'
)
worker
.
port
.
addEventListener
(
'message'
,
(
event
)
=>
{
setState
(
event
.
data
)
})
worker
.
port
.
start
()
}}
>
Get
shared
worker
data
<
/button
>
<
p
>
SharedWorker
state
:
<
/p
>
<
p
id
=
"shared-worker-state"
>
{
state
}
<
/p
>
<
/div
>
)
}
test/e2e/app-dir/worker/app/shared-worker.ts
0 → 100644
View file @
19fc57d9
// SharedWorker script - handles multiple connections
self
.
addEventListener
(
'connect'
,
(
event
:
MessageEvent
)
=>
{
const
port
=
event
.
ports
[
0
]
// Import the dependency and send the message
import
(
'./worker-dep'
)
.
then
((
mod
)
=>
{
port
.
postMessage
(
'shared-worker.ts:'
+
mod
.
default
)
})
.
catch
((
error
)
=>
{
console
.
error
(
'SharedWorker import error:'
,
error
)
port
.
postMessage
(
'error: '
+
error
.
message
)
})
port
.
start
()
})
test/e2e/app-dir/worker/public/unbundled-shared-worker.js
0 → 100644
View file @
19fc57d9
// SharedWorker script for unbundled test
self
.
addEventListener
(
'connect'
,
(
event
)
=>
{
const
port
=
event
.
ports
[
0
]
port
.
postMessage
(
'unbundled-shared-worker'
)
port
.
start
()
})
test/e2e/app-dir/worker/worker.test.ts
View file @
19fc57d9
...
@@ -49,4 +49,34 @@ describe('app dir - workers', () => {
...
@@ -49,4 +49,34 @@ describe('app dir - workers', () => {
)
)
)
)
})
})
it
(
'should support shared workers with string specifiers'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/shared-unbundled'
)
expect
(
await
browser
.
elementByCss
(
'#shared-worker-state'
).
text
()).
toBe
(
'default'
)
await
browser
.
elementByCss
(
'button'
).
click
()
await
retry
(
async
()
=>
expect
(
await
browser
.
elementByCss
(
'#shared-worker-state'
).
text
()).
toBe
(
'unbundled-shared-worker'
)
)
})
it
(
'should support shared workers with dynamic imports'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/shared-bundled'
)
expect
(
await
browser
.
elementByCss
(
'#shared-worker-bundled-state'
).
text
()
).
toBe
(
'default'
)
await
browser
.
elementByCss
(
'button'
).
click
()
await
retry
(
async
()
=>
expect
(
await
browser
.
elementByCss
(
'#shared-worker-bundled-state'
).
text
()
).
toBe
(
'shared-worker.ts:worker-dep'
)
)
})
})
})
turbopack/crates/turbopack-ecmascript/src/analyzer/imports.rs
View file @
19fc57d9
...
@@ -770,7 +770,9 @@ impl Visit for Analyzer<'_> {
...
@@ -770,7 +770,9 @@ impl Visit for Analyzer<'_> {
// we could actually unwrap thanks to the optimisation above but it can't hurt to be safe...
// we could actually unwrap thanks to the optimisation above but it can't hurt to be safe...
if
let
Some
(
comments
)
=
self
.comments
{
if
let
Some
(
comments
)
=
self
.comments
{
let
callee_span
=
match
&
n
.callee
{
let
callee_span
=
match
&
n
.callee
{
box
Expr
::
Ident
(
Ident
{
sym
,
..
})
if
sym
==
"Worker"
=>
Some
(
n
.span
),
box
Expr
::
Ident
(
Ident
{
sym
,
..
})
if
sym
==
"Worker"
||
sym
==
"SharedWorker"
=>
{
Some
(
n
.span
)
}
_
=>
None
,
_
=>
None
,
};
};
...
...
turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs
View file @
19fc57d9
...
@@ -1958,6 +1958,10 @@ impl JsValue {
...
@@ -1958,6 +1958,10 @@ impl JsValue {
"Worker"
.to_string
(),
"Worker"
.to_string
(),
"The standard Worker constructor: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker"
"The standard Worker constructor: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker"
),
),
WellKnownFunctionKind
::
SharedWorkerConstructor
=>
(
"SharedWorker"
.to_string
(),
"The standard SharedWorker constructor: https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/SharedWorker"
),
WellKnownFunctionKind
::
URLConstructor
=>
(
WellKnownFunctionKind
::
URLConstructor
=>
(
"URL"
.to_string
(),
"URL"
.to_string
(),
"The standard URL constructor: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL"
"The standard URL constructor: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL"
...
@@ -3623,6 +3627,7 @@ pub enum WellKnownFunctionKind {
...
@@ -3623,6 +3627,7 @@ pub enum WellKnownFunctionKind {
NodeResolveFrom
,
NodeResolveFrom
,
NodeProtobufLoad
,
NodeProtobufLoad
,
WorkerConstructor
,
WorkerConstructor
,
SharedWorkerConstructor
,
URLConstructor
,
URLConstructor
,
}
}
...
@@ -3795,6 +3800,12 @@ pub mod test_utils {
...
@@ -3795,6 +3800,12 @@ pub mod test_utils {
true
,
true
,
"ignored Worker constructor"
,
"ignored Worker constructor"
,
),
),
"SharedWorker"
=>
JsValue
::
unknown_if
(
ignore
,
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
SharedWorkerConstructor
),
true
,
"ignored SharedWorker constructor"
,
),
"define"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
Define
),
"define"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
Define
),
"URL"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
URLConstructor
),
"URL"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
URLConstructor
),
"process"
=>
JsValue
::
WellKnownObject
(
WellKnownObjectKind
::
NodeProcess
),
"process"
=>
JsValue
::
WellKnownObject
(
WellKnownObjectKind
::
NodeProcess
),
...
...
turbopack/crates/turbopack-ecmascript/src/references/mod.rs
View file @
19fc57d9
...
@@ -75,7 +75,7 @@ use turbopack_core::{
...
@@ -75,7 +75,7 @@ use turbopack_core::{
issue
::{
IssueExt
,
IssueSeverity
,
IssueSource
,
StyledString
,
analyze
::
AnalyzeIssue
},
issue
::{
IssueExt
,
IssueSeverity
,
IssueSource
,
StyledString
,
analyze
::
AnalyzeIssue
},
module
::
Module
,
module
::
Module
,
reference
::{
ModuleReference
,
ModuleReferences
},
reference
::{
ModuleReference
,
ModuleReferences
},
reference_type
::{
CommonJsReferenceSubType
,
ReferenceType
},
reference_type
::{
CommonJsReferenceSubType
,
ReferenceType
,
WorkerReferenceSubType
},
resolve
::{
resolve
::{
FindContextFileResult
,
ModulePart
,
find_context_file
,
FindContextFileResult
,
ModulePart
,
find_context_file
,
origin
::{
PlainResolveOrigin
,
ResolveOrigin
,
ResolveOriginExt
},
origin
::{
PlainResolveOrigin
,
ResolveOrigin
,
ResolveOriginExt
},
...
@@ -1719,6 +1719,43 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
...
@@ -1719,6 +1719,43 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
WorkerAssetReference
::
new
(
WorkerAssetReference
::
new
(
origin
,
origin
,
Request
::
parse
(
pat
)
.to_resolved
()
.await
?
,
Request
::
parse
(
pat
)
.to_resolved
()
.await
?
,
WorkerReferenceSubType
::
WebWorker
,
issue_source
(
source
,
span
),
in_try
,
),
ast_path
.to_vec
()
.into
(),
);
}
return
Ok
(());
}
// Ignore (e.g. dynamic parameter or string literal), just as Webpack does
return
Ok
(());
}
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
SharedWorkerConstructor
)
=>
{
let
args
=
linked_args
(
args
)
.await
?
;
if
let
Some
(
url
@
JsValue
::
Url
(
_
,
JsValueUrlKind
::
Relative
))
=
args
.first
()
{
let
pat
=
js_value_to_pattern
(
url
);
if
!
pat
.has_constant_parts
()
{
let
(
args
,
hints
)
=
explain_args
(
&
args
);
handler
.span_warn_with_code
(
span
,
&
format!
(
"new SharedWorker({args}) is very dynamic{hints}"
,),
DiagnosticId
::
Lint
(
errors
::
failed_to_analyze
::
ecmascript
::
NEW_WORKER
.to_string
(),
),
);
if
ignore_dynamic_requests
{
return
Ok
(());
}
}
if
*
compile_time_info
.environment
()
.rendering
()
.await
?
==
Rendering
::
Client
{
analysis
.add_reference_code_gen
(
WorkerAssetReference
::
new
(
origin
,
Request
::
parse
(
pat
)
.to_resolved
()
.await
?
,
WorkerReferenceSubType
::
SharedWorker
,
issue_source
(
source
,
span
),
issue_source
(
source
,
span
),
in_try
,
in_try
,
),
),
...
@@ -3149,6 +3186,12 @@ async fn value_visitor_inner(
...
@@ -3149,6 +3186,12 @@ async fn value_visitor_inner(
true
,
true
,
"ignored Worker constructor"
,
"ignored Worker constructor"
,
),
),
"SharedWorker"
=>
JsValue
::
unknown_if
(
ignore
,
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
SharedWorkerConstructor
),
true
,
"ignored SharedWorker constructor"
,
),
"define"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
Define
),
"define"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
Define
),
"URL"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
URLConstructor
),
"URL"
=>
JsValue
::
WellKnownFunction
(
WellKnownFunctionKind
::
URLConstructor
),
"process"
=>
JsValue
::
WellKnownObject
(
WellKnownObjectKind
::
NodeProcess
),
"process"
=>
JsValue
::
WellKnownObject
(
WellKnownObjectKind
::
NodeProcess
),
...
...
turbopack/crates/turbopack-ecmascript/src/references/worker.rs
View file @
19fc57d9
...
@@ -32,6 +32,7 @@ use crate::{
...
@@ -32,6 +32,7 @@ use crate::{
pub
struct
WorkerAssetReference
{
pub
struct
WorkerAssetReference
{
pub
origin
:
ResolvedVc
<
Box
<
dyn
ResolveOrigin
>>
,
pub
origin
:
ResolvedVc
<
Box
<
dyn
ResolveOrigin
>>
,
pub
request
:
ResolvedVc
<
Request
>
,
pub
request
:
ResolvedVc
<
Request
>
,
pub
worker_type
:
WorkerReferenceSubType
,
pub
issue_source
:
IssueSource
,
pub
issue_source
:
IssueSource
,
pub
in_try
:
bool
,
pub
in_try
:
bool
,
}
}
...
@@ -40,12 +41,14 @@ impl WorkerAssetReference {
...
@@ -40,12 +41,14 @@ impl WorkerAssetReference {
pub
fn
new
(
pub
fn
new
(
origin
:
ResolvedVc
<
Box
<
dyn
ResolveOrigin
>>
,
origin
:
ResolvedVc
<
Box
<
dyn
ResolveOrigin
>>
,
request
:
ResolvedVc
<
Request
>
,
request
:
ResolvedVc
<
Request
>
,
worker_type
:
WorkerReferenceSubType
,
issue_source
:
IssueSource
,
issue_source
:
IssueSource
,
in_try
:
bool
,
in_try
:
bool
,
)
->
Self
{
)
->
Self
{
WorkerAssetReference
{
WorkerAssetReference
{
origin
,
origin
,
request
,
request
,
worker_type
,
issue_source
,
issue_source
,
in_try
,
in_try
,
}
}
...
@@ -59,8 +62,7 @@ impl WorkerAssetReference {
...
@@ -59,8 +62,7 @@ impl WorkerAssetReference {
let
module
=
url_resolve
(
let
module
=
url_resolve
(
*
self
.origin
,
*
self
.origin
,
*
self
.request
,
*
self
.request
,
// TODO support more worker types
ReferenceType
::
Worker
(
self
.worker_type
),
ReferenceType
::
Worker
(
WorkerReferenceSubType
::
WebWorker
),
Some
(
self
.issue_source
),
Some
(
self
.issue_source
),
self
.in_try
,
self
.in_try
,
);
);
...
@@ -81,7 +83,7 @@ impl WorkerAssetReference {
...
@@ -81,7 +83,7 @@ impl WorkerAssetReference {
return
Ok
(
None
);
return
Ok
(
None
);
};
};
Ok
(
Some
(
WorkerLoaderModule
::
new
(
*
chunkable
)))
Ok
(
Some
(
WorkerLoaderModule
::
new
(
*
chunkable
,
self
.worker_type
)))
}
}
}
}
...
@@ -103,8 +105,14 @@ impl ModuleReference for WorkerAssetReference {
...
@@ -103,8 +105,14 @@ impl ModuleReference for WorkerAssetReference {
impl
ValueToString
for
WorkerAssetReference
{
impl
ValueToString
for
WorkerAssetReference
{
#[turbo_tasks
::
function]
#[turbo_tasks
::
function]
async
fn
to_string
(
&
self
)
->
Result
<
Vc
<
RcStr
>>
{
async
fn
to_string
(
&
self
)
->
Result
<
Vc
<
RcStr
>>
{
let
worker_name
=
match
self
.worker_type
{
WorkerReferenceSubType
::
WebWorker
=>
"Worker"
,
WorkerReferenceSubType
::
SharedWorker
=>
"SharedWorker"
,
WorkerReferenceSubType
::
ServiceWorker
=>
"ServiceWorker"
,
_
=>
"Worker"
,
};
Ok
(
Vc
::
cell
(
Ok
(
Vc
::
cell
(
format!
(
"new
Worker {}"
,
self
.request
.to_string
()
.await
?
,)
.into
(),
format!
(
"new
{} {}"
,
worker_name
,
self
.request
.to_string
()
.await
?
,)
.into
(),
))
))
}
}
}
}
...
...
turbopack/crates/turbopack-ecmascript/src/worker_chunk/chunk_item.rs
View file @
19fc57d9
...
@@ -58,6 +58,7 @@ impl WorkerLoaderChunkItem {
...
@@ -58,6 +58,7 @@ impl WorkerLoaderChunkItem {
impl
EcmascriptChunkItem
for
WorkerLoaderChunkItem
{
impl
EcmascriptChunkItem
for
WorkerLoaderChunkItem
{
#[turbo_tasks
::
function]
#[turbo_tasks
::
function]
async
fn
content
(
self
:
Vc
<
Self
>
)
->
Result
<
Vc
<
EcmascriptChunkItemContent
>>
{
async
fn
content
(
self
:
Vc
<
Self
>
)
->
Result
<
Vc
<
EcmascriptChunkItemContent
>>
{
let
_this
=
self
.await
?
;
let
chunks_data
=
self
.chunks_data
()
.await
?
;
let
chunks_data
=
self
.chunks_data
()
.await
?
;
let
chunks_data
=
chunks_data
.iter
()
.try_join
()
.await
?
;
let
chunks_data
=
chunks_data
.iter
()
.try_join
()
.await
?
;
let
chunks_data
:
Vec
<
_
>
=
chunks_data
let
chunks_data
:
Vec
<
_
>
=
chunks_data
...
@@ -65,6 +66,8 @@ impl EcmascriptChunkItem for WorkerLoaderChunkItem {
...
@@ -65,6 +66,8 @@ impl EcmascriptChunkItem for WorkerLoaderChunkItem {
.map
(|
chunk_data
|
EcmascriptChunkData
::
new
(
chunk_data
))
.map
(|
chunk_data
|
EcmascriptChunkData
::
new
(
chunk_data
))
.collect
();
.collect
();
// All worker types use the same blob URL generation
// The difference is handled at the JavaScript level when creating the worker
let
code
=
formatdoc!
{
let
code
=
formatdoc!
{
r
#
"
r
#
"
{TURBOPACK_EXPORT_VALUE}({TURBOPACK_WORKER_BLOB_URL}({chunks:#}));
{TURBOPACK_EXPORT_VALUE}({TURBOPACK_WORKER_BLOB_URL}({chunks:#}));
...
...
turbopack/crates/turbopack-ecmascript/src/worker_chunk/module.rs
View file @
19fc57d9
...
@@ -11,6 +11,7 @@ use turbopack_core::{
...
@@ -11,6 +11,7 @@ use turbopack_core::{
module
::
Module
,
module
::
Module
,
module_graph
::
ModuleGraph
,
module_graph
::
ModuleGraph
,
reference
::{
ModuleReference
,
ModuleReferences
},
reference
::{
ModuleReference
,
ModuleReferences
},
reference_type
::
WorkerReferenceSubType
,
resolve
::
ModuleResolveResult
,
resolve
::
ModuleResolveResult
,
};
};
...
@@ -21,13 +22,20 @@ use super::chunk_item::WorkerLoaderChunkItem;
...
@@ -21,13 +22,20 @@ use super::chunk_item::WorkerLoaderChunkItem;
#[turbo_tasks
::
value]
#[turbo_tasks
::
value]
pub
struct
WorkerLoaderModule
{
pub
struct
WorkerLoaderModule
{
pub
inner
:
ResolvedVc
<
Box
<
dyn
ChunkableModule
>>
,
pub
inner
:
ResolvedVc
<
Box
<
dyn
ChunkableModule
>>
,
pub
worker_type
:
WorkerReferenceSubType
,
}
}
#[turbo_tasks
::
value_impl]
#[turbo_tasks
::
value_impl]
impl
WorkerLoaderModule
{
impl
WorkerLoaderModule
{
#[turbo_tasks
::
function]
#[turbo_tasks
::
function]
pub
fn
new
(
module
:
ResolvedVc
<
Box
<
dyn
ChunkableModule
>>
)
->
Vc
<
Self
>
{
pub
fn
new
(
Self
::
cell
(
WorkerLoaderModule
{
inner
:
module
})
module
:
ResolvedVc
<
Box
<
dyn
ChunkableModule
>>
,
worker_type
:
WorkerReferenceSubType
,
)
->
Vc
<
Self
>
{
Self
::
cell
(
WorkerLoaderModule
{
inner
:
module
,
worker_type
,
})
}
}
#[turbo_tasks
::
function]
#[turbo_tasks
::
function]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment