feat(pwa): ✨ add new network only strategy
This commit is contained in:
parent
838bdec6ea
commit
9bf6d9596c
|
|
@ -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(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue