Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
service worker - Progressive caching for a progressive web app - How to load groups of files step by step

Here is a service worker that caches only the basic starting files of the app,

self.addEventListener("fetch", event => {
  event.respondWith( caches.match(event.request)
    .then( cachedResponse => {
      return cachedResponse || fetch(event.request);
    } )
  );
});

const cacheName0 = "bare-bones-of-the-app";
const mainResourcesToPrecache = [
"index.html",
"app.js",
"style.css"
];

self.addEventListener("install", event => {
  event.waitUntil(
    caches.open(cacheName0).then(cache => {
      return cache.addAll(mainResourcesToPrecache);
    })
  );
});

So far so good. Now let's say this PWA is a game and the user makes progress as he completes one level after another. And let's say we have,

const cacheName1 = "level-1";
const resourcesForLevel1 = [
"somepicture.jpg",
"someartwork.png",
"somesound.mp3"
];
const cacheName2 = "level-2";
const resourcesForLevel2 = [
"anotherpicture.jpg",
"anotherartwork.png",
"anothersound.mp3"
];
// etc

In this case how do we wake the service worker up with a function firing in the main thread and thus make the browser get the files in advance as the user reaches certain points in the app?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The nice thing about the Cache Storage API is that the same caches are exposed in both the ServiceWorkerGlobalScope and the Window context. So you can do what you're suggesting without having to send a message to the service worker asking it to cache things for you.

The important thing is to make sure that you're using the same cache names in both contexts.

async function cacheLevel(levelNumber) {
  // Make sure this matches the cache name you use in the SW.
  const cacheName = `level-${levelNumber};

  // Assume there's a levelNumber => URLs mapping function.
  const urls = getURLsForLevel(levelNumber);

  const cache = await caches.open(cacheName);
  await cache.addAll(urls);
}

(Using postMessage() to communicate with the service worker is an option as well, but it's not required for your use case.)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!