From f7bb83e859b48bb3b3aae91aae40c3a550fce463 Mon Sep 17 00:00:00 2001 From: Scott Deeter Date: Tue, 20 Oct 2026 16:18:39 -0700 Subject: [PATCH 01/10] Fix web update refresh delays with network-first navigation --- config/eslint/eslint.config.mjs | 11 ++++ config/rsbuild/rsbuild.common.ts | 26 ++------- cspell.json | 3 + package-lock.json | 7 ++- package.json | 7 ++- src/libs/actions/AppUpdate/updateApp/index.ts | 7 +-- src/libs/actions/Session/clearCache/index.ts | 2 +- src/libs/actions/UpdateRequired/index.ts | 7 +-- .../ErrorPage/UpdateRequiredView/index.tsx | 14 ++++- src/setup/platformSetup/index.ts | 4 +- tsconfig.json | 1 + tsconfig.worker.json | 10 ++++ web/service-worker.ts | 55 +++++++++++++++++++ 13 files changed, 116 insertions(+), 38 deletions(-) create mode 100644 tsconfig.worker.json create mode 100644 web/service-worker.ts diff --git a/config/eslint/eslint.config.mjs b/config/eslint/eslint.config.mjs index 9b6129fdbc7d..357a70023cbb 100644 --- a/config/eslint/eslint.config.mjs +++ b/config/eslint/eslint.config.mjs @@ -790,6 +790,17 @@ const config = defineConfig([ }, }, + { + files: ['web/service-worker.ts'], + languageOptions: { + parserOptions: { + project: path.resolve(projectRoot, 'tsconfig.worker.json'), + projectService: false, + }, + globals: globals.serviceworker, + }, + }, + { files: ['tests/tooling/**/*.ts'], rules: { diff --git a/config/rsbuild/rsbuild.common.ts b/config/rsbuild/rsbuild.common.ts index caf4082088e8..ee9b0e6ad5f2 100644 --- a/config/rsbuild/rsbuild.common.ts +++ b/config/rsbuild/rsbuild.common.ts @@ -1,7 +1,7 @@ import type {RsbuildConfig} from '@rsbuild/core'; import type {DefinePluginOptions, RspackPluginInstance, SwcJsMinimizerRspackPluginOptions} from '@rspack/core'; -import {GenerateSW} from '@aaroon/workbox-rspack-plugin'; +import {InjectManifest} from '@aaroon/workbox-rspack-plugin'; import {pluginSvgr} from '@rsbuild/plugin-svgr'; import {RsdoctorRspackPlugin} from '@rsdoctor/rspack-plugin'; import {rspack} from '@rspack/core'; @@ -511,9 +511,9 @@ const getCommonConfiguration = async ({file = '.env', platform = 'web', isDevSer ...(isDevelopment ? [] : [ - new GenerateSW({ - clientsClaim: true, - skipWaiting: true, + new InjectManifest({ + swSrc: path.resolve(dirname, '../../web/service-worker.ts'), + swDest: 'service-worker.js', // Cap is generous on purpose: the vendor (~6.5 MiB), main (~5.5 MiB), // authScreens.prefetch (~10.1 MiB) chunks and the canvaskit wasm (~7.7 MiB) are // all critical for offline boot, so we precache the lot. JS chunks are @@ -525,24 +525,6 @@ const getCommonConfiguration = async ({file = '.env', platform = 'web', isDevSer // worker requests the original URLs and the CDN transparently serves the Brotli copy, // so adding the twins to the precache as well would download every chunk twice. exclude: [/\.map$/, /^manifest.*\.js$/, /\.br$/], - // Single-page app: any unmatched navigation should serve the cached app shell. - navigateFallback: '/index.html', - // Don't fall back for asset-like or .well-known requests. - navigateFallbackDenylist: [/^\/_/, /^\/\.well-known/, /\/[^/?]+\.[^/]+$/], - runtimeCaching: [ - { - // Same-origin user media (receipts, chat attachments) — cache opportunistically - // so they're viewable on offline refresh. The function below is serialized into - // the generated service worker, so it executes in the SW context where - // `sameOrigin` is the appropriate Workbox match (no need to read `self.location`). - urlPattern: ({sameOrigin, url, request}) => sameOrigin && (request.destination === 'image' || /\/(receipts|chat-attachments)\//.test(url.pathname)), - handler: 'StaleWhileRevalidate', - options: { - cacheName: 'user-media', - expiration: {maxEntries: 200, maxAgeSeconds: 7 * 24 * 60 * 60}, - }, - }, - ], }), ]), // Inject into HTML. diff --git a/cspell.json b/cspell.json index 2b91aeb1b9e5..edcedf988eb0 100644 --- a/cspell.json +++ b/cspell.json @@ -939,6 +939,8 @@ "prebuild", "prebuilds", "precheck", + "precached", + "precaching", "prescribers", "presentationml", "prestep", @@ -1028,6 +1030,7 @@ "sideloaded", "sideproject", "simctl", + "serviceworker", "showcerts", "simployer", "skia", diff --git a/package-lock.json b/package-lock.json index e4817fd261c9..2af1601b76c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -291,7 +291,12 @@ "type-fest": "4.35.0", "typescript": "^7.0.2", "typescript-eslint": "8.58.1", - "wait-port": "^0.2.9" + "wait-port": "^0.2.9", + "workbox-core": "7.4.0", + "workbox-expiration": "7.4.0", + "workbox-precaching": "7.4.0", + "workbox-routing": "7.4.0", + "workbox-strategies": "7.4.0" }, "engines": { "bun": "1.3.14", diff --git a/package.json b/package.json index 8e35b35275e1..946a969fecc0 100644 --- a/package.json +++ b/package.json @@ -367,7 +367,12 @@ "type-fest": "4.35.0", "typescript": "^7.0.2", "typescript-eslint": "8.58.1", - "wait-port": "^0.2.9" + "wait-port": "^0.2.9", + "workbox-core": "7.4.0", + "workbox-expiration": "7.4.0", + "workbox-precaching": "7.4.0", + "workbox-routing": "7.4.0", + "workbox-strategies": "7.4.0" }, "overrides": { "typescript": "$typescript", diff --git a/src/libs/actions/AppUpdate/updateApp/index.ts b/src/libs/actions/AppUpdate/updateApp/index.ts index 3d0a08e4a865..fb41d2364c1a 100644 --- a/src/libs/actions/AppUpdate/updateApp/index.ts +++ b/src/libs/actions/AppUpdate/updateApp/index.ts @@ -1,10 +1,7 @@ -import clearWorkboxRecoveryCaches from '@libs/clearWorkboxRecoveryCaches'; - /** - * On web or mWeb, clear stale service worker and Cache Storage entries before reloading - * so the user gets the latest bundle instead of a cached version that triggers Update Required. + * Navigation checks the network for fresh HTML while the service worker keeps its offline assets. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars export default function updateApp(isProduction: boolean) { - clearWorkboxRecoveryCaches().then(() => window.location.reload()); + window.location.reload(); } diff --git a/src/libs/actions/Session/clearCache/index.ts b/src/libs/actions/Session/clearCache/index.ts index dcdfdb269d6d..213c935ad8dd 100644 --- a/src/libs/actions/Session/clearCache/index.ts +++ b/src/libs/actions/Session/clearCache/index.ts @@ -8,7 +8,7 @@ const clearStorage: ClearCache = async () => { // Clear the service worker's user-media cache so receipts and attachments // from one user's session cannot be served to a subsequent user on the // same browser (shared device / account switching). Must match the - // `cacheName` string in `config/rsbuild/rsbuild.common.ts` (GenerateSW). + // `cacheName` string in `web/service-worker.ts`. if (typeof caches !== 'undefined') { await caches.delete('user-media'); } diff --git a/src/libs/actions/UpdateRequired/index.ts b/src/libs/actions/UpdateRequired/index.ts index b65a3f37fe41..1934d856a837 100644 --- a/src/libs/actions/UpdateRequired/index.ts +++ b/src/libs/actions/UpdateRequired/index.ts @@ -1,4 +1,3 @@ -import clearWorkboxRecoveryCaches from '@libs/clearWorkboxRecoveryCaches'; import Log from '@libs/Log'; import CONST from '@src/CONST'; @@ -29,7 +28,7 @@ function markReloadAttempted(): boolean { /** * Web should always serve the latest version, so a 426 usually means the browser loaded an old bundle from its cache. - * The app version is baked into the bundle, so the request can only be retried with a new bundle: clear the caches and reload, + * The app version is baked into the bundle, so the request can only be retried with a new bundle: reload from the network, * and show the Update Required screen if the same bundle version still gets a 426 afterwards. */ function alertUser() { @@ -40,11 +39,11 @@ function alertUser() { hasHandledUpdateRequired = true; if (markReloadAttempted()) { - clearWorkboxRecoveryCaches().then(() => window.location.reload()); + window.location.reload(); return; } - Log.alert('[UpdateRequired] Got a 426 on web after clearing the cache and reloading', {appVersion: pkg.version}); + Log.alert('[UpdateRequired] Got a 426 on web after reloading', {appVersion: pkg.version}); Onyx.set(ONYXKEYS.RAM_ONLY_UPDATE_REQUIRED, true); } diff --git a/src/pages/ErrorPage/UpdateRequiredView/index.tsx b/src/pages/ErrorPage/UpdateRequiredView/index.tsx index e28d47e9204b..d45cc13d7641 100644 --- a/src/pages/ErrorPage/UpdateRequiredView/index.tsx +++ b/src/pages/ErrorPage/UpdateRequiredView/index.tsx @@ -21,7 +21,7 @@ import updateApp from '@userActions/AppUpdate/updateApp'; import CONST from '@src/CONST'; -import React from 'react'; +import React, {useState} from 'react'; import {View} from 'react-native'; function UpdateRequiredView() { @@ -34,6 +34,15 @@ function UpdateRequiredView() { const isReduceMotionEnabled = Accessibility.useReducedMotion(); const illustrations = useMemoizedLazyIllustrations(['UpgradeRocket']); const {isProduction} = useEnvironment(); + const [isRefreshing, setIsRefreshing] = useState(false); + + const refreshPage = () => { + if (isRefreshing) { + return; + } + setIsRefreshing(true); + updateApp(isProduction); + }; return ( @@ -69,7 +78,8 @@ function UpdateRequiredView() {