Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!isValidResponse) {
throw new WorkboxError('bad-precaching-response', {
url,
status: response.status,
});
}
// Redirected responses cannot be used to satisfy a navigation request, so
// any redirected response must be "copied" rather than cloned, so the new
// response doesn't contain the `redirected` flag. See:
// https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
if (response.redirected) {
response = await copyResponse(response);
}
await cacheWrapper.put({
event,
plugins,
response,
// `request` already uses `url`. We may be able to reuse it.
request: cacheKey === url ? request : new Request(cacheKey),
cacheName: this._cacheName,
matchOptions: {
ignoreSearch: true,
},
});
}
}
if (error || !response) {
response = await this._respondFromCache({request, event});
if (process.env.NODE_ENV !== 'production') {
if (response) {
logs.push(`Found a cached response in the '${this._cacheName}'` +
` cache.`);
} else {
logs.push(`No response found in the '${this._cacheName}' cache.`);
}
}
} else {
// Keep the service worker alive while we put the request in the cache
const responseClone = response.clone();
const cachePut = cacheWrapper.put({
cacheName: this._cacheName,
request,
response: responseClone,
event,
plugins: this._plugins,
});
if (event) {
try {
// The event has been responded to so we can keep the SW alive to
// respond to the request
event.waitUntil(cachePut);
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
async _getFromNetwork(request: Request, event?: ExtendableEvent) {
const response = await fetchWrapper.fetch({
request,
event,
fetchOptions: this._fetchOptions,
plugins: this._plugins,
});
// Keep the service worker while we put the request to the cache
const responseClone = response.clone();
const cachePutPromise = cacheWrapper.put({
cacheName: this._cacheName,
request,
response: responseClone,
event,
plugins: this._plugins,
});
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
}
}
async _getFromNetwork({request, event}: {
request: Request,
event?: ExtendableEvent,
}): Promise {
const response = await fetchWrapper.fetch({
request,
event,
fetchOptions: this._fetchOptions,
plugins: this._plugins,
});
const cachePutPromise = cacheWrapper.put({
cacheName: this._cacheName,
request,
response: response.clone(),
event,
plugins: this._plugins,
});
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
}
}