From 0b5693ae2771f85b85b0d241ea11e0914b7bdcf8 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 14 Sep 2023 23:21:43 +0800 Subject: [PATCH] First step in caching assets --- public/sw.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/public/sw.js b/public/sw.js index 31ba4e73..8d761025 100644 --- a/public/sw.js +++ b/public/sw.js @@ -9,6 +9,25 @@ import { self.__WB_DISABLE_DEV_LOGS = true; +const assetsRoute = new Route( + ({ request, sameOrigin }) => { + const isAsset = + request.destination === 'style' || request.destination === 'script'; + const hasHash = /-[0-9a-f]{4,}\./i.test(request.url); + return sameOrigin && isAsset && hasHash; + }, + new NetworkFirst({ + cacheName: 'assets', + networkTimeoutSeconds: 5, + plugins: [ + new CacheableResponsePlugin({ + statuses: [0, 200], + }), + ], + }), +); +registerRoute(assetsRoute); + const imageRoute = new Route( ({ request, sameOrigin }) => { const isRemote = !sameOrigin;