Files
collective-lists/apps/web/src/global.d.ts
Oier Bravo Urtasun a6e6915b0b feat(fase-14): PWA auto-update toast (14.1)
Migrate from `registerSW({ immediate: true })` to `useRegisterSW` from
`virtual:pwa-register/svelte`, mirror its `needRefresh` / `offlineReady`
stores into stable layout-local writables, and render a new
`UpdateToast` pill (bottom-center) that surfaces:

  - "New version available" + a "Reload" button that calls
    `updateServiceWorker(true)`
  - "Ready to use offline" (auto-dismiss after 4s) on first install

Adds `[pwa] …` console.info telemetry on register / need-refresh /
apply. Stand-in for the unavailable `updateViaCache: 'none'` runtime
option (RegisterSWOptions doesn't expose it in vite-plugin-pwa 0.21.2):
poll `reg.update()` every 15min, which keeps a too-aggressively-cached
SW file from pinning users on the previous build for up to 24h.

`apps/web/src/global.d.ts` declares the virtual module's surface inline
so svelte-check picks it up without a relative path into node_modules;
the same file pre-declares the Fase-14.3 `__APP_VERSION__` triplet.

PU-01 stubs the registrar via `window.__pwaRegisterStub` (the layout
checks for it before calling the real `useRegisterSW`) so the toast
flow can be driven from a Playwright test without a second build.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-18 06:59:29 +02:00

41 lines
1.4 KiB
TypeScript

/**
* Ambient declarations for app-wide constants and virtual modules.
*
* - `virtual:pwa-register/svelte` is provided at build time by
* `vite-plugin-pwa`'s Svelte helper (Fase 14.1). vite-plugin-pwa ships
* `svelte.d.ts` for this but TypeScript only picks it up if the path
* is referenced; declaring the module here inlines the (small) surface
* without coupling to the package's internal layout.
*
* - `__APP_VERSION__`, `__APP_COMMIT__`, `__BUILD_TIME__` (Fase 14.3)
* are replaced literally by Vite at build time via the `define`
* option in `vite.config.ts`. They're consumed exclusively through
* `$lib/version.ts` — never inline — so tree-shaking can't drop
* the build-time substitution.
*/
declare module 'virtual:pwa-register/svelte' {
import type { Writable } from 'svelte/store';
export interface RegisterSWOptions {
immediate?: boolean;
onNeedRefresh?: () => void;
onOfflineReady?: () => void;
onRegisteredSW?: (
swScriptUrl: string,
registration: ServiceWorkerRegistration | undefined
) => void;
onRegisterError?: (error: unknown) => void;
}
export function useRegisterSW(options?: RegisterSWOptions): {
needRefresh: Writable<boolean>;
offlineReady: Writable<boolean>;
updateServiceWorker: (reloadPage?: boolean) => Promise<void>;
};
}
declare const __APP_VERSION__: string;
declare const __APP_COMMIT__: string;
declare const __BUILD_TIME__: string;