Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

feat: add assetsHash for cache naming #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nename0
Copy link

@nename0 nename0 commented Sep 23, 2018

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

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

Copy link
Collaborator

@woutervanvliet woutervanvliet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR, it's actually something that I'll be needing myself in the near future.

Just got a few tiny remarks about the tests, but otherwise I'm quite happy.

const transformOptions = serviceWorkerOption => {
expect(serviceWorkerOption)
.to.have.property('assetsHash')
.that.is.a('string')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the input is known, so is the output - could you update the test case and let it test against the actual expected value of the hash?

options: fakeOptions,
assets: {
[filename]: {
source: () => '',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can add a second test case, with a different mocked source, as evidence that the hash is something else when the contents change.

Copy link
Author

@nename0 nename0 Sep 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think we've a design decision here. Maybe I didn't explain fully, but currently only the filenames get hashed not the contents. In my project I use filenames with hashes so there is no problem with this. On the other hand after reading this I think the only real use case to make the assetsHash include the content would be to make the serviceworker byte different when the assets change, but not their filenames. In this case it would probably be easier to just make the jsonStats include the compilation hash by changing this line to true.
What do you think @woutervanvliet ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I indeed misunderstood the point of your PR, but yes - my approach would be to have some kind of value, that's the same between builds of the same code, but different when sourcecode changes.

Tbh; I'm not that familiar with webpack internals, but it sounds like enabling the hash would solve the same problem as your PR does.

@devCrossNet @oliviertassinari What do you say?

@ghost
Copy link

ghost commented Apr 2, 2019

pls merge this uwu

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants