managarten/packages/shared-pwa/src/config.ts
Till JS 4b2007e97c fix(pwa): wire up manifest link + SW registration so install prompt works
The PWA was configured end-to-end in vite.config.ts and built the
manifest + service worker correctly, but neither was ever loaded by
the browser — no <link rel="manifest"> in the HTML and no script
registering the generated registerSW.js. Chrome therefore never fired
beforeinstallprompt, no install icon appeared in the URL bar, and the
hand-rolled PwaUpdatePrompt hung on navigator.serviceWorker.ready
because no SW had been registered.

Changes:
- Render pwaInfo.webManifest.linkTag into <svelte:head> in the root
  layout so Chrome finds the hashed manifest.
- Replace the hand-rolled SW-update logic in PwaUpdatePrompt with
  useRegisterSW() from virtual:pwa-register/svelte — it registers the
  worker (immediate: true) and exposes reactive needRefresh +
  updateServiceWorker stores that match registerType: 'prompt'.
- Add triple-slash refs to vite-plugin-pwa/info and /svelte in
  app.d.ts for the virtual module types.
- Set manifest.id = startUrl in @mana/shared-pwa so Chrome doesn't
  warn and keeps the install identity stable across start_url edits.
- Keep devEnabled: false and expand the comment: the 2026-04-08
  dreams mic-button bug and the /offline navigateFallback both
  misbehave when Workbox precache doesn't run under vite dev. Test
  the install flow via `pnpm build && pnpm preview` instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 14:05:49 +02:00

145 lines
3.6 KiB
TypeScript

/**
* PWA Configuration Factory
*
* Creates a complete @vite-pwa/sveltekit configuration with sensible defaults
* and preset-based caching strategies.
*
* @example
* ```ts
* import { createPWAConfig } from '@mana/shared-pwa';
* import { SvelteKitPWA } from '@vite-pwa/sveltekit';
*
* export default defineConfig({
* plugins: [
* sveltekit(),
* SvelteKitPWA(createPWAConfig({
* name: 'Calendar - Kalender',
* shortName: 'Calendar',
* description: 'Kalender mit Offline-Unterstützung',
* themeColor: '#3b82f6',
* preset: 'standard',
* })),
* ],
* });
* ```
*/
import type { PWAConfigOptions, PWAConfig, ManifestConfig, WorkboxConfig } from './types.js';
import {
DEFAULT_BACKGROUND_COLOR,
DEFAULT_CATEGORIES,
DEFAULT_INCLUDE_ASSETS,
DEFAULT_GLOB_PATTERNS,
DEFAULT_GLOB_IGNORES,
DEFAULT_NAVIGATE_FALLBACK_DENYLIST,
DEFAULT_ICONS,
} from './defaults.js';
import { getPresetRuntimeCaching } from './presets.js';
/**
* Create a complete PWA configuration for SvelteKit apps
*/
export function createPWAConfig(options: PWAConfigOptions): PWAConfig {
const {
name,
shortName,
description,
themeColor,
backgroundColor = DEFAULT_BACKGROUND_COLOR,
preset = 'standard',
shortcuts = [],
categories = DEFAULT_CATEGORIES,
includeAssets = [],
globIgnores = [],
additionalRuntimeCaching = [],
navigateFallback = '/offline',
navigateFallbackDenylist = DEFAULT_NAVIGATE_FALLBACK_DENYLIST,
devEnabled = true,
registerType = 'autoUpdate',
lang = 'de',
startUrl = '/',
} = options;
// Build manifest
const manifest: ManifestConfig = {
// Pin the app identity to the start URL. Without `id`, Chrome derives
// one from start_url and warns in DevTools; it also refuses to
// re-prompt an install if start_url ever changes.
id: startUrl,
name,
short_name: shortName,
description,
theme_color: themeColor,
background_color: backgroundColor,
display: 'standalone',
orientation: 'any',
scope: '/',
start_url: startUrl,
lang,
categories,
icons: DEFAULT_ICONS,
};
// Add shortcuts if provided
if (shortcuts.length > 0) {
manifest.shortcuts = shortcuts.map((shortcut) => ({
name: shortcut.name,
short_name: shortcut.short_name,
description: shortcut.description,
url: shortcut.url,
icons: [{ src: 'pwa-192x192.png', sizes: '192x192' }],
}));
}
// Build workbox config
const workbox: WorkboxConfig = {
globPatterns: DEFAULT_GLOB_PATTERNS,
globIgnores: [...DEFAULT_GLOB_IGNORES, ...globIgnores],
cleanupOutdatedCaches: true,
clientsClaim: true,
skipWaiting: true,
navigateFallback,
navigateFallbackDenylist,
runtimeCaching: [...getPresetRuntimeCaching(preset), ...additionalRuntimeCaching],
maximumFileSizeToCacheInBytes: 8 * 1024 * 1024, // 8 MiB for large unified apps
};
// Return complete config
return {
registerType,
devOptions: {
enabled: devEnabled,
},
includeAssets: [...DEFAULT_INCLUDE_ASSETS, ...includeAssets],
manifest,
workbox,
};
}
/**
* Create PWA config with SQLite WASM support (for offline-first apps)
* Adds proper glob ignores and OPFS configuration
*/
export function createOfflineFirstPWAConfig(
options: PWAConfigOptions & {
/**
* Additional packages to exclude from precaching
*/
excludePackages?: string[];
}
): PWAConfig {
const { excludePackages = [], globIgnores = [], ...rest } = options;
// Add SQLite-specific ignores
const allGlobIgnores = [
'**/*sqlite*',
'**/*wasm*',
...excludePackages.map((pkg) => `**/${pkg}/**`),
...globIgnores,
];
return createPWAConfig({
...rest,
globIgnores: allGlobIgnores,
});
}