Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ampAssetsCaching() {
// Versioned Assets
router.registerRoute(
VERSIONED_ASSETS_RE,
new CacheFirst({
cacheName: VERSIONED_CACHE_NAME,
plugins: [
new Plugin({
maxAgeSeconds: 14 * 24 * 60 * 60, // 14 days
}),
],
}),
);
// Unversioned runtimes
router.registerRoute(
UNVERSIONED_RUNTIME_RE,
new StaleWhileRevalidate({
cacheName: UNVERSIONED_CACHE_NAME,
// Unversioned runtimes
router.registerRoute(
UNVERSIONED_RUNTIME_RE,
new StaleWhileRevalidate({
cacheName: UNVERSIONED_CACHE_NAME,
plugins: [
new Plugin({
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
}),
],
}),
);
// Unversioned Extensions
router.registerRoute(
UNVERSIONED_EXTENSIONS_RE,
new StaleWhileRevalidate({
cacheName: UNVERSIONED_CACHE_NAME,
plugins: [
new Plugin({
maxAgeSeconds: 24 * 60 * 60, // 1 day
}),
],
}),
);
}
ampAssetsCaching() {
// Versioned Assets
router.registerRoute(
VERSIONED_ASSETS_RE,
new CacheFirst({
cacheName: VERSIONED_CACHE_NAME,
plugins: [
new Plugin({
maxAgeSeconds: 14 * 24 * 60 * 60, // 14 days
}),
],
}),
);
// Unversioned runtimes
router.registerRoute(
UNVERSIONED_RUNTIME_RE,
new StaleWhileRevalidate({
cacheName: UNVERSIONED_CACHE_NAME,
plugins: [
new Plugin({
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
}),
],
}),
);
// Unversioned Extensions
router.registerRoute(
UNVERSIONED_EXTENSIONS_RE,
new StaleWhileRevalidate({
cacheName: UNVERSIONED_CACHE_NAME,
/.*cdn\.jsdelivr\.net/,
new CacheFirst({
cacheName: 'jsdelivr-v1',
fetchOptions: {
credentials: 'omit',
mode: 'cors',
},
plugins: [
new ExpirationPlugin({ maxAgeSeconds: oneWeek, purgeOnQuotaError: true }),
],
}),
)
//#endregion
//#region JavaScript files
registerRoute(
/.+\/app\/\w{2,3}\.\w{7}\.js$/,
new CacheFirst({
cacheName: 'javascript-v1',
fetchOptions: {
credentials: 'omit',
mode: 'cors',
},
plugins: [
new ExpirationPlugin({ maxAgeSeconds: oneWeek, purgeOnQuotaError: true }),
],
}),
)
registerRoute(
/.+\/plugins\/.+\.js$/,
new StaleWhileRevalidate({
cacheName: 'javascript-v1',
new AmpDocumentCachablePlugin({
maxEntries: documentCachingOptions.maxDocumentsInCache || 10,
maxAgeSeconds:
documentCachingOptions.maxAgeSecondsforDocumentsInCache ||
5 * 24 * 60 * 60,
allowedNonAMPPages: documentCachingOptions.allowedNonAMPPages,
}),
],
networkTimeoutSeconds: documentCachingOptions.timeoutSeconds,
},
fallbackOfflinePageUrl,
),
navigationPreloadOptions,
);
router.registerRoute(navRoute);
return navRoute;
}
/.+\/plugins\/.+\.css$/,
new StaleWhileRevalidate({
cacheName: 'stylesheet-v1',
fetchOptions: {
credentials: 'omit',
mode: 'cors',
},
plugins: [
new ExpirationPlugin({ maxAgeSeconds: oneWeek, purgeOnQuotaError: true }),
],
}),
)
//#endregion
//#region Fonts
registerRoute(
({ request }) => request.destination === 'font',
new StaleWhileRevalidate({
cacheName: 'font-v1',
fetchOptions: {
credentials: 'omit',
mode: 'cors',
},
plugins: [new ExpirationPlugin({ maxEntries: 12 })],
}),
)
//#endregion
],
};
switch (assetCachingOption.cachingStrategy) {
case 'NETWORK_FIRST':
cachingStrategy = new NetworkFirst(cachingConfig);
break;
case 'STALE_WHILE_REVALIDATE':
cachingStrategy = new StaleWhileRevalidate(cachingConfig);
break;
default:
cachingStrategy = new CacheFirst(cachingConfig);
break;
}
router.registerRoute(assetCachingOption.regexp, cachingStrategy);
});
}
linksRegExps.forEach(link => {
navigationRoute_.addDeniedUrls(link);
router.registerRoute(
link,
new CacheFirst({
cacheName: AMP_PREFETCHED_LINKS,
plugins: [
new AmpPrefetchPlugin({
maxEntries: 10,
maxAgeSeconds: maxAgeSecondsInCache,
postDelete: (url: string) => {
const linkRE = convertUrlToRegExp(cleanHostInfoFromUrl(url));
navigationRoute_.removeDeniedUrls(linkRE);
},
}),
],
networkTimeoutSeconds: 0.5,
}),
);
import { registerRoute } from 'workbox-routing'
import {
CacheFirst,
StaleWhileRevalidate,
NetworkOnly,
} from 'workbox-strategies'
import { ExpirationPlugin } from 'workbox-expiration'
const oneWeek = 7 * 24 * 3600
if (process.env.NODE_ENV === 'development') {
registerRoute(/\.js/, new NetworkOnly())
registerRoute(/\.css/, new NetworkOnly())
}
//#region Pictures
registerRoute(
/\/preview\/\d+/,
new CacheFirst({
cacheName: 'texture-preview-v2',
fetchOptions: {
credentials: 'omit',
},
plugins: [
new ExpirationPlugin({ maxAgeSeconds: oneWeek, purgeOnQuotaError: true }),
],
}),
)