Files
scrabble-game/ui/src/lib/pwa.ts
T
Ilia Denisov 990cb4a68d
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Failing after 11s
CI / conformance (pull_request) Successful in 10s
CI / gate (pull_request) Failing after 0s
CI / deploy (pull_request) Has been skipped
feat(pwa): installable web app + landing web entry
Make the web SPA an installable PWA and surface it to users:

- manifest.webmanifest + an install-only service worker + 192/512/maskable
  icons (ui/public); PWA head tags in index.html; the .webmanifest MIME type
  registered in the gateway (the distroless image has no /etc/mime.types).
- Platform-adaptive install CTA (components/InstallApp.svelte + lib/pwa):
  one-tap on Chromium, manual Add-to-Home-Screen instructions on iOS Safari,
  hidden elsewhere / once installed / inside a Mini App. Shown under the
  logged-out login card and at the bottom of Settings.
- The landing gains a third entry linking /app/ (the brand tile), with a
  caption under all three (Telegram / VK / Веб-версия).

The service worker is navigation-only (network-first, cached-shell fallback);
hashed assets and the Connect stream are untouched. It exists to satisfy
Chromium's installability requirement and is the single growth point for a
future opt-in offline mode.

Tests: pwa.ts unit tests; a webui probe (manifest/sw.js content-type + the
SPA-fallback-to-HTML trap); e2e for the one-tap CTA, the iOS instructions
modal and the landing web entry. Docs: ARCHITECTURE §13, FUNCTIONAL (+ru),
gateway README.
2026-07-05 21:05:52 +02:00

58 lines
2.8 KiB
TypeScript

// Pure PWA install helpers, kept out of the Svelte layer (and free of app imports) so the
// platform branching is unit-testable in the node test env. The reactive runtime (the captured
// install prompt, the Mini-App gate, the service-worker registration) lives in pwa.svelte.ts.
/** InstallMode is how the install call-to-action resolves for the current platform. */
export type InstallMode = 'oneTap' | 'iosInstructions' | 'hidden';
/**
* isStandalone reports whether the app is already running as an installed PWA — launched from
* the home screen / desktop rather than a browser tab. Both the standard display-mode media
* query and the iOS-only navigator.standalone flag are checked. Returns false with no window
* (e.g. under the unit tests).
*/
export function isStandalone(): boolean {
if (typeof window === 'undefined') return false;
const displayMode = window.matchMedia?.('(display-mode: standalone)').matches ?? false;
const iosStandalone =
(window.navigator as Navigator & { standalone?: boolean }).standalone === true;
return displayMode || iosStandalone;
}
/**
* isIosSafari reports whether the app runs in Safari on iOS / iPadOS — the only browser there
* that can add a site to the home screen, and one with no programmatic install (it never fires
* beforeinstallprompt). In-app webviews (Telegram, VK) and the iOS Chrome/Firefox/Edge wrappers
* are excluded, as is desktop Safari. Used to switch the CTA to manual Share -> Add-to-Home-Screen
* instructions.
*/
export function isIosSafari(): boolean {
if (typeof navigator === 'undefined') return false;
const ua = navigator.userAgent;
// iPadOS 13+ reports a Mac UA but exposes touch points — catch that as iOS too.
const isIos =
/iPhone|iPod|iPad/.test(ua) || (/Macintosh/.test(ua) && (navigator.maxTouchPoints ?? 0) > 1);
if (!isIos) return false;
// Safari only: the iOS Chrome (CriOS), Firefox (FxiOS), Edge (EdgiOS), Opera (OPiOS) and the
// Google-app (GSA) wrappers all carry "Safari" but cannot add to the home screen from our CTA.
return /Safari/.test(ua) && !/CriOS|FxiOS|EdgiOS|OPiOS|GSA/.test(ua);
}
/**
* resolveInstallMode chooses how the install CTA should behave from the platform facts:
* `deferredAvailable` — a beforeinstallprompt event was captured (Chromium one-tap); `standalone`
* — already installed; `iosSafari` — manual instructions apply; `inMiniApp` — running inside a
* Telegram/VK Mini App. The CTA is offered only on the plain web and never once installed.
*/
export function resolveInstallMode(facts: {
deferredAvailable: boolean;
standalone: boolean;
iosSafari: boolean;
inMiniApp: boolean;
}): InstallMode {
if (facts.inMiniApp || facts.standalone) return 'hidden';
if (facts.deferredAvailable) return 'oneTap';
if (facts.iosSafari) return 'iosInstructions';
return 'hidden';
}