/** * `@nifrajs/web/service-worker` - generate a service worker from a build manifest. * * A service worker is the one piece of an app that outlives a deploy, keeps serving after you have * stopped, and can hand one user a response produced for another. So the generated worker is * deliberately narrow, and every rule below exists because the permissive version of it is a bug: * * - **Only content-hashed assets are precached.** A hashed URL names its bytes, so serving it from * cache forever is correct by construction. Anything unhashed is left to the network. * - **Documents are never cached.** Only navigations that FAIL are answered, and only with the * offline page you nominate. A cached HTML document is how a service worker serves one signed-in * user the page rendered for another; there is no per-user story here worth that risk. * - **GET, same-origin, `ok`, and not `no-store`.** Anything else goes straight to the network. * - **The cache name carries the build id.** A deploy that changed the assets gets a new cache, and * activation deletes every older one, so a stale worker cannot pin an old build indefinitely. * * It is opt-in and generated at build time; an app that does not call this ships nothing. */ /** The parts of a `BuildManifest` a worker needs. Structural, so a caller can pass extra fields. */ export interface ServiceWorkerManifest { readonly entry: string; readonly assets: readonly string[]; readonly css?: readonly string[]; } export interface ServiceWorkerOptions { /** * Distinguishes this build's cache from the last one. Use something that changes exactly when the * assets do - a content hash, a commit sha, a release version. */ readonly buildId: string; /** * URL of a page to serve when a navigation fails and the network is unreachable. It must be a * static, user-independent document (a prerendered `/offline`), because every visitor gets the same * bytes. Omit it and failed navigations simply fail, which is the honest default. */ readonly offlineUrl?: string; /** Cache name prefix. Default `nifra`. */ readonly cacheName?: string; /** Extra same-origin URLs to precache. Only pass immutable ones. */ readonly additionalPrecache?: readonly string[]; } /** * Generate the service worker source for a build. * * Write the result to a file served from the ORIGIN ROOT (`/sw.js`): a worker's default scope is its * own directory, so one served from `/assets/` could never control the pages it exists for. */ export declare function generateServiceWorker(manifest: ServiceWorkerManifest, options: ServiceWorkerOptions): string; /** * The registration snippet, for a `