0

I was reading about precaching with service workers and i ended up with this precaching.

I cant understand why it uses fixed urls with hashes. They must be created dynamically every time a file changes.

So whats the point of putting them manually and setting revision to null?

George Paouris
  • 525
  • 4
  • 16

1 Answers1

0

If you are talking about the way Workbox tags the file hash value to the URL then I can answer this one.

The hash is a calculation of the file contents. Which stays the same until you change anything in the file. Then the file has a different hash. So Workbox generates a manifest each time you run the script to generate the service worker.

Then when the service worker is installed it will only fetch updates for network assets that have been updated. It can also do a hash check to verify the file is what is expects.

Another way I have been accomplishing this without a manifest file and a build step to maintain is to make HEAD requests to the network assets. The server should return a last updated header value you can check against a value in the cache of when the file was created.

I have not quite perfected this technique, but when I get it stable I will probably create an article about the method.

But the ultimate goal is to eliminate as many round trips as possible and reduce the data burden on the server and the client.

Chris Love
  • 3,740
  • 1
  • 19
  • 16
  • Thank you for your response but i asked a different question. Iam asking why the documentation presents a manifest with fixed urls and hashes when the correct way is to dynamically create them. If you are putting them like fixed strings then everytime you change something to a file, you also have to change it to SW file. – George Paouris Feb 17 '20 at 06:37
  • for precached assets they will most likely be fixed, static assets like JS or CSS files. If you are looking to cache a large site, like a large e-commerce site then you do not pre-cache everything. Caching is an art and there are at least 3 dozen different stategies I have employed on different apps. Then you get into invalidation rules, etc. It is a very complex topic to do correctly. – Chris Love Feb 17 '20 at 15:32