Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
config.hitFilter.call(null, params);
}
// Retry the fetch. Ignore URL search params from the URL as they're
// now in the post body.
await fetch(new Request(url.origin + url.pathname, {
body: params.toString(),
method: 'POST',
mode: 'cors',
credentials: 'omit',
headers: {'Content-Type': 'text/plain'},
}));
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(url.href)}'` +
`has been replayed`);
}
} catch (err) {
await queue.unshiftRequest(entry);
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(url.href)}'` +
`failed to replay, putting it back in the queue.`);
}
throw err;
}
}
if (process.env.NODE_ENV !== 'production') {
logger.log(`All Google Analytics request successfully replayed; ` +
`the queue is now empty!`);
}
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)}'.`);
}
}
}
}
return response;
}
const storableRequest = await StorableRequest.fromRequest(request.clone());
const entry: UnidentifiedQueueStoreEntry = {
requestData: storableRequest.toObject(),
timestamp,
};
// Only include metadata if it's present.
if (metadata) {
entry.metadata = metadata;
}
await this._queueStore[
`${operation}Entry` as 'pushEntry' | 'unshiftEntry'](entry);
if (process.env.NODE_ENV !== 'production') {
logger.log(`Request for '${getFriendlyURL(request.url)}' has ` +
`been added to background sync queue '${this._name}'.`);
}
// Don't register for a sync if we're in the middle of a sync. Instead,
// we wait until the sync is complete and call register if
// `this._requestsAddedDuringSync` is true.
if (this._syncInProgress) {
this._requestsAddedDuringSync = true;
} else {
await this.registerSync();
}
}
}).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
// Fall back to the network if we don't have a cached response
// (perhaps due to manual cache cleanup).
if (process.env.NODE_ENV !== 'production') {
logger.warn(`The precached response for ` +
`${getFriendlyURL(precachedURL)} in ${cacheName} was not found. ` +
`Falling back to the network instead.`);
}
return fetch(precachedURL);
});
plugins: this._plugins,
});
let error;
if (response) {
if (process.env.NODE_ENV !== 'production') {
logs.push(`Found a cached response in the '${this._cacheName}'` +
` cache. Will update with the network response in the background.`);
}
if (event) {
try {
event.waitUntil(fetchAndCachePromise);
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
}
}
}
} else {
if (process.env.NODE_ENV !== 'production') {
logs.push(`No response found in the '${this._cacheName}' cache. ` +
`Will wait for the network response.`);
}
try {
response = await fetchAndCachePromise;
} catch (err) {
error = err;
}
}
if (process.env.NODE_ENV !== 'production') {
let cacheable = true;
if (this._statuses) {
cacheable = this._statuses.includes(response.status);
}
if (this._headers && cacheable) {
cacheable = Object.keys(this._headers).some((headerName) => {
return response.headers.get(headerName) === this._headers![headerName];
});
}
if (process.env.NODE_ENV !== 'production') {
if (!cacheable) {
logger.groupCollapsed(`The request for ` +
`'${getFriendlyURL(response.url)}' returned a response that does ` +
`not meet the criteria for being cached.`);
logger.groupCollapsed(`View cacheability criteria here.`);
logger.log(`Cacheable statuses: ` +
JSON.stringify(this._statuses));
logger.log(`Cacheable headers: ` +
JSON.stringify(this._headers, null, 2));
logger.groupEnd();
const logFriendlyHeaders: {[key: string]: string} = {};
response.headers.forEach((value, key) => {
logFriendlyHeaders[key] = value;
});
logger.groupCollapsed(`View response status and headers here.`);
logger.log(`Response status: ` + response.status);
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)}'.`);
}
}
}
return response;
}
}
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)}'.`);
}
}
}
return response;
}
}