grow / grow.dev

Grow's homepage and documentation site.
https://grow.io
85 stars 36 forks source link

Using Service Workers (PWA) with Grow #46

Closed Quirksmode closed 6 years ago

Quirksmode commented 6 years ago

Hi, I have been using the Grow framework for a few weeks now and have been really impressed. One thing I am struggling with is converting the site to a PWA.

The issue I have is that because I am using the fingerprint functionality for my CSS, JS and Images, (which generates fingerprinted file names) I am then unable to target these files from within JS files (service-worker.js). I need to be able to pass to the JS file what the fingerprint and root path will be.

One idea I had was to create a global JS object and pass the variables to the service-worker.js file

e.g. within base.html

let APP = {} APP.fingerprint = {{env.fingerprint}} APP.root = {{podspec.root}}

The problem I have is that the env.fingerprint variable does not seem to match fingerprint on the built files.

How can I assign the correct fingerprint variables so I can set the correct paths for the files in the service-worker.js file?

e.g.

const installFilesEssential = [ root + 'static/js/main-' + fingerprint + '.min.js', root + 'static/css/main-' + fingerprint + '.css', ]

Does this technique seem ok, or is there a better way?

Zoramite commented 6 years ago

The fingerprint you were using (env.fingerprint) is a fingerprint for the entire environment. What you are looking for would be a fingerprint for the static document. Something like this should work:

{{g.static('/static/js/main.min.js').fingerprint}}

This would then load the static file and get the correct fingerprint at build time.