-
Notifications
You must be signed in to change notification settings - Fork 406
[Website] Fix service worker cache purging to support multi-tab scenarios #3414
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
Draft
brandonpayton
wants to merge
4
commits into
trunk
Choose a base branch
from
ff-loading-issues-with-multiple-tabs
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−42
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aa092b1
Update stale Firefox comment on isCurrentServiceWorkerActive
brandonpayton e97877b
Fall back to old caches in cacheFirstFetch for multi-tab resilience
brandonpayton 9b5b261
Use purgeExcessOldCaches in service worker activate handler
brandonpayton 2912b06
Re-enable and fix multi-tab deployment E2E test
brandonpayton 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 |
|---|---|---|
|
|
@@ -18,6 +18,14 @@ export async function cacheFirstFetch(request: Request): Promise<Response> { | |
| return cachedResponse; | ||
| } | ||
|
|
||
| // After a service worker update, old tabs may still reference | ||
| // hashed asset URLs from the previous build. Search older | ||
| // caches before going to the network so those tabs don't 404. | ||
| const previousVersionResponse = await matchInPreviousVersionCaches(request); | ||
| if (previousVersionResponse) { | ||
| return previousVersionResponse; | ||
| } | ||
|
|
||
| /** | ||
| * Strip the Range header if present. Safari sometimes adds Range headers | ||
| * to requests, which results in 206 Partial Content responses that can't | ||
|
|
@@ -153,6 +161,55 @@ export async function purgeEverythingFromPreviousRelease() { | |
| return Promise.all(oldKeys.map((key) => caches.delete(key))); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes all but the most recent old cache so that tabs still | ||
| * running the previous build can find their hashed assets. | ||
| * | ||
| * Bounds total cache storage to ~3 versions (current + up to 2 old). | ||
| * The single surviving old cache will be cleaned up on the *next* | ||
| * deploy's activation. | ||
| */ | ||
| export async function purgeExcessOldCaches() { | ||
| const allNames = await caches.keys(); | ||
| const oldNames = allNames.filter( | ||
| (name) => | ||
| name.startsWith(CACHE_NAME_PREFIX) && name !== LATEST_CACHE_NAME | ||
| ); | ||
| if (oldNames.length <= 1) { | ||
| return; | ||
| } | ||
| // Keep the last entry (typically the most recent old cache) | ||
| // and delete the rest. | ||
| const toDelete = oldNames.slice(0, -1); | ||
|
Comment on lines
+181
to
+183
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit weak to depend up on the last being "typically the most recent". We need to look more closely at this. |
||
| await Promise.all(toDelete.map((name) => caches.delete(name))); | ||
| } | ||
|
|
||
| /** | ||
| * Searches all previous-version playground caches for a matching | ||
| * response. Used as a fallback in `cacheFirstFetch` so that old | ||
| * tabs can still load hashed assets after a service worker update. | ||
| */ | ||
| async function matchInPreviousVersionCaches( | ||
| request: Request | ||
| ): Promise<Response | undefined> { | ||
| const allCacheNames = await caches.keys(); | ||
| for (const cacheName of allCacheNames) { | ||
| if ( | ||
| cacheName.startsWith(CACHE_NAME_PREFIX) && | ||
| cacheName !== LATEST_CACHE_NAME | ||
| ) { | ||
| const cache = await caches.open(cacheName); | ||
| const response = await cache.match(request, { | ||
| ignoreSearch: true, | ||
| }); | ||
| if (response) { | ||
| return response; | ||
| } | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Answers whether a given URL has a response in the offline mode cache. | ||
| * Ignores the search part of the URL by default. | ||
|
|
@@ -245,9 +302,15 @@ function fetchFresh(resource: RequestInfo | URL, init?: RequestInit) { | |
| } | ||
|
|
||
| export function isCurrentServiceWorkerActive() { | ||
| // @ts-ignore | ||
| // Firefox doesn't support serviceWorker.state | ||
| if (!('serviceWorker' in self) || !('state' in self.serviceWorker)) { | ||
| // self.serviceWorker.state is available in all major browsers | ||
| // (Firefox added support in v120, Nov 2023). The guard below | ||
| // is kept as a safety net for unusual environments. | ||
| if ( | ||
| !('serviceWorker' in self) || | ||
| // @ts-ignore – ServiceWorkerGlobalScope.serviceWorker is | ||
| // typed without `state` in older lib definitions. | ||
| !('state' in self.serviceWorker) | ||
| ) { | ||
| return true; | ||
| } | ||
| // @ts-ignore | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if all packages produce a hashed filename. I have a vague memory of at least one exception to the name-with-hash approach, but it might have been for Node.js deps consumed by Studio builds. We need to make sure.