Adding PWA to your Hugo static site is quite easy.
What is PWA and why should I add it?
Progressive web applications (PWA) are a nice way to give your websites an “app makeover”. Using PWAs your sites are perceived to load faster, are available offline, and in general improve user experience.
PWAs are supported by all web browsers. Using PWAs on single page applications have shown remarkable improvement in site speed and have provided us the ability to continuing to serve static content even when the connectivity is down.
Back to Hugo - although I can see incremental gains, nothing jumped out to make up for a compelling case for PWA. The sites generated by Hugo are static - pages are already generated and waiting to be loaded. Depending on the theme, you may already be seeing the best load times that your site can achieve. So, YMMV.
How do I add PWA to Hugo?
At a minimum a PWA needs a manifest and service worker. We will use a offline-first cache strategy to enable PWA.
Create your icons
PWAs are available offline and can be installed on the phone. This requires icons of different sizes for your website. Just pluck your logo, remove the text if necessary and generate icons of all popular sizes. You can use a service like Favicomatic to make that a one-click process.
Create manifest
Create a new file called manifest.json in your /static folder and add content similar to below
Create a new javascript file sw.js in /static. This script does all the heavy lifting like maintaining cache and enabling your site to be treated like an app.
I just copied the code from the sample service worker script available at [https://github.com/wildhaber/offline-first-sw].
Modify the file and choose to make some or all files available offline - I typically include CSS, common Javascript files and images. See the end of this article for the file used by this blog.
Include manifest in head
Include a link to your manifest in your header.
I use Minimo theme and included the following line in <root>/layouts/partials/head/extra.html. Note that I am not over-writing the file in the theme folder, but overriding it using my own version (as is typical to Hugo themes).
1
<linkrel="manifest"href="/manifest.json">
Include SW in footer
Include the service worker JS file reference in your footer. For some strange reason, I chose to be different and included that in my sidebar.html.
You should see details provided in your manifest including the different icons. You will also see the different offline files you specified in the Cache Storage section.
Lighthouse scores
Go to Audit tab and do a Lighthouse audit for your site.
Mobile Lighthouse score for home page -
Lighthouse score for an article -
Rejoice.
Conclusion
Adding and setting up PWA to Hugo is trivial. But, I did not clearly make out any significant advantages of enabling PWA on my blog - given that I am not after providing a read-offline feature.