fix(service-worker): cache opaque responses when requests exceeds timeout threshold (#30977)

PR Close #30977
This commit is contained in:
George Kalpakas
2019-06-24 15:04:15 +03:00
committed by Alex Rickabaugh
parent d7be38f84b
commit 93abc35213
2 changed files with 34 additions and 3 deletions

View File

@ -378,7 +378,7 @@ export class DataGroup {
// If the network fetch times out or errors, fall back on the cache.
if (res === undefined) {
ctx.waitUntil(this.safeCacheResponse(req, networkFetch));
ctx.waitUntil(this.safeCacheResponse(req, networkFetch, true));
// Ignore the age, the network response will be cached anyway due to the
// behavior of freshness.
@ -434,9 +434,10 @@ export class DataGroup {
}
}
private async safeCacheResponse(req: Request, res: Promise<Response>): Promise<void> {
private async safeCacheResponse(req: Request, res: Promise<Response>, okToCacheOpaque?: boolean):
Promise<void> {
try {
await this.cacheResponse(req, await res, await this.lru());
await this.cacheResponse(req, await res, await this.lru(), okToCacheOpaque);
} catch {
// TODO: handle this error somehow?
}