Let's make a PWA of this game!
To make this app fully progressive, it needs to be served over HTTPS. Let's use Google Firebase hosting for that!
- Create a project for yourself at Google Firebase
- Install the Firebase CLI
- Run
firebase init
in this project. Choose "Hosting" and use the current directory (.
) as web directory (instead of the defaultpublic
path).
Now you can serve the app locally with firebase serve
. You can deploy the app to a HTTPS environment using firebase deploy
.
You can test the PWA features using Lighthouse in Chrome Devtools.
This part can be done locally. Use firebase serve
to run the app locally.
- Register the service worker in
app.js
. Check in your Devtools if it is being registered correctly. - Prefetch all the required assets (
/
,/style.css
,/app.js
) oninstall
. Check in your Devtools if the assets are added to the application cache. - Now serve the cached assets from cache, on the
fetch
event. Check if the app works offline. - All right, you made it so far! Now it would be nice to update the pre-fetched assets when there are updates.
You can check it by changing something in
index.html
, without changing the service worker, and see if it is updated in the cache on refresh (and being served after a second refresh).
A bonus exercise: support different caches per service worker instance. I recommend doing exercise B first!
- You need a different cache name per instance.
- On
activate
of the new service worker, it should cleanup the old caches. - The new service worker is being activated when all tabs are closed.
- Add a app
manifest.json
. You can use App Manifest Generator for that. - Register it using
<link rel="manifest" href="manifest.json">
. You might also want to add<meta name="theme-color" content="yourhexcolorhere">
. - Validate your manifest using Chrome Lighthouse.
For iOS, some manifest options are ignored / not implemented yet. You the following metatags to make it work on iOS (replace the values!).
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="your hex color here">
<meta name="apple-mobile-web-app-title" content="your app name here">
<link rel="apple-touch-icon" href="images/icons/icon-152x152.png">
Now deploy using firebase deploy
. On both iOS and Android you should be able to add it to your homescreen and use it offline.
On Android, the PWA will be detected, and a popup should automatically show up within 30 seconds.