Does a service worker allow one to simply use long expiration headers on static assets?

run_the_race

Say I have a service worker that populates the cache with the following working code when its activated:

async function install() {
    console.debug("SW: Installing ...");
    const cache = await caches.open(CACHE_VERSION);
    await cache.addAll(CACHE_ASSETS);
    console.log("SW: Installed");
}
async function handleInstall(event) {
    event.waitUntil(install());
}
self.addEventListener("install", handleInstall);

When performs cache.addAll(), will the browser use its own internal cache, or will it always download the content from the site? This is important, because it one creates a new service worker release, and there are new static assets, the old version maybe be cached by the service worker.

If not then I guess one still has to do named hashed/versioned static assets. Something I was hoping service workers would make none applicable.

Jeff Posnick

cache.addAll()'s behavior is described in the service worker specification, but here's a more concise summary:

  1. For each item in the parameter array, if it's a string and not a Request, construct a new Request using that string as input.
  2. Perform fetch() on each request and get a response.
  3. As long as the response has an ok status, call cache.put() to add the response to the cache, using the request as the key.

To answer your question, the most relevant step is 1., as that determines what kind of Request is passed to fetch(). If you just pass in a string, then there are a lot of defaults that will be used when implicitly constructing the Request. If you want more control over what's fetch()ed, then you should explicitly create a Request yourself and pass that to cache.addAll() instead of passing in strings.

For instance, this is how you'd explicitly set the cache mode on all the requests to 'reload', which always skip the browser's normal HTTP cache and go against the network for a response:

// Define your list of URLs somewhere...
const URLS = ['/one.css', '/two.js', '/three.js', '...'];

// Later...
const requests = URLS.map((url) => new Request(url, {cache: 'reload'}));
await cache.addAll(requests);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cloudflare does not cache static assets with proper headers

Cashing all the static assets at once in a service worker in PWA

Service Worker Response Cache Headers

Service-worker force update of new assets

Which headers indicate a request for static assets

Netlify Headers Cache Control For Static Assets

Reading request headers inside a service worker

is it possible to modify service worker cache response headers?

Adding headers to angular service worker responses

How does one use the VSCode debugger to debug a Gunicorn worker process?

Cache non static url with service worker

Can Angular 5 Service Worker fetch assets in background

Can a service worker fetch and cache cross-origin assets?

How can we use FCM Service Worker with a existing Service Worker?

How to use different directory to serve static assets?

Why doesn't this friend funtion allow brace-init-list but the static one does?

Is it safe for a service worker to return cached CSP headers containing nonces?

Angular 5 Multiple Service Worker in one domain

Handle fetch in service worker but allow client to see redirect

Will Service Worker Query Cache Algorithm Allow Expression Matching URL paths?

Does it make sense saving a service worker in cache?

Terraform does not create docker service on worker node

.NET Core Worker service does not have CRON

Angular caching - should we use service worker?

Use ESM in a service worker file (import/export)

How to use service worker to respond to navigation requests?

How to use a Service Worker to cache a virtual file?

Long-running process inside a Service Worker (or something similar)

Suggest a top level service worker or multiple service workers in one domain?