From 7e58a4e14f4a3efe3699381a447919ef4c31c362 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Fri, 14 Apr 2023 17:23:41 +0800 Subject: [PATCH] Use different build path for icons, cache them --- public/sw.js | 21 +++++++++++++++++++++ vite.config.js | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/public/sw.js b/public/sw.js index b44a94b0..95121e3c 100644 --- a/public/sw.js +++ b/public/sw.js @@ -33,6 +33,27 @@ const imageRoute = new Route( ); registerRoute(imageRoute); +const iconsRoute = new Route( + ({ request, sameOrigin }) => { + const isIcon = request.url.includes('/icons/'); + return sameOrigin && isIcon; + }, + new CacheFirst({ + cacheName: 'icons', + plugins: [ + new ExpirationPlugin({ + maxEntries: 50, + maxAgeSeconds: 3 * 24 * 60 * 60, // 3 days + purgeOnQuotaError: true, + }), + new CacheableResponsePlugin({ + statuses: [0, 200], + }), + ], + }), +); +registerRoute(iconsRoute); + // 1-day cache for // - /api/v1/instance // - /api/v1/custom_emojis diff --git a/vite.config.js b/vite.config.js index 7493c085..aecbde7f 100644 --- a/vite.config.js +++ b/vite.config.js @@ -94,6 +94,15 @@ export default defineConfig({ main: resolve(__dirname, 'index.html'), compose: resolve(__dirname, 'compose/index.html'), }, + output: { + chunkFileNames: (chunkInfo) => { + const { facadeModuleId } = chunkInfo; + if (facadeModuleId && facadeModuleId.includes('icon')) { + return 'assets/icons/[name]-[hash].js'; + } + return 'assets/[name]-[hash].js'; + }, + }, }, }, });