feat(pwa): add new network only strategy

This commit is contained in:
Julien Oculi 2024-07-18 15:39:54 +02:00
parent 838bdec6ea
commit 9bf6d9596c

View file

@ -54,6 +54,32 @@ export class Strategy {
} }
} }
} }
networkOnly(
{ request, preloadResponse }: Pick<FetchEvent, 'request' | 'preloadResponse'>,
ac: AbortController = new AbortController()
): Promise<Response> {
// Browser preload
const preload = getPreload(preloadResponse, ac)
.then(response => {
if (response === undefined) {
throw new Error(`no preload response for ${request.url}`)
}
// Cancel fetch event
ac.abort()
return response
})
// Client fetch
const fetched = fetch(request, { signal: ac.signal })
.then(response => {
// Cancel preload event
ac.abort()
return response
})
return Promise.race([preload, fetched])
}
} }
function getPreload( function getPreload(