Progressive Web Apps (PWAs) allow developers to deliver an app-like experience to the user. Paired with the top-notch performance of Gatsby.js, and you get a blazing fast website.
The steps in this guide assume you have a working Gatsby project. So if you haven’t already, you can get started with Gatsby by following along the Gatsby First Steps article. You’ll be diving right into the site configuration.
The Web App Manifest is a brief JSON file with metadata about your web app. It provides some instructions for the browser on how to behave when installed on the user’s device.
Adding a web app manifest to Gatsby can be done by installing the gatsby-plugin-manifest:
$ npm install --save gatsby-plugin-manifest
Then, we can add the plugin to our Gatsby configuration:
module.exports = {
...
plugins: [
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `Alligator.io`,
short_name: `Alligator`,
start_url: `/`,
background_color: `#FFF`,
theme_color: `#FAE042`,
display: `standalone`,
},
},
],
}
Now let’s take a look at some of the options to configure our web app manifest.
We can define a set of icons for the browser to use when the app is saved to the user’s home screen. There are three options available for us to do so.
There are two automatic options available. With the first, we need to provide Gatsby.js with the largest icon size with the following prerequisites:
We then add the following line to the plugin’s options:
icon: `src/images/icon.png`,
Gatsby will generate a pre-configured set of icons from the provided source.
The hybrid option gives us a bit more flexibility over which icons are automatically generated. We follow the same prerequisite as before for the source file. And add the following to the plugin options:
icon: `src/images/icon.png`,
icons: [
{
src: `/favicons/android-chrome-192x192.png`,
sizes: `192x192`,
type: `image/png`,
},
{
src: `/favicons/android-chrome-512x512.png`,
sizes: `512x512`,
type: `image/png`,
},
],
Here, we can provide the icons
array configurations for the desired icon sizes.
With the manual mode, we’re responsible for providing the desired icons in the static folder, as well as defining the entire web app manifest.
We specify the icon sizes in the plugin’s options, while omitting the icon
setting.
icons: [
{
src: `/favicons/android-chrome-192x192.png`,
sizes: `192x192`,
type: `image/png`,
},
{
src: `/favicons/android-chrome-512x512.png`,
sizes: `512x512`,
type: `image/png`,
},
],
There are a few more options available to override the defaults, such as localization and data fetching, which you can find in on package’s page on the npm website. I’d also recommend visiting the Web App Manifest fundamentals for further insight into what each option can do.
Now that we have our web app manifest ready to go, let’s take a look at how we can generate a Service Worker for our site. To get started, let’s install the gatsby-plugin-offline:
$ npm install --save gatsby-plugin-offline
Then, we can add the following to our Gatsby configuration:
plugins: [`gatsby-plugin-offline`]
Important: For the web app manifest to be cached, we’ll need to list gatsby-plugin-manifest
BEFORE gatsby-plugin-offline
.
The plugin creates a service worker for us by leveraging Google’s Workbox library. We get automatic file caching, enabling pages to be available offline after the user’s first visit.
If there is a specific need to your site that requires a little extra from the service worker, you can override the default options with a workbox config. However, keep in mind that changes to the defaults could break offline support. Be sure to go over the linked resources for further information.
If you find that offline functionality needs to be turned off, you can remove the service worker by utilizing the gatsby-plugin-remove-serviceworker.
To do so, you’ll need to first install the removal plugin and uninstall gatsby-plugin-offline
:
$ npm install gatsby-plugin-remove-serviceworker
$ npm uninstall gatsby-plugin-offline
Then, you can update the Gatsby config:
plugins: [
- `gatsby-plugin-offline`,
+ `gatsby-plugin-remove-serviceworker`,
]
This will ensure the service worker is properly unregistered.
Adding offline support to Gatsby opens up a lot of possibilities. Things like offline browsing, cached assets for faster load times, and saving websites to mobile home screens for easier reach. There’s a lot more you can do with these plugins and I highly encourage you to read the resources linked throughout the article.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!