This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/sw.js.map

1 line
19 KiB
Text
Raw Normal View History

2022-08-16 17:11:00 +05:30
{"version":3,"file":"sw.js","sources":["../src/platform/web/assets/icon.png?url","../src/platform/web/sw.js"],"sourcesContent":["export default \"__VITE_ASSET__2a39c64c__\"","/*\nCopyright 2020 Bruno Windels <bruno@windels.cloud>\nCopyright 2020 The Matrix.org Foundation C.I.C.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport NOTIFICATION_BADGE_ICON from \"./assets/icon.png?url\";\n// replaced by the service worker build plugin\nconst UNHASHED_PRECACHED_ASSETS = DEFINE_UNHASHED_PRECACHED_ASSETS;\nconst HASHED_PRECACHED_ASSETS = DEFINE_HASHED_PRECACHED_ASSETS;\nconst HASHED_CACHED_ON_REQUEST_ASSETS = DEFINE_HASHED_CACHED_ON_REQUEST_ASSETS;\n\nconst unhashedCacheName = `hydrogen-assets-${DEFINE_GLOBAL_HASH}`;\nconst hashedCacheName = `hydrogen-assets`;\nconst mediaThumbnailCacheName = `hydrogen-media-thumbnails-v2`;\n\nself.addEventListener('install', function(e) {\n e.waitUntil((async () => {\n const unhashedCache = await caches.open(unhashedCacheName);\n await unhashedCache.addAll(UNHASHED_PRECACHED_ASSETS);\n const hashedCache = await caches.open(hashedCacheName);\n await Promise.all(HASHED_PRECACHED_ASSETS.map(async asset => {\n if (!await hashedCache.match(asset)) {\n await hashedCache.add(asset);\n }\n }));\n })());\n});\n\nself.addEventListener('activate', (event) => {\n // on a first page load/sw install,\n // start using the service worker on all pages straight away\n self.clients.claim();\n event.waitUntil(purgeOldCaches());\n});\n\nasync function purgeOldCaches() {\n // remove any caches we don't know about\n const keyList = await caches.keys();\n for (const key of keyList) {\n if (key !== unhashedCacheName && key !== hashedCacheName && key !== mediaThumbnailCacheName) {\n await caches.delete(key);\n }\n }\n // remove the cache for any old hashed resource\n const hashedCache = await caches.open(hashedCacheName);\n const keys = await hashedCache.keys();\n const hashedAssetURLs =\n HASHED_PRECACHED_ASSETS\n .concat(HASHED_CACHED_ON_REQUEST_ASSETS)\n .map(a => new URL(a, self.registration.scope).href);\n\n for (const request of keys) {\n if (!hashedAssetURLs.some(url => url === request.url)) {\n hashedCache.delete(request);\n }\n }\n}\n\nself.addEventListener('fetch', (event) => {\n /*\n service worker shouldn't handle xhr uploads because otherwise\n the progress events won't fire.\n This has to do with xhr not being supported in service workers.\n */\n if (event.request.method === \"GET\") {\n event.respondWith(handleRequest(event.request));\n }\n});\n\nfunction isCacheableThumbnail(url) {\n if (url.pathname.startsWith(\"/_matrix/media/r0/thumbnail/\")) {\n const width = parseInt(url.searchParams.get(\"width\"), 10);\n const height = parseInt(url.searchParams.get(\"height\"), 10);\n if (width <= 50 && height <= 50) {\n return true;\n }\n }\n return false;\n}\n\nconst baseURL = new URL(self.registration.scope);\nlet pendingFetchAbortController = new AbortController();\n\nasync function handleRequest(request) {\n try {\n if (request.url.includes(\"config.json\") || /theme-.+\\.json/.test(request.url)) {\n return handleStaleWhileRevalidateRequest(request);\n }\n const url = new URL(request.url);\n // rewrite / to /index.html so it hits the cache\n if (url.origin === baseURL.origin && url.pathname === baseURL.p