Add Workbox service worker example - #283
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Coverage Report for CI Build 32667671422Coverage decreased (-0.02%) to 92.821%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
Workbox Workflow Integration PlanStatus: Example implemented with injected policy
Domstack does not run Workbox Instead, The authored service worker remains normal user code. It imports Workbox packages directly and passes the generated Goals
Non-goals
Current example architectureThe Workbox example uses:
The manifest settings hook injects a policy constant: context.defineServiceWorkerConstant('__DOMSTACK_WORKBOX_POLICY__', {
version: manifest.version,
offlineFallbackUrl: '/offline/',
precacheManifest: [
{ url: '/', revision: 'sha256hex...' },
{ url: '/about/', revision: 'sha256hex...' },
{ url: '/global-ABC123.css', revision: null, integrity: 'sha256-...' },
],
runtimeUrls: ['/progressive-cache/'],
networkOnlyUrls: ['/admin/'],
})The service worker reads the injected policy inside the manifest-enabled branch: declare const __DOMSTACK_WORKBOX_POLICY__: StaticMpaWorkboxServiceWorkerPolicy
if (manifestEnabled) {
const policy = __DOMSTACK_WORKBOX_POLICY__
precacheAndRoute(policy.precacheManifest)
}The policy constant must not be read at module top level in watch mode. Watch mode builds do not define it. Workbox APIs currently usedThe example uses:
Policy shapeWorkbox's native precache input is: type WorkboxPrecacheEntry = {
url: string
revision: string | null
integrity?: string
}The example policy includes that native shape plus app-specific route policy: type StaticMpaWorkboxServiceWorkerPolicy = {
version: string
offlineFallbackUrl: string
precacheManifest: WorkboxPrecacheEntry[]
runtimeUrls: string[]
networkOnlyUrls: string[]
}Only
Why injected policy is preferredInjected policy has these advantages:
This means Domstack only needs the general It does not need Workbox-specific source transformation in core. Watch modeWorkbox precaching is disabled in watch mode. Watch mode sets The service worker branch for watch mode:
The client also unregisters existing workers and clears known caches in watch mode. Watch builds disable esbuild splitting so Client lifecycleThe Workbox example uses Current behavior:
Watch-mode cleanup happens before normal registration and does not wait for Production registration waits for Runtime caching policyThe example only runtime-caches routes selected by manifest vars. It uses Workbox plugins to keep runtime cache behavior bounded:
The example does not cache arbitrary API/data requests by default. Potential package helperA future helper could live outside core or as an optional export: import { createWorkboxPolicy } from '@domstack/static/workbox'
export default {
hooks: {
manifestBuilt: [context => {
context.defineServiceWorkerConstant(
'__APP_WORKBOX_POLICY__',
createWorkboxPolicy(context.manifest, options),
)
}],
},
}The helper could cover:
This should remain optional. Domstack core should continue exposing generic manifest hooks rather than hard-coding Workbox behavior. Deprecated ideasThese ideas were considered but are not the current direction:
They remain possible for external integrations, but the example and current plan prefer injected constants. |
Summary
Adds the Workbox-based static-MPA service-worker example on top of #262 and #282.
manifestBuilt.Validation
npm run build --workspace=@domstack/static-mpa-workbox-offline-examplenpx tsc -p examples/static-mpa-workbox-offline/tsconfig.jsonnpm testStack created with GitHub Stacks CLI • Give Feedback 💬