serviceworker-webpack-plugin icon indicating copy to clipboard operation
serviceworker-webpack-plugin copied to clipboard

feat: add assetsHash for cache naming

Open nename0 opened this issue 7 years ago • 1 comments

What is accomplished by your PR?

This PR adds another parameter to the options passed to the transformOptions function. It's an hash of the assets-filename array. I use this for the naming of the cache in the SW, because the jsonStats param does not contain anything useful for naming. Whenever the array of file(name)s to cache changes, so does the assetsHash therefore a new cache is opened and loaded with the new files. Example implementation (a transformOptions function which passes on the assetsHash is needed):

var serviceWorkerOption = ...
const staticCacheName = 'static-' + serviceWorkerOption.assetsHash;
self.addEventListener('install', (event) => {
    event.waitUntil(caches.open(staticCacheName).then((cache) =>
        cache.addAll(serviceWorkerOption.assets)
    ));
});
self.addEventListener('activate', (event) => {
    const deleteOldCaches = caches.keys().then((cacheNames) =>
        Promise.all(cacheNames.filter((cacheName) => staticCacheName !== cacheName)
            .map((cacheName) => caches.delete(cacheName))
        )
    );
    event.waitUntil(deleteOldCaches);
});

Is there something controversial in your PR?

Maybe the default transformOptions function should pass on the assetsHash?

Checklist

  • [x] Run unit tests to ensure all existing tests are still passing
  • [x] Add new passing unit tests to cover the code introduced by your PR
  • [ ] Change the documentation
  • [ ] add an example???

nename0 avatar Sep 23 '18 15:09 nename0

pls merge this uwu

ghost avatar Apr 02 '19 00:04 ghost