My blog website needs to be a progressive web application. Or in other words, a mobile app that looks like it knows what it’s doing. Lighthouse also needs to give me a good score.
So this was an interesting problem I never faced before! Make a statically generated website a progressive web application (PWA). The placement of PWA requisite files for a normal web application is familiar, but making a hugo app changes it up a bit. So I read some articles!
I learned that the
sw.js files should go with the
./static folder. The icons can also be accessed through the
./static/icons folder, or where ever I placed them.
But how am I to make the website know where these files are?
I just pulled out some of the HTML template files from the theme, and I put them into my own
./layouts folder. In the
footer.html templates, I put the corresponding script, meta, and link tags.
What about a caching strategy? Since website has many pages and it is mostly static, I should be fine caching first then defaulting to the network request. The posts and home page should be cached but not the posts page. The posts page should never cache, because that is where I need new posts to show up. Caching also should not occur when I am developing or in the drafts release, just to make things easier to change. So my service worker file eventually came to look like the following code snippet.
Thanks to this service worker file, there are a set of default files that are cached in the
'install' event. Those files are probably critical. Then in the
'fetch' event, the intent is to cache any file related to a post that is not being developed in
localhost or in