Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const addFetchListener = ({
ignoreURLParametersMatching = [/^utm_/],
directoryIndex = 'index.html',
cleanURLs = true,
urlManipulation,
}: FetchListenerOptions = {}) => {
const cacheName = cacheNames.getPrecacheName();
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
self.addEventListener('fetch', ((event: FetchEvent) => {
const precachedURL = getCacheKeyForURL(event.request.url, {
cleanURLs,
directoryIndex,
ignoreURLParametersMatching,
urlManipulation,
});
if (!precachedURL) {
if (process.env.NODE_ENV !== 'production') {
logger.debug(`Precaching did not find a match for ` +
getFriendlyURL(event.request.url));
}
return;
}
export const registerNavigationRoute = (cachedAssetUrl: string,
options: NavigationRouteOptions = {}): NavigationRoute => {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(cachedAssetUrl, 'string', {
moduleName: 'workbox-routing',
funcName: 'registerNavigationRoute',
paramName: 'cachedAssetUrl',
});
}
const cacheName = cacheNames.getPrecacheName(options.cacheName);
const handler = async () => {
try {
const response = await caches.match(cachedAssetUrl, {cacheName});
if (response) {
return response;
}
// This shouldn't normally happen, but there are edge cases:
// https://github.com/GoogleChrome/workbox/issues/1441
throw new Error(`The cache ${cacheName} did not have an entry for ` +
`${cachedAssetUrl}.`);
} catch (error) {
// If there's either a cache miss, or the caches.match() call threw
// an exception, then attempt to fulfill the navigation request with
// a response from the network rather than leaving the user with a
self.addEventListener('activate', ((event: ExtendableEvent) => {
const cacheName = cacheNames.getPrecacheName();
event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
if (process.env.NODE_ENV !== 'production') {
if (cachesDeleted.length > 0) {
logger.log(`The following out-of-date precaches were cleaned up ` +
`automatically:`, cachesDeleted);
}
}
}));
}) as EventListener);
};
constructor(cacheName?: string) {
this._cacheName = cacheNames.getPrecacheName(cacheName);
this._urlsToCacheKeys = new Map();
this._urlsToCacheModes = new Map();
this._cacheKeysToIntegrities = new Map();
}