Unverified Commit 38f75c76 authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Discover `ServiceWorkerEntryModule`s in `next-api` and compile +…

[turbopack] Discover `ServiceWorkerEntryModule`s in `next-api` and compile + serve those service workers (#94922)

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.
parent 032855e3
...@@ -87,6 +87,7 @@ use crate::{ ...@@ -87,6 +87,7 @@ use crate::{
AppPageRoute, Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes, AppPageRoute, Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes,
}, },
server_actions::{build_server_actions_loader, create_server_actions_manifest}, server_actions::{build_server_actions_loader, create_server_actions_manifest},
service_worker::service_worker_output_assets,
sri_manifest::get_sri_manifest_asset, sri_manifest::get_sri_manifest_asset,
}; };
...@@ -1419,6 +1420,15 @@ impl AppEndpoint { ...@@ -1419,6 +1420,15 @@ impl AppEndpoint {
None None
}; };
// Compile any service workers registered via `navigator.serviceWorker.register(new
// URL(...), { scope })` reachable from this endpoint.
client_assets.extend(
service_worker_output_assets(project, *module_graphs.base)
.await?
.iter()
.copied(),
);
let client_assets: ResolvedVc<OutputAssets> = let client_assets: ResolvedVc<OutputAssets> =
ResolvedVc::cell(client_assets.into_iter().collect::<Vec<_>>()); ResolvedVc::cell(client_assets.into_iter().collect::<Vec<_>>());
......
...@@ -25,5 +25,6 @@ pub mod project_asset_hashes_manifest; ...@@ -25,5 +25,6 @@ pub mod project_asset_hashes_manifest;
pub mod route; pub mod route;
pub mod routes_hashes_manifest; pub mod routes_hashes_manifest;
mod server_actions; mod server_actions;
mod service_worker;
mod sri_manifest; mod sri_manifest;
mod versioned_content_map; mod versioned_content_map;
...@@ -13,7 +13,10 @@ use next_core::{ ...@@ -13,7 +13,10 @@ use next_core::{
mode::NextMode, mode::NextMode,
next_app::{AppPage, AppPath}, next_app::{AppPage, AppPath},
next_client::{ next_client::{
ClientChunkingContextOptions, get_client_chunking_context, get_client_compile_time_info, ClientChunkingContextOptions, ClientContextType, ServiceWorkerChunkingContextOptions,
get_client_chunking_context, get_client_compile_time_info,
get_client_module_options_context, get_client_resolve_options_context,
get_service_worker_chunking_context,
}, },
next_config::{ next_config::{
DIST_PROFILES_DIR_NAME, ModuleIds as ModuleIdStrategyConfig, NextConfig, OutputType, DIST_PROFILES_DIR_NAME, ModuleIds as ModuleIdStrategyConfig, NextConfig, OutputType,
...@@ -1626,6 +1629,52 @@ impl Project { ...@@ -1626,6 +1629,52 @@ impl Project {
})) }))
} }
#[turbo_tasks::function]
pub(super) async fn service_worker_chunking_context(
self: Vc<Self>,
) -> Result<Vc<Box<dyn ChunkingContext>>> {
Ok(get_service_worker_chunking_context(
ServiceWorkerChunkingContextOptions {
mode: self.next_mode(),
root_path: self.project_root_path().owned().await?,
output_root: self.node_root().owned().await?,
output_root_to_root_path: self.node_root_to_root_path().owned().await?,
environment: self.client_compile_time_info().environment(),
minify: self.next_config().turbo_minify(self.next_mode()),
source_maps: self.next_config().client_source_maps(self.next_mode()),
no_mangling: self.no_mangling(),
hash_salt: self.next_config().output_hash_salt().to_resolved().await?,
},
))
}
#[turbo_tasks::function]
pub(super) async fn service_worker_asset_context(
self: Vc<Self>,
) -> Result<Vc<Box<dyn AssetContext>>> {
Ok(Vc::upcast(ModuleAssetContext::new(
TransitionOptions::default().cell(),
self.client_compile_time_info(),
get_client_module_options_context(
self.project_path().owned().await?,
self.execution_context(),
self.client_compile_time_info().environment(),
ClientContextType::Other,
self.next_mode(),
self.next_config(),
self.encryption_key(),
),
get_client_resolve_options_context(
self.project_path().owned().await?,
ClientContextType::Other,
self.next_mode(),
self.next_config(),
self.execution_context(),
),
Layer::new_with_user_friendly_name(rcstr!("service-worker"), rcstr!("Service Worker")),
)))
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) async fn server_chunking_context( pub(super) async fn server_chunking_context(
self: Vc<Self>, self: Vc<Self>,
......
use anyhow::Result;
use async_trait::async_trait;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, FxIndexSet, ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{ChunkingContext, EntryChunkGroupResult, availability_info::AvailabilityInfo},
context::AssetContext,
file_source::FileSource,
issue::{Issue, IssueExt, IssueSeverity, IssueStage, StyledString},
module::Module,
module_graph::{
ModuleGraph, SingleModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupEntry},
},
output::{OutputAsset, OutputAssets},
reference_type::{EntryReferenceSubType, ReferenceType},
source::Source,
};
use turbopack_ecmascript::references::service_worker::{
ServiceWorkerEntryModule, service_worker_chunk_filename,
};
use crate::project::Project;
/// Discovers every `navigator.serviceWorker.register(new URL(...), { scope })` registration
/// reachable from `module_graph` — via the [`ServiceWorkerEntryModule`] markers the analyzer leaves
/// in the graph — and compiles each registered worker into a single self-contained bundle.
///
/// Each is emitted under `node_root/service-worker/<scope-derived name>` (e.g.
/// `service-worker/sw.js`, `service-worker/sw-offline-mode.js`) and served at the matching
/// origin-root URL (`/sw.js`, `/sw-offline-mode.js`).
///
/// Only one service worker is supported per scope.
#[turbo_tasks::function]
pub async fn service_worker_output_assets(
project: Vc<Project>,
module_graph: Vc<ModuleGraph>,
) -> Result<Vc<OutputAssets>> {
let mut by_scope: FxIndexMap<RcStr, FxIndexSet<FileSystemPath>> = FxIndexMap::default();
for layer in module_graph.iter_graphs().await?.iter() {
let layer = layer.connect();
for module in layer.await?.iter_reachable_modules()? {
if let Some(marker) = ResolvedVc::try_downcast_type::<ServiceWorkerEntryModule>(module)
{
let marker = marker.await?;
let path = marker.inner.ident().await?.path.clone();
by_scope
.entry(marker.scope.clone())
.or_default()
.insert(path);
}
}
}
let mut assets: Vec<ResolvedVc<Box<dyn OutputAsset>>> = vec![];
for (scope, sources) in by_scope {
match sources.len() {
0 => {}
1 => {
let source = sources.into_iter().next().unwrap();
assets.push(
service_worker_chunk(project, scope, source)
.to_resolved()
.await?,
);
}
_ => {
let file_path = sources.iter().next().unwrap().clone();
let mut files = sources
.iter()
.map(|path| path.path.clone())
.collect::<Vec<_>>();
files.sort();
ConflictingServiceWorkersIssue {
scope,
files,
file_path,
}
.resolved_cell()
.emit();
}
}
}
Ok(Vc::cell(assets))
}
/// Compiles a single service worker `source_path` (registered at `scope`) into one self-contained
/// chunk emitted at `node_root/service-worker/<scope-derived name>`.
#[turbo_tasks::function]
async fn service_worker_chunk(
project: Vc<Project>,
scope: RcStr,
source_path: FileSystemPath,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let asset_context = project.service_worker_asset_context();
let chunking_context = project.service_worker_chunking_context();
let node_root = project.node_root().owned().await?;
let is_production = project.next_mode().await?.is_production();
let filename = service_worker_chunk_filename(&scope);
let source: Vc<Box<dyn Source>> = Vc::upcast(FileSource::new(source_path));
let module = asset_context
.process(source, ReferenceType::Entry(EntryReferenceSubType::Web))
.module()
.to_resolved()
.await?;
let own_graph = ModuleGraph::from_graphs(
vec![SingleModuleGraph::new_with_entry(
ChunkGroupEntry::Entry(vec![module]),
/* include_traced */ *project.should_write_nft_manifests().await?,
/* include_binding_usage */ is_production,
)],
/* binding_usage */ None,
)
.connect();
let EntryChunkGroupResult { asset, .. } = *chunking_context
.entry_chunk_group(
node_root.join("service-worker")?.join(&filename)?,
ChunkGroup::Entry(vec![module]),
own_graph,
/* extra_chunks */ OutputAssets::empty(),
/* extra_referenced_chunks */ OutputAssets::empty(),
AvailabilityInfo::root(),
)
.await?;
Ok(*asset)
}
/// Emitted when more than one service worker, with differing source files, is registered for the
/// same scope. Each scope can only serve a single service worker.
#[turbo_tasks::value(shared)]
struct ConflictingServiceWorkersIssue {
scope: RcStr,
files: Vec<RcStr>,
file_path: FileSystemPath,
}
#[async_trait]
#[turbo_tasks::value_impl]
impl Issue for ConflictingServiceWorkersIssue {
async fn title(&self) -> Result<StyledString> {
Ok(StyledString::Text(rcstr!(
"Multiple service workers registered for the same scope."
)))
}
async fn description(&self) -> Result<Option<StyledString>> {
Ok(Some(StyledString::Stack(vec![
StyledString::Text(
format!(
"Multiple service workers with different source files were registered for \
scope '{}'. Each scope serves a single service worker.",
self.scope
)
.into(),
),
StyledString::Line(vec![
StyledString::Text(rcstr!("Registered source files: ")),
StyledString::Code(self.files.join(", ").into()),
]),
])))
}
fn severity(&self) -> IssueSeverity {
IssueSeverity::Error
}
async fn file_path(&self) -> Result<FileSystemPath> {
Ok(self.file_path.clone())
}
fn stage(&self) -> IssueStage {
IssueStage::ProcessModule
}
}
...@@ -55,6 +55,7 @@ export type FsOutput = { ...@@ -55,6 +55,7 @@ export type FsOutput = {
| 'publicFolder' | 'publicFolder'
| 'nextStaticFolder' | 'nextStaticFolder'
| 'legacyStaticFolder' | 'legacyStaticFolder'
| 'serviceWorker'
| 'devVirtualFsItem' | 'devVirtualFsItem'
itemPath: string itemPath: string
...@@ -131,6 +132,7 @@ export async function setupFsCheck(opts: { ...@@ -131,6 +132,7 @@ export async function setupFsCheck(opts: {
const publicFolderItems = new Set<string>() const publicFolderItems = new Set<string>()
const nextStaticFolderItems = new Set<string>() const nextStaticFolderItems = new Set<string>()
const legacyStaticFolderItems = new Set<string>() const legacyStaticFolderItems = new Set<string>()
const serviceWorkerItems = new Set<string>()
const appFiles = new Set<string>() const appFiles = new Set<string>()
const pageFiles = new Set<string>() const pageFiles = new Set<string>()
...@@ -153,6 +155,7 @@ export async function setupFsCheck(opts: { ...@@ -153,6 +155,7 @@ export async function setupFsCheck(opts: {
const publicFolderPath = path.join(opts.dir, 'public') const publicFolderPath = path.join(opts.dir, 'public')
const nextStaticFolderPath = path.join(distDir, 'static') const nextStaticFolderPath = path.join(distDir, 'static')
const legacyStaticFolderPath = path.join(opts.dir, 'static') const legacyStaticFolderPath = path.join(opts.dir, 'static')
const serviceWorkerFolderPath = path.join(distDir, 'service-worker')
let customRoutes: UnwrapPromise<ReturnType<typeof loadCustomRoutes>> = { let customRoutes: UnwrapPromise<ReturnType<typeof loadCustomRoutes>> = {
redirects: [], redirects: [],
rewrites: { rewrites: {
...@@ -216,6 +219,16 @@ export async function setupFsCheck(opts: { ...@@ -216,6 +219,16 @@ export async function setupFsCheck(opts: {
if (opts.config.output !== 'standalone') throw err if (opts.config.output !== 'standalone') throw err
} }
try {
for (const file of await recursiveReadDir(serviceWorkerFolderPath)) {
serviceWorkerItems.add(encodeURIPath(normalizePathSep(file)))
}
} catch (err: any) {
if (err.code !== 'ENOENT') {
throw err
}
}
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST) const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST) const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST)
const middlewareManifestPath = path.join( const middlewareManifestPath = path.join(
...@@ -425,6 +438,7 @@ export async function setupFsCheck(opts: { ...@@ -425,6 +438,7 @@ export async function setupFsCheck(opts: {
debug('customRoutes', customRoutes) debug('customRoutes', customRoutes)
debug('publicFolderItems', publicFolderItems) debug('publicFolderItems', publicFolderItems)
debug('nextStaticFolderItems', nextStaticFolderItems) debug('nextStaticFolderItems', nextStaticFolderItems)
debug('serviceWorkerItems', serviceWorkerItems)
debug('pageFiles', pageFiles) debug('pageFiles', pageFiles)
debug('appFiles', appFiles) debug('appFiles', appFiles)
...@@ -526,6 +540,7 @@ export async function setupFsCheck(opts: { ...@@ -526,6 +540,7 @@ export async function setupFsCheck(opts: {
const itemsToCheck: Array<[Set<string>, FsOutput['type']]> = [ const itemsToCheck: Array<[Set<string>, FsOutput['type']]> = [
[this.devVirtualFsItems, 'devVirtualFsItem'], [this.devVirtualFsItems, 'devVirtualFsItem'],
[serviceWorkerItems, 'serviceWorker'],
[nextStaticFolderItems, 'nextStaticFolder'], [nextStaticFolderItems, 'nextStaticFolder'],
[legacyStaticFolderItems, 'legacyStaticFolder'], [legacyStaticFolderItems, 'legacyStaticFolder'],
[publicFolderItems, 'publicFolder'], [publicFolderItems, 'publicFolder'],
...@@ -649,6 +664,10 @@ export async function setupFsCheck(opts: { ...@@ -649,6 +664,10 @@ export async function setupFsCheck(opts: {
itemsRoot = publicFolderPath itemsRoot = publicFolderPath
break break
} }
case 'serviceWorker': {
itemsRoot = serviceWorkerFolderPath
break
}
case 'appFile': case 'appFile':
case 'pageFile': case 'pageFile':
case 'nextImage': case 'nextImage':
...@@ -672,6 +691,7 @@ export async function setupFsCheck(opts: { ...@@ -672,6 +691,7 @@ export async function setupFsCheck(opts: {
'nextStaticFolder', 'nextStaticFolder',
'publicFolder', 'publicFolder',
'legacyStaticFolder', 'legacyStaticFolder',
'serviceWorker',
] as (typeof type)[] ] as (typeof type)[]
).includes(type) ).includes(type)
......
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