-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 all 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
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,108 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { | ||
| beginProjectFaviconRequest, | ||
| createProjectFaviconRequest, | ||
| hasLoadedProjectFavicon, | ||
| markProjectFaviconFailed, | ||
| markProjectFaviconLoaded, | ||
| } from "./projectFaviconCache"; | ||
|
|
||
| describe("project favicon cache", () => { | ||
| it("ignores callbacks from a superseded URL", () => { | ||
| const cacheKey = "environment-1:/workspace:v1-favicon.svg"; | ||
| const expiredUrl = "https://environment.example/api/assets/expired/v1-favicon.svg"; | ||
| const refreshedUrl = "https://environment.example/api/assets/refreshed/v1-favicon.svg"; | ||
|
|
||
| const expiredRequest = createProjectFaviconRequest(cacheKey, expiredUrl); | ||
| const endExpiredRequest = beginProjectFaviconRequest(expiredRequest); | ||
| markProjectFaviconLoaded(expiredRequest); | ||
| const refreshedRequest = createProjectFaviconRequest(cacheKey, refreshedUrl); | ||
| const endRefreshedRequest = beginProjectFaviconRequest(refreshedRequest); | ||
|
|
||
| expect(markProjectFaviconLoaded(expiredRequest)).toBe(false); | ||
| expect(markProjectFaviconFailed(expiredRequest)).toBe(false); | ||
| expect(hasLoadedProjectFavicon(cacheKey)).toBe(true); | ||
| expect(markProjectFaviconFailed(refreshedRequest)).toBe(true); | ||
| expect(hasLoadedProjectFavicon(cacheKey)).toBe(false); | ||
|
|
||
| endRefreshedRequest(); | ||
| endExpiredRequest(); | ||
| }); | ||
|
|
||
| it("evicts the URL that actually failed", () => { | ||
| const cacheKey = "environment-1:/workspace:v2-favicon.svg"; | ||
| const faviconUrl = "https://environment.example/api/assets/current/v2-favicon.svg"; | ||
| const request = createProjectFaviconRequest(cacheKey, faviconUrl); | ||
| const endRequest = beginProjectFaviconRequest(request); | ||
|
|
||
| markProjectFaviconLoaded(request); | ||
|
|
||
| expect(markProjectFaviconFailed(request)).toBe(true); | ||
| expect(hasLoadedProjectFavicon(cacheKey)).toBe(false); | ||
|
|
||
| endRequest(); | ||
| }); | ||
|
|
||
| it("does not supersede a request until the next request begins", () => { | ||
| const cacheKey = "environment-1:/workspace:v3-favicon.svg"; | ||
| const committedUrl = "https://environment.example/api/assets/current/v3-favicon.svg"; | ||
| const abandonedUrl = "https://environment.example/api/assets/abandoned/v3-favicon.svg"; | ||
| const committedRequest = createProjectFaviconRequest(cacheKey, committedUrl); | ||
| const endCommittedRequest = beginProjectFaviconRequest(committedRequest); | ||
|
|
||
| createProjectFaviconRequest(cacheKey, abandonedUrl); | ||
|
|
||
| expect(markProjectFaviconLoaded(committedRequest)).toBe(true); | ||
| expect(hasLoadedProjectFavicon(cacheKey)).toBe(true); | ||
|
|
||
| endCommittedRequest(); | ||
| }); | ||
|
|
||
| it("requires a cache key before creating a URL-bearing request", () => { | ||
| const firstUrl = "https://environment.example/api/assets/first/favicon.svg"; | ||
| const secondUrl = "https://environment.example/api/assets/second/favicon.svg"; | ||
|
|
||
| expect(createProjectFaviconRequest(null, firstUrl)).toBeNull(); | ||
| expect(createProjectFaviconRequest(null, secondUrl)).toBeNull(); | ||
| }); | ||
|
|
||
| it("restores the remaining active URL when a newer request ends", () => { | ||
| const cacheKey = "environment-1:/workspace:v4-favicon.svg"; | ||
| const firstRequest = createProjectFaviconRequest( | ||
| cacheKey, | ||
| "https://environment.example/api/assets/first/v4-favicon.svg", | ||
| ); | ||
| const secondRequest = createProjectFaviconRequest( | ||
| cacheKey, | ||
| "https://environment.example/api/assets/second/v4-favicon.svg", | ||
| ); | ||
| const endFirstRequest = beginProjectFaviconRequest(firstRequest); | ||
| const endSecondRequest = beginProjectFaviconRequest(secondRequest); | ||
|
|
||
| expect(markProjectFaviconLoaded(firstRequest)).toBe(false); | ||
| endSecondRequest(); | ||
| expect(markProjectFaviconLoaded(firstRequest)).toBe(true); | ||
| endFirstRequest(); | ||
| expect(markProjectFaviconLoaded(firstRequest)).toBe(false); | ||
| }); | ||
|
|
||
| it("bounds remembered loaded revisions", () => { | ||
| const firstCacheKey = "environment-1:/workspace:revision-0"; | ||
| let lastCacheKey = firstCacheKey; | ||
|
|
||
| for (let revision = 0; revision < 300; revision++) { | ||
| lastCacheKey = `environment-1:/workspace:revision-${revision}`; | ||
| const request = createProjectFaviconRequest( | ||
| lastCacheKey, | ||
| `https://environment.example/api/assets/revision-${revision}/favicon.svg`, | ||
| ); | ||
| const endRequest = beginProjectFaviconRequest(request); | ||
| markProjectFaviconLoaded(request); | ||
| endRequest(); | ||
| } | ||
|
|
||
| expect(hasLoadedProjectFavicon(firstCacheKey)).toBe(false); | ||
| expect(hasLoadedProjectFavicon(lastCacheKey)).toBe(true); | ||
| }); | ||
| }); |
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,94 @@ | ||
| export interface ProjectFaviconRequest { | ||
| readonly cacheKey: string; | ||
| readonly faviconUrl: string; | ||
| } | ||
|
|
||
| interface ActiveFaviconRequests { | ||
| readonly urls: Map<string, number>; | ||
| currentUrl: string; | ||
| } | ||
|
|
||
| const MAX_LOADED_FAVICONS = 256; | ||
| const activeFaviconRequests = new Map<string, ActiveFaviconRequests>(); | ||
| const loadedFaviconKeys = new Map<string, true>(); | ||
|
|
||
| export function createProjectFaviconRequest( | ||
| cacheKey: string, | ||
| faviconUrl: string, | ||
| ): ProjectFaviconRequest; | ||
| export function createProjectFaviconRequest( | ||
| cacheKey: string | null, | ||
| faviconUrl: string | null, | ||
| ): ProjectFaviconRequest | null; | ||
| export function createProjectFaviconRequest(cacheKey: string | null, faviconUrl: string | null) { | ||
| if (!cacheKey || !faviconUrl) return null; | ||
| return { cacheKey, faviconUrl }; | ||
| } | ||
|
|
||
| export function beginProjectFaviconRequest(request: ProjectFaviconRequest) { | ||
| let activeRequests = activeFaviconRequests.get(request.cacheKey); | ||
| if (!activeRequests) { | ||
| activeRequests = { currentUrl: request.faviconUrl, urls: new Map() }; | ||
| activeFaviconRequests.set(request.cacheKey, activeRequests); | ||
| } | ||
|
|
||
| const activeCount = activeRequests.urls.get(request.faviconUrl) ?? 0; | ||
| activeRequests.urls.delete(request.faviconUrl); | ||
| activeRequests.urls.set(request.faviconUrl, activeCount + 1); | ||
| activeRequests.currentUrl = request.faviconUrl; | ||
|
|
||
| let ended = false; | ||
| return () => { | ||
| if (ended) return; | ||
| ended = true; | ||
|
|
||
| const remainingCount = (activeRequests.urls.get(request.faviconUrl) ?? 1) - 1; | ||
| if (remainingCount > 0) { | ||
| activeRequests.urls.set(request.faviconUrl, remainingCount); | ||
| return; | ||
| } | ||
|
|
||
| activeRequests.urls.delete(request.faviconUrl); | ||
| if (activeRequests.urls.size === 0) { | ||
| if (activeFaviconRequests.get(request.cacheKey) === activeRequests) { | ||
| activeFaviconRequests.delete(request.cacheKey); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (activeRequests.currentUrl === request.faviconUrl) { | ||
| activeRequests.currentUrl = Array.from(activeRequests.urls.keys()).at(-1)!; | ||
| } | ||
| }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| export function hasLoadedProjectFavicon(cacheKey: string | null) { | ||
| return cacheKey !== null && loadedFaviconKeys.has(cacheKey); | ||
| } | ||
|
|
||
| function isCurrentProjectFaviconRequest(request: ProjectFaviconRequest) { | ||
| return activeFaviconRequests.get(request.cacheKey)?.currentUrl === request.faviconUrl; | ||
| } | ||
|
|
||
| function rememberLoadedProjectFavicon(cacheKey: string) { | ||
| loadedFaviconKeys.delete(cacheKey); | ||
| loadedFaviconKeys.set(cacheKey, true); | ||
|
|
||
| if (loadedFaviconKeys.size > MAX_LOADED_FAVICONS) { | ||
| loadedFaviconKeys.delete(loadedFaviconKeys.keys().next().value!); | ||
| } | ||
| } | ||
|
|
||
| export function markProjectFaviconLoaded(request: ProjectFaviconRequest) { | ||
| if (!isCurrentProjectFaviconRequest(request)) return false; | ||
|
|
||
| rememberLoadedProjectFavicon(request.cacheKey); | ||
| return true; | ||
| } | ||
|
|
||
| export function markProjectFaviconFailed(request: ProjectFaviconRequest) { | ||
| if (!isCurrentProjectFaviconRequest(request)) return false; | ||
|
|
||
| loadedFaviconKeys.delete(request.cacheKey); | ||
| return true; | ||
|
gabrielelpidio marked this conversation as resolved.
|
||
| } | ||
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.