dea08f9c47
Adds the manifest, icons and service worker that make the app installable, and offers it as a toast once Chrome says it qualifies. Declining snoozes the offer for a month; installing ends it. A waiting service worker never activates on its own. Reloading the page under a listener to swap in a new build would cut the song they are in the middle of, so updates land on the next cold start instead. Audio is kept out of the cache entirely: range requests and multi-megabyte bodies do not belong in a shell cache. Artwork is cached, and the SPA navigation fallback denies /api so it cannot swallow the event stream. Installed on Android the app paints edge to edge, so the transport pads itself past the gesture bar. MediaSession gains setPositionState, which is what gives the notification shade a seek bar that moves. Three things kept the bundle from ever being compressed, each hiding the next: the nginx image ships with gzip off, gzip_proxied defaults to off and skips anything carrying a Via header, and gzip_http_version defaults to 1.1 while the host proxy speaks 1.0. With those fixed and the pages split per route, the first load goes from 555KB to 60KB of app code plus a vendor chunk that survives redeploys. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useToastStore, toast } from '../store/useToastStore';
|
|
|
|
/**
|
|
* Chrome fires `beforeinstallprompt` when the app qualifies for installation.
|
|
* Not in lib.dom yet, so the shape is declared here.
|
|
*/
|
|
interface BeforeInstallPromptEvent extends Event {
|
|
prompt: () => Promise<void>;
|
|
userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }>;
|
|
}
|
|
|
|
const SNOOZE_KEY = 'muzick.install-prompt.snoozed-at';
|
|
const SNOOZE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
// Long enough that the suggestion lands after the app has proved useful, not
|
|
// while the first page is still painting.
|
|
const DELAY_MS = 20_000;
|
|
|
|
function snoozed(): boolean {
|
|
try {
|
|
const at = Number(localStorage.getItem(SNOOZE_KEY));
|
|
return Number.isFinite(at) && at > 0 && Date.now() - at < SNOOZE_MS;
|
|
} catch {
|
|
return false; // Private mode or blocked storage — just ask.
|
|
}
|
|
}
|
|
|
|
function snooze() {
|
|
try {
|
|
localStorage.setItem(SNOOZE_KEY, String(Date.now()));
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Offers "Add to Home screen" as a toast, once the browser says the app is
|
|
* installable. Declining snoozes the suggestion for a month; installing
|
|
* silences it for good.
|
|
*/
|
|
export function useInstallPrompt() {
|
|
useEffect(() => {
|
|
// Already installed — the standalone display mode is the reliable signal.
|
|
if (window.matchMedia?.('(display-mode: standalone)').matches) return;
|
|
if (snoozed()) return;
|
|
|
|
let deferred: BeforeInstallPromptEvent | null = null;
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
let unsubscribe: (() => void) | undefined;
|
|
|
|
const offer = () => {
|
|
if (!deferred) return;
|
|
const event = deferred;
|
|
let accepted = false;
|
|
|
|
const id = toast.info('Muzick runs better from your home screen.', {
|
|
ttl: 0,
|
|
action: {
|
|
label: 'Install',
|
|
onClick: () => {
|
|
accepted = true;
|
|
unsubscribe?.();
|
|
// A prompt can only be shown once per event; drop it either way.
|
|
deferred = null;
|
|
void event.prompt().then(() => event.userChoice).then((choice) => {
|
|
if (choice.outcome === 'dismissed') snooze();
|
|
}).catch(() => snooze());
|
|
},
|
|
},
|
|
});
|
|
|
|
// The toast's own close button calls dismiss() directly, so the only way
|
|
// to notice a decline is to watch the toast leave the store.
|
|
unsubscribe = useToastStore.subscribe((state) => {
|
|
if (accepted) return;
|
|
if (!state.toasts.some((t) => t.id === id)) {
|
|
snooze();
|
|
unsubscribe?.();
|
|
}
|
|
});
|
|
};
|
|
|
|
const onBeforeInstallPrompt = (e: Event) => {
|
|
// Suppress Chrome's own mini-infobar so only our toast asks.
|
|
e.preventDefault();
|
|
deferred = e as BeforeInstallPromptEvent;
|
|
timer = setTimeout(offer, DELAY_MS);
|
|
};
|
|
|
|
const onInstalled = () => {
|
|
deferred = null;
|
|
if (timer) clearTimeout(timer);
|
|
snooze();
|
|
};
|
|
|
|
window.addEventListener('beforeinstallprompt', onBeforeInstallPrompt);
|
|
window.addEventListener('appinstalled', onInstalled);
|
|
|
|
return () => {
|
|
window.removeEventListener('beforeinstallprompt', onBeforeInstallPrompt);
|
|
window.removeEventListener('appinstalled', onInstalled);
|
|
if (timer) clearTimeout(timer);
|
|
unsubscribe?.();
|
|
};
|
|
}, []);
|
|
}
|