diff --git a/config/rsbuild/rsbuild.common.ts b/config/rsbuild/rsbuild.common.ts index a8262a5abc25..3d545f381378 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'; @@ -521,9 +521,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 @@ -535,24 +535,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 648053fa2182..9c759d4d18ba 100644 --- a/cspell.json +++ b/cspell.json @@ -951,6 +951,8 @@ "prebuild", "prebuilds", "precheck", + "precached", + "precaching", "prescribers", "presentationml", "prestep", @@ -1045,6 +1047,7 @@ "sideloaded", "sideproject", "simctl", + "serviceworker", "showcerts", "simployer", "skia", diff --git a/knip.json b/knip.json index 27382f1dbe49..d20f4ba0d241 100644 --- a/knip.json +++ b/knip.json @@ -12,6 +12,7 @@ "src/HybridAppHandler.tsx", "scripts/**/*.{js,mjs,ts}", "web/proxy.ts", + "web/service-worker.ts", "config/rsbuild/**/*.{js,mjs,cjs,ts}", "config/repack/**/*.{js,mjs,ts}", ".github/scripts/**/*.ts", diff --git a/package-lock.json b/package-lock.json index d0d03e3de140..3f09a44bd93e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -292,7 +292,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.4.4", diff --git a/package.json b/package.json index 2e2f28a94085..d3d7bc9fc37f 100644 --- a/package.json +++ b/package.json @@ -368,7 +368,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/AppUpdateURL.ts b/src/libs/AppUpdateURL.ts new file mode 100644 index 000000000000..2f51f820e511 --- /dev/null +++ b/src/libs/AppUpdateURL.ts @@ -0,0 +1,10 @@ +/** Shared URL marker for upgrade navigations that must wait for fresh HTML. */ +const APP_UPDATE_QUERY_PARAM = 'appUpdateRequired'; + +function getAppUpdateURL(href: string): string { + const url = new URL(href); + url.searchParams.set(APP_UPDATE_QUERY_PARAM, 'true'); + return url.href; +} + +export {APP_UPDATE_QUERY_PARAM, getAppUpdateURL}; diff --git a/src/libs/actions/AppUpdate/updateApp/index.ts b/src/libs/actions/AppUpdate/updateApp/index.ts index 3d0a08e4a865..fee8f04c4f7d 100644 --- a/src/libs/actions/AppUpdate/updateApp/index.ts +++ b/src/libs/actions/AppUpdate/updateApp/index.ts @@ -1,10 +1,9 @@ -import clearWorkboxRecoveryCaches from '@libs/clearWorkboxRecoveryCaches'; +import {getAppUpdateURL} from '@libs/AppUpdateURL'; /** - * 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. + * The upgrade marker prevents a slow network from sending the user back to an unsupported cached shell. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars export default function updateApp(isProduction: boolean) { - clearWorkboxRecoveryCaches().then(() => window.location.reload()); + window.location.replace(getAppUpdateURL(window.location.href)); } 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..22fa7d2ffcb9 100644 --- a/src/libs/actions/UpdateRequired/index.ts +++ b/src/libs/actions/UpdateRequired/index.ts @@ -1,4 +1,4 @@ -import clearWorkboxRecoveryCaches from '@libs/clearWorkboxRecoveryCaches'; +import {getAppUpdateURL} from '@libs/AppUpdateURL'; import Log from '@libs/Log'; import CONST from '@src/CONST'; @@ -29,7 +29,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 +40,11 @@ function alertUser() { hasHandledUpdateRequired = true; if (markReloadAttempted()) { - clearWorkboxRecoveryCaches().then(() => window.location.reload()); + window.location.replace(getAppUpdateURL(window.location.href)); 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() {