formatting and comments
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
/* __PWA_BUILD_ID__ is replaced by the Vite build before this file is emitted. */
|
||||
/**
|
||||
* Network policy for the `/control/` application shell.
|
||||
*
|
||||
* `__PWA_BUILD_ID__` is replaced by Vite before emission. Navigations use a
|
||||
* network-first strategy so fresh HTML can reference the newest hashed bundle;
|
||||
* known static assets use cache-first. The worker never handles API, WebSocket
|
||||
* or OBS routes because they are outside both this fetch policy and its scope.
|
||||
*/
|
||||
const buildId = '__PWA_BUILD_ID__'
|
||||
const cacheName = `lxc-control-v2-shell-${buildId}`
|
||||
const shellPath = '/control/'
|
||||
@@ -24,8 +31,9 @@ async function precacheShell() {
|
||||
|
||||
const html = await response.clone().text()
|
||||
await Promise.all(shellRoutes.map(route => cache.put(route, response.clone())))
|
||||
const generatedAssets = [...html.matchAll(/(?:src|href)="(\/assets\/[^"?#]+)"/g)]
|
||||
.map(match => match[1])
|
||||
const generatedAssets = [...html.matchAll(/(?:src|href)="(\/assets\/[^"?#]+)"/g)].map(
|
||||
match => match[1],
|
||||
)
|
||||
await cache.addAll([...new Set([...stableAssets, ...generatedAssets])])
|
||||
}
|
||||
|
||||
@@ -34,13 +42,17 @@ self.addEventListener('install', event => {
|
||||
})
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
event.waitUntil((async () => {
|
||||
const names = await caches.keys()
|
||||
await Promise.all(names
|
||||
.filter(name => name.startsWith('lxc-control-v2-shell-') && name !== cacheName)
|
||||
.map(name => caches.delete(name)))
|
||||
await self.clients.claim()
|
||||
})())
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
const names = await caches.keys()
|
||||
await Promise.all(
|
||||
names
|
||||
.filter(name => name.startsWith('lxc-control-v2-shell-') && name !== cacheName)
|
||||
.map(name => caches.delete(name)),
|
||||
)
|
||||
await self.clients.claim()
|
||||
})(),
|
||||
)
|
||||
})
|
||||
|
||||
self.addEventListener('message', event => {
|
||||
@@ -52,9 +64,11 @@ function isControlShell(pathname) {
|
||||
}
|
||||
|
||||
function isStaticAsset(pathname) {
|
||||
return pathname.startsWith('/assets/')
|
||||
|| pathname.startsWith('/pwa/')
|
||||
|| pathname === '/control/manifest.webmanifest'
|
||||
return (
|
||||
pathname.startsWith('/assets/') ||
|
||||
pathname.startsWith('/pwa/') ||
|
||||
pathname === '/control/manifest.webmanifest'
|
||||
)
|
||||
}
|
||||
|
||||
async function navigationResponse(request) {
|
||||
@@ -62,16 +76,20 @@ async function navigationResponse(request) {
|
||||
const pathname = new URL(request.url).pathname
|
||||
try {
|
||||
const response = await fetch(request)
|
||||
if (shellRoutes.includes(pathname)
|
||||
&& response.ok
|
||||
&& response.headers.get('content-type')?.includes('text/html')) {
|
||||
if (
|
||||
shellRoutes.includes(pathname) &&
|
||||
response.ok &&
|
||||
response.headers.get('content-type')?.includes('text/html')
|
||||
) {
|
||||
await cache.put(pathname, response.clone())
|
||||
}
|
||||
return response
|
||||
} catch {
|
||||
return (await cache.match(request, { ignoreSearch: true }))
|
||||
|| (await cache.match(shellPath))
|
||||
|| Response.error()
|
||||
return (
|
||||
(await cache.match(request, { ignoreSearch: true })) ||
|
||||
(await cache.match(shellPath)) ||
|
||||
Response.error()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user