# Plan: PWA + Firebase Hosting for JamBuddy Status: **proposed**. Each phase is independently shippable; stop after any one. --- ## 0. Why this is low-risk The Electron shell (`electron/main.cjs`, `preload.cjs`) does **nothing but host a window** — no IPC, no native APIs, `sandbox:false` only to give the renderer full Web Audio. All logic lives in the React renderer, which already runs browser-only via `npm run dev`. So the web build is the same `vite build` output the desktop installer already ships; we just add a manifest + service worker and serve `dist/` over HTTPS. Firebase gives us HTTPS for free, which `getUserMedia` (the mic) requires — that's the only hard web constraint. Nothing in the app makes network calls today, so there's no API surface to re-host and no secrets to manage. --- ## Phase 1 — PWA scaffolding (no Firebase yet) Goal: `vite build` produces an installable, offline-capable web app served from any static host. ### 1.1 Add `vite-plugin-pwa` Only new dependency. Wraps Workbox under a Vite plugin — generates the service worker and injects the manifest. Cheaper than hand-rolling a SW and gets update flow for free. ```js // vite.config.js import { VitePWA } from 'vite-plugin-pwa' export default defineConfig({ plugins: [ react(), tailwindcss(), VitePWA({ registerType: 'autoUpdate', includeAssets: ['favicon.ico', 'apple-touch-icon.png'], manifest: { name: 'JamBuddy — WhatTheFlat', short_name: 'JamBuddy', description: 'Real-time key and chord detection for live jams.', theme_color: '#0f0f0f', // matches bg-surface token background_color: '#0f0f0f', display: 'standalone', orientation: 'any', start_url: '/', scope: '/', icons: [ /* see 1.3 */ ], }, workbox: { globPatterns: ['**/*.{js,css,html,svg,png,ico,woff2}'], // Audio is captured live — nothing to cache from a remote. Precache the app shell only. navigateFallback: '/index.html', }, }), ], }) ``` ### 1.2 Create `public/` directory Vite serves `public/` at root and copies it verbatim into `dist/`. We need it for: - `favicon.ico`, `apple-touch-icon.png` (180×180) - Any static PWA icons referenced by the manifest - (Optional) `robots.txt` — not needed for an app, skip. The existing `src/assets/whattheflat-logo.png` is imported in `App.jsx` and bundled by Vite — leave that as-is; **don't** move it. The manifest icons must live in `public/` because they're referenced by URL, not imported. ### 1.3 Generate icon set from the existing logo Derive all required sizes from `assets/whattheflat-logo.png` (one source image → PNG output). Required manifest entries: ```js icons: [ { src: '/icon-192.png', sizes: '192x192', type: 'image/png', purpose: 'any' }, { src: '/icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'any' }, { src: '/icon-512-maskable.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }, ] ``` Plus `apple-touch-icon.png` (180×180) in `public/` — iOS ignores the manifest and uses this ``. The maskable variant needs safe-zone padding (~10%) so the logo isn't cropped by Android's circular mask. One-liner with ImageMagick if available, else export manually from the source PNG: ```bash magick assets/whattheflat-logo.png -resize 192x192 public/icon-192.png magick assets/whattheflat-logo.png -resize 512x512 public/icon-512.png magick assets/whattheflat-logo.png -resize 180x180 public/apple-touch-icon.png ``` ### 1.4 Update `index.html` Two problems with the current `
`: 1. **No PWA tags** — add theme-color, manifest link, apple-touch-icon, apple-mobile-web-app-capable, description. 2. **CSP is hardcoded for dev** — `connect-src 'self' http://localhost:5173 ws://localhost:5173` and `script-src 'self' 'unsafe-eval'`. The `unsafe-eval` and the localhost entries are Vite dev artifacts; production Vite doesn't need `unsafe-eval`. Keep a CSP (it's good hygiene and the app is genuinely offline) but drop the dev bits: ```html ``` CSP (production): ``` default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; media-src 'self' blob:; ``` `blob:` in `media-src` is required because `getUserMedia` streams are blob-backed and the analyser reads them. Verify nothing else regresses — `vite-plugin-pwa` registers the SW from a same-origin script, so `'self'` covers it. Move the dev-only CSP into a Vite conditional so dev still allows HMR/websocket: ```js // vite.config.js — inject dev CSP via plugin, or keep a separate dev index template. ``` Simplest: keep one CSP in `index.html` with the production values, and in dev let Vite's own injected tags coexist (HMR works over the same origin websocket; CSP `connect-src 'self'` already allows it). If dev breaks, add `ws://localhost:5173` only in a dev-specific block. ### 1.5 Verify - `npm run build` → `dist/` contains `manifest.webmanifest`, `registerSW.js`, `sw.js`, and all `public/` icons. - Serve `dist/` locally with `npm run preview` and use Chrome DevTools → Application → Manifest (should show icons, installability ✓) and Service Workers (registered). - Install to desktop / "Add to Home Screen" on mobile. - Confirm the app still works **fully offline** after first load (airplane mode). Since there are no network calls, the only risk is the SW failing to precache — Workbox handles this. --- ## Phase 2 — Firebase Hosting setup Goal: serve `dist/` over HTTPS on a `*.web.app` domain (or custom domain). ### 2.1 Install the CLI ```bash npm install -D firebase-tools ``` ### 2.2 Init ```bash npx firebase init hosting ``` Answer: - **Public directory:** `dist` - **Single-page app (rewrite all urls to /index.html):** Yes — though the app has no client-side routing today, this protects future routes and is harmless. - **Set up automatic builds with GitHub:** **No.** (You're not using GitHub.) - **File `dist/index.html` already exists — overwrite?** **No.** This is critical — saying yes would clobber the Vite-built index. This creates: - `firebase.json` - `.firebaserc` ### 2.3 `firebase.json` (target shape) ```json { "hosting": { "public": "dist", "ignore": ["firebase.json", "**/.*", "**/node_modules/**"], "rewrites": [{ "source": "**", "destination": "/index.html" }], "headers": [ { "source": "**/*.@(js|css|svg|png|ico|woff2|webmanifest)", "headers": [ { "key": "Cache-Control", "value": "public, max-age=31536000, immutable" } ] }, { "source": "/sw.js", "headers": [ { "key": "Cache-Control", "value": "no-cache" } ] }, { "source": "/index.html", "headers": [ { "key": "Cache-Control", "value": "no-cache" } ] } ] } } ``` Why the headers: Vite hashes asset filenames, so JS/CSS/icons are content-addressed → cache them **immortal**. `index.html` and `sw.js` must stay fresh so updates propagate — `no-cache` revalidates them every time. (Note: with `vite-plugin-pwa`'s `autoUpdate`, the SW self-updates; the `no-cache` on `sw.js` is belt-and-braces.) ### 2.4 First deploy ```bash npm run build npx firebase deploy --only hosting ``` Output: `https://