mischnic / parcel-plugin-sw-cache

📦👷 Parcel plugin for caching using a service worker
https://npm.im/parcel-plugin-sw-cache
MIT License
47 stars 6 forks source link

How to setup expiration in handlers #23

Closed brielov closed 2 years ago

brielov commented 4 years ago

If I use cache-only, how can I make it expire after lets say 1 hour?

mischnic commented 4 years ago

A Google search turned up: https://developers.google.com/web/tools/workbox/modules/workbox-cache-expiration

workbox.routing.registerRoute(
  /\/images\//,
  new workbox.strategies.CacheOnly({
    cacheName: 'image-cache',
    plugins: [
      new workbox.expiration.Plugin({
        maxAgeSeconds: 24 * 60 * 60,
      }),
    ],
  })
);

or: https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config

runtimeCaching: [{
    // Match any same-origin request that contains 'api'.
    urlPattern: /api/,
    // Apply a network-first strategy.
    handler: 'NetworkFirst',
    options: {
      // Fall back to the cache after 10 seconds.
      networkTimeoutSeconds: 10,
      // Use a custom cache name for this route.
      cacheName: 'my-api-cache',
      // Configure custom cache expiration.
      expiration: {
          maxEntries: 5,
          maxAgeSeconds: 60,
      }
    }
}]