Repository navigation
fix: cache project favicons across web and mobile #4767
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
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bcd7c2
fix: cache project favicons across web and mobile
gabrielelpidio f77304e
fix: restore favicon load error fallback
gabrielelpidio 275cf65
fix: harden favicon cache invalidation
gabrielelpidio fb71493
fix: ignore stale favicon load errors
gabrielelpidio 6ca94bf
fix: make favicon refresh ordering deterministic
gabrielelpidio 6ada303
fix(mobile): register favicon requests after commit
gabrielelpidio 0d0779c
fix(mobile): bound favicon request state
gabrielelpidio 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
Some comments aren't visible on the classic Files Changed page.
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
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,128 @@ | ||
| import type { ComponentType, Dispatch, ReactElement, SetStateAction } from "react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; | ||
| import type { EnvironmentId } from "@t3tools/contracts"; | ||
|
|
||
| const testState = vi.hoisted(() => ({ | ||
| faviconUrl: "https://environment.test/api/assets/token-a/v1-20-favicon.svg", | ||
| })); | ||
|
|
||
| const hooks = vi.hoisted(() => { | ||
| let cursor = 0; | ||
| let slots: unknown[] = []; | ||
| const nextIndex = () => cursor++; | ||
|
|
||
| return { | ||
| beginRender() { | ||
| cursor = 0; | ||
| }, | ||
| reset() { | ||
| cursor = 0; | ||
| slots = []; | ||
| }, | ||
| useMemoCache(size: number): unknown[] { | ||
| const index = nextIndex(); | ||
| if (!slots[index]) { | ||
| slots[index] = Array.from({ length: size }, () => Symbol.for("react.memo_cache_sentinel")); | ||
| } | ||
| return slots[index] as unknown[]; | ||
| }, | ||
| useState<T>(initialValue: T | (() => T)): [T, Dispatch<SetStateAction<T>>] { | ||
| const index = nextIndex(); | ||
| if (index >= slots.length) { | ||
| slots[index] = | ||
| typeof initialValue === "function" ? (initialValue as () => T)() : initialValue; | ||
| } | ||
| const setValue: Dispatch<SetStateAction<T>> = (nextValue) => { | ||
| const previous = slots[index] as T; | ||
| slots[index] = | ||
| typeof nextValue === "function" ? (nextValue as (value: T) => T)(previous) : nextValue; | ||
| }; | ||
| return [slots[index] as T, setValue]; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock("react", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("react")>(); | ||
| return { | ||
| ...actual, | ||
| useState: hooks.useState, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock("react/compiler-runtime", () => ({ c: hooks.useMemoCache })); | ||
| vi.mock("../assets/assetUrls", () => ({ | ||
| useAssetUrl: () => testState.faviconUrl, | ||
| })); | ||
|
|
||
| import { ProjectFavicon } from "./ProjectFavicon"; | ||
|
|
||
| type ProjectFaviconImageProps = { | ||
| readonly cacheKey: string; | ||
| readonly src: string; | ||
| readonly className?: string | undefined; | ||
| readonly fallbackIcon: ComponentType<{ className?: string }>; | ||
| }; | ||
|
|
||
| type ImageElement = ReactElement<{ | ||
| readonly src: string; | ||
| readonly onLoad?: () => void; | ||
| readonly onError?: () => void; | ||
| }>; | ||
|
|
||
| type ProjectFaviconImageElement = ReactElement<{ | ||
| readonly children: [ReactElement | null, ImageElement | null, ImageElement | null]; | ||
| }>; | ||
|
|
||
| function resolveImageComponent(): { | ||
| readonly Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement; | ||
| readonly props: ProjectFaviconImageProps; | ||
| } { | ||
| hooks.beginRender(); | ||
| const element = ProjectFavicon({ | ||
| environmentId: "environment-test" as EnvironmentId, | ||
| cwd: "/workspace-test", | ||
| }) as ReactElement<ProjectFaviconImageProps>; | ||
| hooks.reset(); | ||
|
|
||
| return { | ||
| Component: element.type as (props: ProjectFaviconImageProps) => ProjectFaviconImageElement, | ||
| props: element.props, | ||
| }; | ||
| } | ||
|
|
||
| function renderImage( | ||
| Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement, | ||
| props: ProjectFaviconImageProps, | ||
| ): ProjectFaviconImageElement { | ||
| hooks.beginRender(); | ||
| return Component(props); | ||
| } | ||
|
|
||
| describe("ProjectFavicon", () => { | ||
| beforeEach(() => { | ||
| hooks.reset(); | ||
| }); | ||
|
|
||
| it("falls back when the displayed favicon fails without discarding a valid older image early", () => { | ||
| const { Component, props } = resolveImageComponent(); | ||
| const initialLoadingImage = renderImage(Component, props).props.children[2]; | ||
| initialLoadingImage?.props.onLoad?.(); | ||
|
|
||
| const refreshedProps = { | ||
| ...props, | ||
| src: "https://environment.test/api/assets/token-b/v1-20-favicon.svg", | ||
| }; | ||
| const refreshing = renderImage(Component, refreshedProps).props.children; | ||
| expect(refreshing[1]?.props.src).toBe(props.src); | ||
| refreshing[2]?.props.onError?.(); | ||
|
|
||
| const afterRefreshError = renderImage(Component, refreshedProps).props.children; | ||
| expect(afterRefreshError[1]?.props.src).toBe(props.src); | ||
| afterRefreshError[1]?.props.onError?.(); | ||
|
|
||
| const afterDisplayedError = renderImage(Component, refreshedProps).props.children; | ||
| expect(afterDisplayedError[0]).not.toBeNull(); | ||
| expect(afterDisplayedError[1]).toBeNull(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.