diff --git a/web/src/lib/pwa.ts b/web/src/lib/pwa.ts index b59f62f..af1b392 100644 --- a/web/src/lib/pwa.ts +++ b/web/src/lib/pwa.ts @@ -47,6 +47,20 @@ export function registerServiceWorker() { }) } +/** + * Looks for a new version now. The room socket calls it when it connects + * again: a deploy restarts the server and drops every socket, so a box + * (which takes new versions by itself) is up to date within moments, + * not an hour later. + */ +export function checkForUpdate() { + if (!import.meta.env.PROD || !('serviceWorker' in navigator)) return + void navigator.serviceWorker + .getRegistration() + .then((reg) => reg?.update()) + .catch(() => {}) +} + /** Chromium's install prompt event, which TypeScript's DOM types lack. */ type InstallPromptEvent = Event & { prompt(): Promise; userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }> } diff --git a/web/src/lib/room-socket.ts b/web/src/lib/room-socket.ts index 4386268..6303607 100644 --- a/web/src/lib/room-socket.ts +++ b/web/src/lib/room-socket.ts @@ -13,6 +13,7 @@ import { gameRoundQuery, gameScoresQuery, type GameRound, type GameScores } from import { guestPassQuery, guestsQuery } from './guests' import { crown, heartsQuery, nightsQuery, type Hearts, type Night } from './nights' import { newer, playbackQuery, type Playback } from './playback' +import { checkForUpdate } from './pwa' import { putQueueGame, queueGamesQuery, type QueueGame } from './queue-games' import { addReaction, type Reaction } from './reactions' import { leaveRoom, queueQuery, roomsQuery, type QueueSnapshot, type Room } from './room' @@ -68,6 +69,7 @@ export function useRoomSocket(roomId: string | undefined, { display = false, onS let retry: ReturnType | undefined let attempt = 0 let closed = false + let opened = false live.set({ roomId, status: 'connecting', members: [] }) const connect = () => { @@ -93,6 +95,9 @@ export function useRoomSocket(roomId: string | undefined, { display = false, onS // as soon as it's connected, and whenever boxd says something new. sock.onopen = () => { if (reportBox && box.info) sendBoxStatus(sock, box.info) + // Back after a drop, maybe a deploy: there may be a new version. + if (opened) checkForUpdate() + opened = true } sock.onmessage = (e) => { attempt = 0