Skip to content

Commit 811b4c8

Browse files
committed
docs: Added documentation for PWA setup
1 parent d4737bd commit 811b4c8

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

template/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ You can find the most recent version of this guide [here](https://github.com/hal
3030
- [Setting up API Proxy](#setting-up-api-proxy)
3131
- [Running tests](#running-tests)
3232
- [Dependencies in Tests](#dependencies-in-tests)
33+
- [Making a Progressive Web App](#making-a-progressive-web-app)
34+
- [Opting Out of Caching](#opting-out-of-caching)
35+
- [Offline-First Considerations](#offline-first-considerations)
36+
- [Progressive Web App Metadata](#progressive-web-app-metadata)
3337
- [Deployment](#deployment)
3438
- [Static Server](#static-server)
3539
- [GitHub Pages](#github-pages)
@@ -317,6 +321,109 @@ To use packages in tests, you also need to install them in `tests` directory.
317321
elm-app test --add-dependencies elm-package.json
318322
```
319323

324+
## Making a Progressive Web App
325+
326+
By default, the production build is a fully functional, offline-first
327+
[Progressive Web App](https://developers.google.com/web/progressive-web-apps/).
328+
329+
Progressive Web Apps are faster and more reliable than traditional web pages, and provide an engaging mobile experience:
330+
331+
* All static site assets are cached so that your page loads fast on subsequent visits, regardless of network connectivity (such as 2G or 3G). Updates are downloaded in the background.
332+
* Your app will work regardless of network state, even if offline. This means your users will be able to use your app at 10,000 feet and on the Subway.
333+
* On mobile devices, your app can be added directly to the user's home screen, app icon and all. You can also re-engage users using web **push notifications**. This eliminates the need for the app store.
334+
335+
The [`sw-precache-webpack-plugin`](https://github.com/goldhand/sw-precache-webpack-plugin)
336+
is integrated into production configuration,
337+
and it will take care of generating a service worker file that will automatically
338+
precache all of your local assets and keep them up to date as you deploy updates.
339+
The service worker will use a [cache-first strategy](https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network)
340+
for handling all requests for local assets, including the initial HTML, ensuring
341+
that your web app is reliably fast, even on a slow or unreliable network.
342+
343+
### Opting Out of Caching
344+
345+
If you would prefer not to enable service workers prior to your initial
346+
production deployment, then remove the call to `serviceWorkerRegistration.register()`
347+
from [`src/index.js`](src/index.js).
348+
349+
If you had previously enabled service workers in your production deployment and
350+
have decided that you would like to disable them for all your existing users,
351+
you can swap out the call to `serviceWorkerRegistration.register()` in
352+
[`src/index.js`](src/index.js) with a call to `serviceWorkerRegistration.unregister()`.
353+
After the user visits a page that has `serviceWorkerRegistration.unregister()`,
354+
the service worker will be uninstalled. Note that depending on how `/service-worker.js` is served,
355+
it may take up to 24 hours for the cache to be invalidated.
356+
357+
### Offline-First Considerations
358+
359+
1. Service workers [require HTTPS](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers#you_need_https),
360+
although to facilitate local testing, that policy
361+
[does not apply to `localhost`](http://stackoverflow.com/questions/34160509/options-for-testing-service-workers-via-http/34161385#34161385).
362+
If your production web server does not support HTTPS, then the service worker
363+
registration will fail, but the rest of your web app will remain functional.
364+
365+
1. Service workers are [not currently supported](https://jakearchibald.github.io/isserviceworkerready/)
366+
in all web browsers. Service worker registration [won't be attempted](src/registerServiceWorker.js)
367+
on browsers that lack support.
368+
369+
1. The service worker is only enabled in the [production environment](#deployment),
370+
e.g. the output of `npm run build`. It's recommended that you do not enable an
371+
offline-first service worker in a development environment, as it can lead to
372+
frustration when previously cached assets are used and do not include the latest
373+
changes you've made locally.
374+
375+
1. If you *need* to test your offline-first service worker locally, build
376+
the application (using `npm run build`) and run a simple http server from your
377+
build directory. After running the build script, `create-react-app` will give
378+
instructions for one way to test your production build locally and the [deployment instructions](#deployment) have
379+
instructions for using other methods. *Be sure to always use an
380+
incognito window to avoid complications with your browser cache.*
381+
382+
1. If possible, configure your production environment to serve the generated
383+
`service-worker.js` [with HTTP caching disabled](http://stackoverflow.com/questions/38843970/service-worker-javascript-update-frequency-every-24-hours).
384+
If that's not possible—[GitHub Pages](#github-pages), for instance, does not
385+
allow you to change the default 10 minute HTTP cache lifetime—then be aware
386+
that if you visit your production site, and then revisit again before
387+
`service-worker.js` has expired from your HTTP cache, you'll continue to get
388+
the previously cached assets from the service worker. If you have an immediate
389+
need to view your updated production deployment, performing a shift-refresh
390+
will temporarily disable the service worker and retrieve all assets from the
391+
network.
392+
393+
1. Users aren't always familiar with offline-first web apps. It can be useful to
394+
[let the user know](https://developers.google.com/web/fundamentals/instant-and-offline/offline-ux#inform_the_user_when_the_app_is_ready_for_offline_consumption)
395+
when the service worker has finished populating your caches (showing a "This web
396+
app works offline!" message) and also let them know when the service worker has
397+
fetched the latest updates that will be available the next time they load the
398+
page (showing a "New content is available; please refresh." message). Showing
399+
this messages is currently left as an exercise to the developer, but as a
400+
starting point, you can make use of the logic included in [`src/registerServiceWorker.js`](src/registerServiceWorker.js), which
401+
demonstrates which service worker lifecycle events to listen for to detect each
402+
scenario, and which as a default, just logs appropriate messages to the
403+
JavaScript console.
404+
405+
1. By default, the generated service worker file will not intercept or cache any
406+
cross-origin traffic, like HTTP [API requests](#integrating-with-an-api-backend),
407+
images, or embeds loaded from a different domain. If you would like to use a
408+
runtime caching strategy for those requests, you can [`eject`](#npm-run-eject)
409+
and then configure the
410+
[`runtimeCaching`](https://github.com/GoogleChrome/sw-precache#runtimecaching-arrayobject)
411+
option in the `SWPrecacheWebpackPlugin` section of
412+
[`webpack.config.prod.js`](../config/webpack.config.prod.js).
413+
414+
### Progressive Web App Metadata
415+
416+
The default configuration includes a web app manifest located at
417+
[`public/manifest.json`](public/manifest.json), that you can customize with
418+
details specific to your web application.
419+
420+
When a user adds a web app to their homescreen using Chrome or Firefox on
421+
Android, the metadata in [`manifest.json`](public/manifest.json) determines what
422+
icons, names, and branding colors to use when the web app is displayed.
423+
[The Web App Manifest guide](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/)
424+
provides more context about what each field means, and how your customizations
425+
will affect your users' experience.
426+
320427
## Deployment
321428

322429

0 commit comments

Comments
 (0)