From f0a470398d8278fb0c6aa413520a105ca7c7735d Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Thu, 11 Dec 2025 12:24:45 +0800 Subject: [PATCH] chore(pwa): more aggressive offline cache for the web version (#2686) --- apps/readest-app/next.config.mjs | 86 ++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/apps/readest-app/next.config.mjs b/apps/readest-app/next.config.mjs index 8df2baa5..bb2ac102 100644 --- a/apps/readest-app/next.config.mjs +++ b/apps/readest-app/next.config.mjs @@ -64,39 +64,61 @@ const nextConfig = { }, }; -const withPWA = withPWAInit({ - dest: 'public', - disable: isDev || appPlatform !== 'web', - cacheStartUrl: false, - dynamicStartUrl: false, - cacheOnFrontEndNav: true, - aggressiveFrontEndNavCaching: true, - reloadOnOnline: true, - swcMinify: true, - fallbacks: { - document: '/offline', - }, - workboxOptions: { - disableDevLogs: true, - manifestTransforms: [ - (manifestEntries) => { - const manifest = manifestEntries.filter((entry) => { - const url = entry.url; - return ( - !url.includes('dynamic-css-manifest.json') && - !url.includes('middleware-manifest.json') && - !url.includes('react-loadable-manifest.json') && - !url.includes('build-manifest.json') && - !url.includes('_buildManifest.js') && - !url.includes('_ssgManifest.js') && - !url.includes('_headers') - ); - }); - return { manifest }; +const pwaDisabled = isDev || appPlatform !== 'web'; + +const withPWA = pwaDisabled + ? (config) => config + : withPWAInit({ + dest: 'public', + cacheStartUrl: false, + dynamicStartUrl: false, + cacheOnFrontEndNav: true, + aggressiveFrontEndNavCaching: true, + reloadOnOnline: true, + swcMinify: true, + fallbacks: { + document: '/offline', }, - ], - }, -}); + workboxOptions: { + disableDevLogs: true, + runtimeCaching: [ + { + urlPattern: /^https?.*/, + handler: 'StaleWhileRevalidate', + options: { + cacheName: 'offlineCache', + expiration: { + maxEntries: 512, + maxAgeSeconds: 365 * 24 * 60 * 60, + }, + cacheableResponse: { + statuses: [0, 200], + }, + }, + }, + ], + cleanupOutdatedCaches: true, + clientsClaim: true, + skipWaiting: true, + manifestTransforms: [ + (manifestEntries) => { + const manifest = manifestEntries.filter((entry) => { + const url = entry.url; + return ( + !url.includes('dynamic-css-manifest.json') && + !url.includes('middleware-manifest.json') && + !url.includes('react-loadable-manifest.json') && + !url.includes('build-manifest.json') && + !url.includes('_buildManifest.js') && + !url.includes('_ssgManifest.js') && + !url.includes('_headers') + ); + }); + return { manifest }; + }, + ], + }, + }); const withAnalyzer = withBundleAnalyzer({ enabled: process.env.ANALYZE === 'true',