Skip to content

[turbopack] Discover ServiceWorkerEntryModules in next-api and compile + serve those service workers#94922

Merged
sampoder merged 2 commits into
canaryfrom
sp/turbopack/sw/03-discovery-generation-serve
Jun 25, 2026
Merged

[turbopack] Discover ServiceWorkerEntryModules in next-api and compile + serve those service workers#94922
sampoder merged 2 commits into
canaryfrom
sp/turbopack/sw/03-discovery-generation-serve

Conversation

@sampoder

@sampoder sampoder commented Jun 17, 2026

Copy link
Copy Markdown
Member

This is the big change in the next- part of the repo to support this. The code goes through the module graph to find any service workers that need compiling and then compiles them. It also has the code changes to packages/next/src/server/lib/router-utils/filesystem.ts that serve these files on their respective routes.

@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Stats cancelled

Commit: 3fe008b
View workflow run

@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 3fe008b

@sampoder sampoder requested review from bgw and lukesandberg June 17, 2026 23:03
@sampoder sampoder marked this pull request as ready for review June 17, 2026 23:03
@sampoder sampoder force-pushed the sp/turbopack/sw/02-discovery-marker branch from bb6471e to 0f165dd Compare June 24, 2026 00:55
@sampoder sampoder force-pushed the sp/turbopack/sw/03-discovery-generation-serve branch from 57abc66 to a1e0472 Compare June 24, 2026 00:56
@sampoder sampoder force-pushed the sp/turbopack/sw/02-discovery-marker branch from 0f165dd to ab2d218 Compare June 24, 2026 01:00
@sampoder sampoder force-pushed the sp/turbopack/sw/03-discovery-generation-serve branch from a1e0472 to a12103d Compare June 24, 2026 01:00
@sampoder sampoder force-pushed the sp/turbopack/sw/02-discovery-marker branch from ab2d218 to 964e7e5 Compare June 24, 2026 04:27
@sampoder sampoder force-pushed the sp/turbopack/sw/03-discovery-generation-serve branch from a12103d to a7734b1 Compare June 24, 2026 04:27
@sampoder sampoder force-pushed the sp/turbopack/sw/02-discovery-marker branch from 964e7e5 to ab06baf Compare June 24, 2026 04:39
@sampoder sampoder force-pushed the sp/turbopack/sw/03-discovery-generation-serve branch from a7734b1 to 6c7d8b7 Compare June 24, 2026 04:39
Comment thread crates/next-api/src/service_worker.rs Outdated
Comment on lines +72 to +76
bail!(
"Multiple service workers with different source files were registered for \
scope '{scope}' ({}). Each scope serves a single service worker.",
files.join(", "),
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an issue

Comment thread crates/next-api/src/service_worker.rs Outdated
Comment on lines +108 to +109
is_production,
is_production,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is_production,
is_production,
/*include_traced*/ is_production,
/*include_binding_usage*/ is_production,
Comment thread crates/next-api/src/service_worker.rs Outdated
let own_graph = ModuleGraph::from_graphs(
vec![SingleModuleGraph::new_with_entry(
ChunkGroupEntry::Entry(vec![module]),
is_production,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use Project::should_write_nft_manifests?

Comment thread crates/next-api/src/service_worker.rs Outdated
is_production,
is_production,
)],
None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
None,
/*binding_usage*/ None,
Comment thread crates/next-api/src/service_worker.rs Outdated
Comment on lines +120 to +121
OutputAssets::empty(),
OutputAssets::empty(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OutputAssets::empty(),
OutputAssets::empty(),
/*extra_chunks*/ OutputAssets::empty(),
/*extra_referenced_chunks*/ OutputAssets::empty(),
@sampoder sampoder force-pushed the sp/turbopack/sw/02-discovery-marker branch from ab06baf to d040635 Compare June 25, 2026 00:31
sampoder added a commit that referenced this pull request Jun 25, 2026
…e` (#94920)

Broke these changes out into a separate PR but these are needed later
down the stack in #94922 which
changes `next-api`.
@sampoder sampoder force-pushed the sp/turbopack/sw/02-discovery-marker branch from d040635 to 412d654 Compare June 25, 2026 00:38
@sampoder sampoder force-pushed the sp/turbopack/sw/03-discovery-generation-serve branch from 6c7d8b7 to adf44b5 Compare June 25, 2026 00:44
@sampoder sampoder force-pushed the sp/turbopack/sw/03-discovery-generation-serve branch from adf44b5 to 3fe008b Compare June 25, 2026 00:49
sampoder added a commit that referenced this pull request Jun 25, 2026
…nk_filename` (#94921)

This is what will be inserted by the analyzer into a page's module
graph: #94923. This allows
`next-api` to discover these modules (see
#94922) and compile them.
Base automatically changed from sp/turbopack/sw/02-discovery-marker to canary June 25, 2026 05:15
@sampoder sampoder merged commit 38f75c7 into sp/turbopack/single-entry-chunks Jun 25, 2026
137 of 139 checks passed
@sampoder sampoder deleted the sp/turbopack/sw/03-discovery-generation-serve branch June 25, 2026 05:23
sampoder added a commit that referenced this pull request Jun 29, 2026
This PR is the PR that "turns on" the service worker feature, this is
because in the analyzer we are now looking for code like:

```
await navigator.serviceWorker.register(new URL('../lib/service-workers/1.js', import.meta.url), ...)
```

And transforms it into registering the URL that Next.js will now serve
based on scope.

URL scheme is:

```
scope: "/" -> `sw.js`
scope: "/offline/mode" -> `sw-offline-mode.js`
```

etc.

It also inserts the `ServiceWorkerEntryModule`s so they can be
discovered by `next-api` (see
#94922).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants