-
Notifications
You must be signed in to change notification settings - Fork 54
fix(ui): prevent white screen when the app mounts invisible #3210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+203
−7
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
945ba39
prevent white screen when app mounts invisible
charlesvien 279ccdb
keep app fade-in running while window is blurred
charlesvien 128e445
cover watchdog cleanup, re-arm and null-ref paths
charlesvien fb8c197
read computed opacity without the dead fallback
charlesvien e63a6f3
parameterize watchdog visibility cases
charlesvien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("@posthog/ui/shell/analytics", () => ({ | ||
| captureException: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@posthog/ui/shell/logger", () => ({ | ||
| logger: { | ||
| scope: () => ({ | ||
| error: vi.fn(), | ||
| warn: vi.fn(), | ||
| info: vi.fn(), | ||
| debug: vi.fn(), | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| import { captureException } from "@posthog/ui/shell/analytics"; | ||
| import { useAppVisibilityWatchdog } from "./useAppVisibilityWatchdog"; | ||
|
|
||
| function mountElement(opacity: string, width: number, height: number) { | ||
| const element = document.createElement("div"); | ||
| element.style.opacity = opacity; | ||
| element.getBoundingClientRect = () => ({ width, height }) as DOMRect; | ||
| document.body.append(element); | ||
| return element; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.runOnlyPendingTimers(); | ||
| vi.useRealTimers(); | ||
| vi.mocked(captureException).mockClear(); | ||
| document.body.innerHTML = ""; | ||
| }); | ||
|
|
||
| describe("useAppVisibilityWatchdog", () => { | ||
| it.each([ | ||
| { | ||
| name: "invisible via opacity", | ||
| opacity: "0", | ||
| width: 1200, | ||
| height: 800, | ||
| active: true, | ||
| reports: true, | ||
| }, | ||
| { | ||
| name: "collapsed to zero size", | ||
| opacity: "1", | ||
| width: 0, | ||
| height: 0, | ||
| active: true, | ||
| reports: true, | ||
| }, | ||
| { | ||
| name: "visible", | ||
| opacity: "1", | ||
| width: 1200, | ||
| height: 800, | ||
| active: true, | ||
| reports: false, | ||
| }, | ||
| { | ||
| name: "inactive", | ||
| opacity: "0", | ||
| width: 1200, | ||
| height: 800, | ||
| active: false, | ||
| reports: false, | ||
| }, | ||
| ])( | ||
| "reports=$reports when $name", | ||
| ({ opacity, width, height, active, reports }) => { | ||
| const ref = { current: mountElement(opacity, width, height) }; | ||
| renderHook(() => useAppVisibilityWatchdog(ref, active)); | ||
|
|
||
| vi.advanceTimersByTime(3000); | ||
|
|
||
| expect(captureException).toHaveBeenCalledTimes(reports ? 1 : 0); | ||
| }, | ||
| ); | ||
|
|
||
| it("reports the element's opacity and source", () => { | ||
| const ref = { current: mountElement("0", 1200, 800) }; | ||
| renderHook(() => useAppVisibilityWatchdog(ref, true)); | ||
|
|
||
| vi.advanceTimersByTime(3000); | ||
|
|
||
| expect(captureException).toHaveBeenCalledWith( | ||
| expect.any(Error), | ||
| expect.objectContaining({ | ||
| source: "app-visibility-watchdog", | ||
| opacity: 0, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("does not report after unmounting before the deadline", () => { | ||
| const ref = { current: mountElement("0", 1200, 800) }; | ||
| const { unmount } = renderHook(() => useAppVisibilityWatchdog(ref, true)); | ||
|
|
||
| unmount(); | ||
| vi.advanceTimersByTime(3000); | ||
|
|
||
| expect(captureException).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("arms when active flips from false to true", () => { | ||
| const ref = { current: mountElement("0", 1200, 800) }; | ||
| const { rerender } = renderHook( | ||
| ({ active }) => useAppVisibilityWatchdog(ref, active), | ||
| { initialProps: { active: false } }, | ||
| ); | ||
|
|
||
| vi.advanceTimersByTime(3000); | ||
| expect(captureException).not.toHaveBeenCalled(); | ||
|
|
||
| rerender({ active: true }); | ||
| vi.advanceTimersByTime(3000); | ||
| expect(captureException).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("does nothing when the ref never attaches", () => { | ||
| const ref = { current: null }; | ||
| renderHook(() => useAppVisibilityWatchdog(ref, true)); | ||
|
|
||
| vi.advanceTimersByTime(3000); | ||
|
|
||
| expect(captureException).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { captureException } from "@posthog/ui/shell/analytics"; | ||
| import { logger } from "@posthog/ui/shell/logger"; | ||
| import { type RefObject, useEffect } from "react"; | ||
|
|
||
| const log = logger.scope("app-visibility"); | ||
| const VISIBILITY_CHECK_DELAY_MS = 3000; | ||
|
|
||
| // Detects the "white screen but app alive" state: mounted and interactive yet stuck invisible. | ||
| export function useAppVisibilityWatchdog( | ||
| ref: RefObject<HTMLElement | null>, | ||
| active: boolean, | ||
| ): void { | ||
| useEffect(() => { | ||
| if (!active) return; | ||
| const timer = setTimeout(() => { | ||
| const element = ref.current; | ||
| if (!element) return; | ||
| const computedOpacity = getComputedStyle(element).opacity; | ||
| const opacity = computedOpacity ? Number.parseFloat(computedOpacity) : 1; | ||
| const rect = element.getBoundingClientRect(); | ||
| if (opacity >= 0.01 && rect.width > 0 && rect.height > 0) return; | ||
| const detail = { | ||
| opacity, | ||
| width: Math.round(rect.width), | ||
| height: Math.round(rect.height), | ||
| route: window.location.hash, | ||
| }; | ||
| log.error("Main app mounted but not visible", detail); | ||
| captureException(new Error("Main app mounted but not visible"), { | ||
| ...detail, | ||
| source: "app-visibility-watchdog", | ||
| }); | ||
| }, VISIBILITY_CHECK_DELAY_MS); | ||
| return () => clearTimeout(timer); | ||
| }, [ref, active]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.