feat(web): in-app PWA update prompt when new service worker is available (#946)

* feat(web): in-app PWA update prompt when new service worker is available (closes #938)

User-controlled reload with a persistent banner, visibility-triggered SW
checks, and an expandable rationale. Switches registerType to prompt.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): align vite.config with soup layers for clean driver merge

Keeps registerType prompt while matching garden IWER stubs and PWA
share_target shape expected by feat/pwa-share-target in the manifest.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Revert "fix(web): align vite.config with soup layers for clean driver merge"

This reverts commit 6f0915b0884d029a2413d8819a4dfe81d7c4e595.

* fix(web): make PWA reload apply waiting service worker updates

Handle SKIP_WAITING in injectManifest sw.ts and reload via controllerchange
with a timed fallback when vite-plugin-pwa prompt mode does not navigate.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): satisfy setTimeout mock typing in PWA reload tests

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): register PWA service worker before auth gates

Mount PwaUpdateProvider at app root and show the update banner on login
and error screens so registerSW runs for logged-out users too.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): offset PWA update banner below top status banners

Reserve top-12 when syncing or reconnecting so the reload prompt stays
visible above SyncingBanner and ReconnectingBanner.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): offset PWA update banner below voice error banner

Use PwaUpdateBannerWithStatusOffset inside VoiceProvider so voice errors
share the same top-12 reservation as sync and reconnect banners.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
HeavyGee
2026-06-18 10:15:10 +08:00
committed by GitHub
co-authored by Cursor
parent 78155a9d27
commit 5f27abddd4
12 changed files with 649 additions and 41 deletions
+139
View File
@@ -0,0 +1,139 @@
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { I18nProvider } from '@/lib/i18n-context'
import { PwaUpdateBanner, PwaUpdateBannerWithStatusOffset } from '@/components/PwaUpdateBanner'
const usePwaUpdateMock = vi.fn()
const useVoiceOptionalMock = vi.fn()
vi.mock('@/lib/pwa-update-context', () => ({
usePwaUpdateContext: () => usePwaUpdateMock(),
}))
vi.mock('@/lib/voice-context', () => ({
useVoiceOptional: () => useVoiceOptionalMock(),
}))
vi.mock('@/hooks/useOnlineStatus', () => ({
useOnlineStatus: () => true,
}))
vi.mock('@/hooks/usePlatform', () => ({
usePlatform: () => ({
haptic: {
impact: vi.fn(),
notification: vi.fn(),
},
}),
}))
function renderBanner() {
return render(
<I18nProvider>
<PwaUpdateBanner />
</I18nProvider>,
)
}
describe('PwaUpdateBanner', () => {
beforeEach(() => {
vi.clearAllMocks()
useVoiceOptionalMock.mockReturnValue(null)
Object.defineProperty(window, 'localStorage', {
value: {
getItem: vi.fn(() => 'en'),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
key: vi.fn(() => null),
length: 0,
},
configurable: true,
})
})
afterEach(() => {
cleanup()
})
it('does not render when no update is available', () => {
usePwaUpdateMock.mockReturnValue({
needRefresh: false,
reload: vi.fn(),
})
renderBanner()
expect(screen.queryByTestId('pwa-update-banner')).not.toBeInTheDocument()
})
it('renders a reload-only banner with no dismiss action', () => {
const reload = vi.fn()
usePwaUpdateMock.mockReturnValue({
needRefresh: true,
reload,
})
renderBanner()
expect(screen.getByTestId('pwa-update-banner')).toBeInTheDocument()
expect(screen.getByText('New version available')).toBeInTheDocument()
expect(screen.getByText('Reload to get the latest HAPI')).toBeInTheDocument()
expect(screen.getAllByRole('button')).toHaveLength(1)
fireEvent.click(screen.getByRole('button', { name: 'Reload' }))
expect(reload).toHaveBeenCalledTimes(1)
})
it('honors a custom top offset when provided', () => {
usePwaUpdateMock.mockReturnValue({
needRefresh: true,
reload: vi.fn(),
})
render(
<I18nProvider>
<PwaUpdateBanner topClassName="top-12" />
</I18nProvider>,
)
expect(screen.getByTestId('pwa-update-banner')).toHaveClass('top-12')
})
it('offsets below voice error banners when shown inside the voice provider', () => {
usePwaUpdateMock.mockReturnValue({
needRefresh: true,
reload: vi.fn(),
})
useVoiceOptionalMock.mockReturnValue({
status: 'error',
errorMessage: 'Mic failed',
})
render(
<I18nProvider>
<PwaUpdateBannerWithStatusOffset isSyncing={false} isReconnecting={false} />
</I18nProvider>,
)
expect(screen.getByTestId('pwa-update-banner')).toHaveClass('top-12')
})
it('expands the rationale section when the disclosure is opened', () => {
usePwaUpdateMock.mockReturnValue({
needRefresh: true,
reload: vi.fn(),
})
renderBanner()
const disclosure = screen.getByText("Why can't I dismiss this?")
expect(screen.queryByText(/agent running/i)).not.toBeVisible()
fireEvent.click(disclosure)
expect(screen.getByText(/agent running/i)).toBeVisible()
expect(screen.getByText(/finish what you are doing first/i)).toBeVisible()
})
})
+73
View File
@@ -0,0 +1,73 @@
import { useOnlineStatus } from '@/hooks/useOnlineStatus'
import { usePlatform } from '@/hooks/usePlatform'
import { usePwaUpdateContext } from '@/lib/pwa-update-context'
import { useTranslation } from '@/lib/use-translation'
import { useVoiceOptional } from '@/lib/voice-context'
export function PwaUpdateBanner({ topClassName }: { topClassName?: string } = {}) {
const { t } = useTranslation()
const { needRefresh, reload } = usePwaUpdateContext()
const isOnline = useOnlineStatus()
const { haptic } = usePlatform()
if (!needRefresh) {
return null
}
const topClass = topClassName ?? (isOnline ? 'top-2' : 'top-10')
return (
<div
data-testid="pwa-update-banner"
className={`fixed left-4 right-4 bg-[var(--app-secondary-bg)] border border-[var(--app-border)] rounded-lg p-4 shadow-lg z-50 ${topClass}`}
>
<div className="flex items-center justify-between gap-3">
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-[var(--app-fg)]">
{t('pwa.update.title')}
</p>
<p className="text-xs text-[var(--app-hint)] mt-0.5">
{t('pwa.update.body')}
</p>
</div>
<button
type="button"
onClick={() => {
haptic.impact('light')
reload()
}}
className="shrink-0 px-4 py-2 bg-[var(--app-fg)] text-[var(--app-bg)] rounded-lg text-sm font-medium active:opacity-80"
>
{t('pwa.update.reload')}
</button>
</div>
<details className="mt-3 border-t border-[var(--app-border)] pt-2">
<summary className="cursor-pointer text-xs text-[var(--app-link)] active:opacity-60 list-none [&::-webkit-details-marker]:hidden">
{t('pwa.update.whyToggle')}
</summary>
<p className="mt-2 text-xs text-[var(--app-hint)] leading-relaxed">
{t('pwa.update.whyBody')}
</p>
</details>
</div>
)
}
export function PwaUpdateBannerWithStatusOffset({
isSyncing,
isReconnecting,
}: {
isSyncing: boolean
isReconnecting: boolean
}) {
const voice = useVoiceOptional()
const hasTopStatusBanner =
isSyncing ||
isReconnecting ||
Boolean(voice && voice.status === 'error' && voice.errorMessage)
return (
<PwaUpdateBanner topClassName={hasTopStatusBanner ? 'top-12' : undefined} />
)
}