You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PWA-ification, via SWPrecacheWebpackPlugin + manifest.json (#1728)
* sw-precache-webpack-plugin, SW registration, manifest.json
* Documentation + a few SW tweaks.
* Added an unregister method, too, just in case.
* More info for the READMEs.
* Add minify to SWPrecacheWebpackPlugin config
* Fix SWPrecacheWebpackPlugin typo
* Fix file references in READMEmd
* Add instructions for testing service-worker locally
* Review feedback
* Some additional PWA metadata
* Use sw-precache-webpack-plugin v0.9.1 for node >=4.0.0 support
* Review feedback.
* Add manifest.json context in a comment.
* Fix typo
* Downgrade to sw-precache-webpack-plugin 0.9.1
* Hide changes in README until 1.0
* Hide changes in User Guide until 1.0
* Hide more docs
Copy file name to clipboardexpand all lines: README.md
+6
Original file line number
Diff line number
Diff line change
@@ -101,6 +101,9 @@ It correctly bundles React in production mode and optimizes the build for the be
101
101
102
102
The build is minified and the filenames include the hashes.<br>
103
103
Your app is ready to be deployed!
104
+
<!--TODO: uncoment and maybe revise
105
+
A [service worker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers) using an [offline-first caching strategy](https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network) is automatically generated.<br>
106
+
Your ([progressive web](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#making-a-progressive-web-app)) app is ready to be deployed!-->
104
107
105
108
## User Guide
106
109
@@ -162,6 +165,9 @@ Please refer to the [User Guide](https://github.com/facebookincubator/create-rea
162
165
* Import CSS and image files directly from JavaScript.
163
166
* Autoprefixed CSS, so you don’t need `-webkit` or other prefixes.
164
167
* A `build` script to bundle JS, CSS, and images for production, with sourcemaps.
168
+
<!--TODO: uncomment
169
+
* An offline-first [service worker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers) and a [web app manifest](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/), meeting all the [Progressive Web App](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#making-a-progressive-web-app) criteria.
170
+
-->
165
171
166
172
**The feature set is intentionally limited**. It doesn’t support advanced features such as server rendering or CSS modules. The tool is also **non-configurable** because it is hard to provide a cohesive experience and easy updates across a set of tools when the user can tweak anything.
- [Progressive Web App Metadata](#progressive-web-app-metadata)
67
+
-->
64
68
-[Deployment](#deployment)
65
69
-[Static Server](#static-server)
66
70
-[Other Solutions](#other-solutions)
@@ -1216,6 +1220,103 @@ Learn more about React Storybook:
1216
1220
1217
1221
You can turn your React app into a [Progressive Web App](https://developers.google.com/web/progressive-web-apps/) by following the steps in [this repository](https://github.com/jeffposnick/create-react-pwa).
1218
1222
1223
+
<!--
1224
+
TODO: uncomment
1225
+
By default, the production build is a fully functional, offline-first
1226
+
[Progressive Web App](https://developers.google.com/web/progressive-web-apps/).
1227
+
1228
+
The [`sw-precache-webpack-plugin`](https://github.com/goldhand/sw-precache-webpack-plugin)
1229
+
is integrated into [`webpack.config.prod.js`](../config/webpack.config.prod.js),
1230
+
and it will take care of generating a service worker file that will automatically
1231
+
precache all of your local assets and keep them up to date as you deploy updates.
1232
+
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)
1233
+
for handling all requests for local assets, including the initial HTML, ensuring
1234
+
that you web app is reliably fast, even on a slow or unreliable network.
1235
+
1236
+
If you would prefer not to enable service workers prior to your initial
1237
+
production deployment, then remove the call to `serviceWorkerRegistration.register()`
1238
+
from [`src/index.js`](src/index.js).
1239
+
1240
+
If you had previously enabled service workers in your production deployment and
1241
+
have decided that you would like to disable them for all your existing users,
1242
+
you can swap out the call to `serviceWorkerRegistration.register()` in
1243
+
[`src/index.js`](src/index.js) with a call to `serviceWorkerRegistration.unregister()`.
1244
+
After the user visits a page that has `serviceWorkerRegistration.unregister()`,
1245
+
the service worker will be uninstalled.
1246
+
1247
+
### Offline-First Considerations
1248
+
1249
+
1. Service workers [require HTTPS](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers#you_need_https),
1250
+
although to facilitate local testing, that policy
1251
+
[does not apply to `localhost`](http://stackoverflow.com/questions/34160509/options-for-testing-service-workers-via-http/34161385#34161385).
1252
+
If your production web server does not support HTTPS, then the service worker
1253
+
registration will fail, but the rest of your web app will remain functional.
1254
+
1255
+
1. Service workers are [not currently supported](https://jakearchibald.github.io/isserviceworkerready/)
1256
+
in all web browsers. Service worker registration [won't be attempted](src/service-worker-registration.js)
1257
+
on browsers that lack support.
1258
+
1259
+
1. The service worker is only enabled in the [production environment](#deployment),
1260
+
e.g. the output of `npm run build`. It's recommended that you do not enable an
1261
+
offline-first service worker in a development environment, as it can lead to
1262
+
frustration when previously cached assets are used and do not include the latest
1263
+
changes you've made locally.
1264
+
1265
+
1. If you *need* to test your offline-first service worker locally, build
1266
+
the application (using `npm run build`) and run a simple http server from your
1267
+
build directory. After running the build script, `create-react-app` will give
1268
+
instructions for one way to test your production build locally using
1269
+
`pushstate-server` and the [deployment instructions](#deployment) have
1270
+
instructions for using the python `SimpleHTTPServer`. *Be sure to always use an
1271
+
incognito window to avoid complications with your browser cache.*
1272
+
1273
+
1. If possible,configure your production environment to serve the generated
If that's not possible—[GitHub Pages](#github-pages), for instance, does not
1276
+
allow you to change the default 10 minute HTTP cache lifetime—then be aware
1277
+
that if you visit your production site, and then revisit again before
1278
+
`service-worker.js` has expired from your HTTP cache, you'll continue to get
1279
+
the previously cached assets from the service worker. If you have an immediate
1280
+
need to view your updated production deployment, performing a shift-refresh
1281
+
will temporarily disable the service worker and retrieve all assets from the
1282
+
network.
1283
+
1284
+
1. Users aren't always familiar with offline-first web apps. It can be useful to
1285
+
[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)
1286
+
when the service worker has finished populating your caches (showing a "This web
1287
+
app works offline!" message) and also let them know when the service worker has
1288
+
fetched the latest updates that will be available the next time they load the
1289
+
page (showing a "New content is available; please refresh." message). Showing
1290
+
this messages is currently left as an exercise to the developer, but as a
1291
+
starting point, you can make use of the logic included in [`src/service-worker-registration.js`](src/service-worker-registration.js), which
1292
+
demonstrates which service worker lifecycle events to listen for to detect each
1293
+
scenario, and which as a default, just logs appropriate messages to the
1294
+
JavaScript console.
1295
+
1296
+
1. By default, the generated service worker file will not intercept or cache any
1297
+
cross-origin traffic, like HTTP [API requests](#integrating-with-an-api-backend),
1298
+
images, or embeds loaded from a different domain. If you would like to use a
1299
+
runtime caching strategy for those requests, you can [`eject`](#npm-run-eject)
The default configuration includes a web app manifest located at
1308
+
[`public/manifest.json`](public/manifest.json), that you can customize with
1309
+
details specific to your web application.
1310
+
1311
+
When a user adds a web app to their homescreen using Chrome or Firefox on
1312
+
Android, the metadata in [`manifest.json`](public/manifest.json) determines what
1313
+
icons, names, and branding colors to use when the web app is displayed.
1314
+
[The Web App Manifest guide](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/)
1315
+
provides more context about what each field means, and how your customizations
1316
+
will affect your users' experience.
1317
+
1318
+
-->
1319
+
1219
1320
## Deployment
1220
1321
1221
1322
`npm run build` creates a `build` directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served `index.html`, and requests to static paths like `/static/js/main.<hash>.js` are served with the contents of the `/static/js/main.<hash>.js` file.
@@ -1291,6 +1392,19 @@ It will get copied to the `build` folder when you run `npm run build`.
1291
1392
1292
1393
Now requests to `/todos/42` will be handled correctly both in development and in production.
1293
1394
1395
+
<!--
1396
+
TODO: uncomment for 1.0
1397
+
1398
+
On a production build, and in a browser that supports [service workers](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers),
1399
+
the service worker will automatically handle all navigation requests, like for
1400
+
`/todos/42`, by serving the cached copy of your `index.html`. This
1401
+
service worker navigation routing can be configured or disabled by
1402
+
[`eject`ing](#npm-run-eject) and then modifying the
0 commit comments