Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/components/layout/app-runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Button } from "@/components/ui/button"
import { requireTranslationValue } from "@/core/i18n/i18n"
import { useLang } from "@/core/i18n/lang-provider"
import {
isPwaInstalled,
PWA_INSTALL_INSTALLED_KEY,
PWA_INSTALL_SESSION_PROMPTED_KEY,
type BeforeInstallPromptEvent,
Expand Down Expand Up @@ -35,19 +36,6 @@ type AppRuntimeProps = {
onInstallPromptConsumed?: () => void
}

function isPwaInstalled(): boolean {
if (typeof window === "undefined") return false
const standaloneDisplayMode = window.matchMedia("(display-mode: standalone)").matches
const iosStandalone = (window.navigator as Navigator & { standalone?: boolean }).standalone === true
let persistedInstalledFlag = false
try {
persistedInstalledFlag = window.localStorage.getItem(PWA_INSTALL_INSTALLED_KEY) === "1"
} catch {
persistedInstalledFlag = false
}
return standaloneDisplayMode || iosStandalone || persistedInstalledFlag
}

function setPwaInstallStorageValue(key: string, value: string): void {
try {
window.localStorage.setItem(key, value)
Expand Down
22 changes: 22 additions & 0 deletions src/core/pwa/install-prompt-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ type PwaInstallPromptWindow = Window & {
[PWA_INSTALL_PROMPT_SLOT]?: BeforeInstallPromptEvent | null
}

export function isPwaInstalled(): boolean {
if (typeof window === "undefined") return false

let standaloneDisplayMode = false
try {
standaloneDisplayMode = typeof window.matchMedia === "function"
&& window.matchMedia("(display-mode: standalone)").matches
} catch {
standaloneDisplayMode = false
}

const iosStandalone = (window.navigator as Navigator & { standalone?: boolean }).standalone === true
let persistedInstalledFlag = false
try {
persistedInstalledFlag = window.localStorage.getItem(PWA_INSTALL_INSTALLED_KEY) === "1"
} catch {
persistedInstalledFlag = false
}

return standaloneDisplayMode || iosStandalone || persistedInstalledFlag
}

function readWindowPrompt(): BeforeInstallPromptEvent | null {
if (typeof window === "undefined") return null
return (window as PwaInstallPromptWindow)[PWA_INSTALL_PROMPT_SLOT] ?? null
Expand Down
10 changes: 2 additions & 8 deletions src/features/install-app/components/install-app-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ import type { Locale } from "@/core/i18n/i18n"
import { JsonLdScript } from "@/core/seo/components/json-ld-script"
import {
consumePwaInstallPrompt,
isPwaInstalled,
usePwaInstallPrompt,
} from "@/core/pwa/install-prompt-store"
import { clearByteflowPwaCaches } from "@/core/storage/pwa-cache-controls"
import type { GuidePlatform, InstallPageCopy } from "@/core/utils/install-app-copy"
import { StaticPageContainer } from "@/components/layout/page-container"
import { detectInstallGuidePlatform } from "../install-guide-platform"

function isStandaloneInstalled() {
if (typeof window === "undefined") return false
const standaloneDisplayMode = window.matchMedia("(display-mode: standalone)").matches
const iosStandalone = (window.navigator as Navigator & { standalone?: boolean }).standalone === true
return standaloneDisplayMode || iosStandalone
}

const BENEFIT_ICON_BY_KEY = {
instant_launch: Zap,
works_offline: WifiOff,
Expand Down Expand Up @@ -81,7 +75,7 @@ export function InstallAppClient({
}, [locale])

React.useEffect(() => {
setInstalled(isStandaloneInstalled())
setInstalled(isPwaInstalled())

const onInstalled = () => {
setInstalled(true)
Expand Down
2 changes: 1 addition & 1 deletion src/generated/route-width-inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@
{
"file": "src/components/layout/app-runtime.tsx",
"element": "div",
"line": 267,
"line": 255,
"reason": "runtime-banner",
"maxWidthTokens": [
"max-w-6xl"
Expand Down
41 changes: 41 additions & 0 deletions tests/component/install-app-page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
capturePwaInstallPrompt,
consumePwaInstallPrompt,
getPwaInstallPromptSnapshot,
PWA_INSTALL_INSTALLED_KEY,
} from "@/core/pwa/install-prompt-store"
import { getAllToolsHref } from "@/core/routing/all-tools-route"
import { getInstallPageCopy } from "@/core/utils/install-app-copy"
Expand All @@ -30,6 +31,7 @@ vi.mock("@/core/analytics/analytics", () => ({
describe("install app page", () => {
beforeEach(() => {
consumePwaInstallPrompt()
window.localStorage.clear()
if (!window.matchMedia) {
Object.defineProperty(window, "matchMedia", {
writable: true,
Expand Down Expand Up @@ -204,4 +206,43 @@ describe("install app page", () => {
await waitFor(() => expect(getPwaInstallPromptSnapshot()).toBeNull())
await waitFor(() => expect(screen.getAllByText(copy.manualHint).length).toBeGreaterThan(0))
})

it("restores persisted install evidence and clears it when a fresh prompt arrives", async () => {
const copy = getInstallPageCopy("en")
window.localStorage.setItem(PWA_INSTALL_INSTALLED_KEY, "1")

render(
<InstallAppClient
locale="en"
copy={copy}
allToolsLabel="All tools"
trustCenterLabel="Trust Center"
localDataControlsLabel="Local data controls"
distributionResearchLabel="Extension and desktop research"
offlineMatrixTitle="Offline support matrix"
offlineMatrixDescription="Review which workflows keep running after cache warm-up."
offlineMatrixLink="Offline matrix"
/>,
)

await waitFor(() => {
const installedActions = screen.getAllByRole("button", { name: copy.alreadyInstalled })
expect(installedActions).toHaveLength(2)
installedActions.forEach((action) => expect(action).toBeDisabled())
})

const beforeInstallPromptEvent = Object.assign(new Event("beforeinstallprompt", { cancelable: true }), {
prompt: vi.fn().mockResolvedValue(undefined),
userChoice: Promise.resolve({ outcome: "dismissed", platform: "web" }),
})
await act(async () => {
capturePwaInstallPrompt(beforeInstallPromptEvent)
})

expect(window.localStorage.getItem(PWA_INSTALL_INSTALLED_KEY)).toBeNull()
await waitFor(() => {
expect(screen.getAllByRole("button", { name: copy.installNow })).toHaveLength(2)
})
expect(screen.queryByRole("button", { name: copy.alreadyInstalled })).not.toBeInTheDocument()
})
})