Skip to content

Commit 1b28131

Browse files
jeffposnickTimer
authored andcommitted
Updates to reflect service worker registration being opt-in (#3924)
* Updates to reflect service worker registration being opt-in. * Fixed an anchor link. * Updates to SWPrecacheWebpackPlugin config, and corresponding docs.
1 parent c1ccbbc commit 1b28131

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

Diff for: packages/react-scripts/config/webpack.config.prod.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,19 @@ module.exports = {
450450
console.log(message);
451451
},
452452
minify: true,
453+
// For unknown URLs, fallback to the index page
454+
navigateFallback: publicUrl + '/index.html',
455+
// Ignores URLs starting from /__ (useful for Firebase):
456+
// https://github.com/facebookincubator/create-react-app/issues/2237#issuecomment-302693219
457+
navigateFallbackWhitelist: [/^(?!\/__).*/],
453458
// Don't precache sourcemaps (they're large) and build asset manifest:
454459
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/],
455-
// `navigateFallback` and `navigateFallbackWhitelist` are disabled by default; see
456-
// https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#service-worker-considerations
457-
// navigateFallback: publicUrl + '/index.html',
458-
// navigateFallbackWhitelist: [/^(?!\/__).*/],
460+
// Disabling skipWaiting ensures better compatibility with web apps that
461+
// use deferred or lazy-loading, at the expense of "keeping around" the
462+
// previously cached version of your web app until all open instances have
463+
// been closed.
464+
// See https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#skip_the_waiting_phase
465+
skipWaiting: false,
459466
}),
460467
// Moment.js is an extremely popular library that bundles large locale files
461468
// by default due to how Webpack interprets its code. This is a practical

Diff for: packages/react-scripts/template/README.md

+43-46
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
7676
- [Getting Started with Styleguidist](#getting-started-with-styleguidist)
7777
- [Publishing Components to npm](#publishing-components-to-npm)
7878
- [Making a Progressive Web App](#making-a-progressive-web-app)
79-
- [Opting Out of Caching](#opting-out-of-caching)
79+
- [Why Opt-in?](#why-opt-in)
8080
- [Offline-First Considerations](#offline-first-considerations)
8181
- [Progressive Web App Metadata](#progressive-web-app-metadata)
8282
- [Analyzing the Bundle Size](#analyzing-the-bundle-size)
@@ -1856,10 +1856,28 @@ Create React App doesn't provide any built-in functionality to publish a compone
18561856
18571857
## Making a Progressive Web App
18581858
1859-
By default, the production build is a fully functional, offline-first
1860-
[Progressive Web App](https://developers.google.com/web/progressive-web-apps/).
1859+
The production build has all the tools necessary to generate a first-class
1860+
[Progressive Web App](https://developers.google.com/web/progressive-web-apps/),
1861+
but **the offline/cache-first behavior is opt-in only**. By default,
1862+
the build process will generate a service worker file, but it will not be
1863+
registered, so it will not take control of your production web app.
18611864
1862-
Progressive Web Apps are faster and more reliable than traditional web pages, and provide an engaging mobile experience:
1865+
In order to opt-in to the offline-first behavior, developers should look for the
1866+
following in their [`src/index.js`](src/index.js) file:
1867+
1868+
```js
1869+
// If you want your app to work offline and load faster, you can change
1870+
// unregister() to register() below. Note this comes with some pitfalls.
1871+
// Learn more about service workers: http://bit.ly/CRA-PWA
1872+
serviceWorker.unregister();
1873+
```
1874+
1875+
As the comment states, switching `serviceWorker.unregister()` to
1876+
`serviceWorker.register()` will opt you in to using the service worker.
1877+
1878+
### Why Opt-in?
1879+
1880+
Offline-first Progressive Web Apps are faster and more reliable than traditional web pages, and provide an engaging mobile experience:
18631881
18641882
* 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.
18651883
* 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.
@@ -1872,33 +1890,14 @@ precache all of your local assets and keep them up to date as you deploy updates
18721890
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)
18731891
for handling all requests for local assets, including
18741892
[navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
1875-
for `/` and `/index.html`, ensuring that your web app is consistently fast, even
1876-
on a slow or unreliable network.
1877-
1878-
>Note: If you are using the `pushState` history API and want to enable
1879-
cache-first navigations for URLs other than `/` and `/index.html`, please
1880-
[follow these steps](#service-worker-considerations).
1881-
1882-
### Opting Out of Caching
1883-
1884-
If you would prefer not to enable service workers prior to your initial
1885-
production deployment, then remove the call to `registerServiceWorker()`
1886-
from [`src/index.js`](src/index.js).
1887-
1888-
If you had previously enabled service workers in your production deployment and
1889-
have decided that you would like to disable them for all your existing users,
1890-
you can swap out the call to `registerServiceWorker()` in
1891-
[`src/index.js`](src/index.js) first by modifying the service worker import:
1892-
```javascript
1893-
import { unregister } from './registerServiceWorker';
1894-
```
1895-
and then call `unregister()` instead.
1896-
After the user visits a page that has `unregister()`,
1897-
the service worker will be uninstalled. Note that depending on how `/service-worker.js` is served,
1898-
it may take up to 24 hours for the cache to be invalidated.
1893+
for your HTML, ensuring that your web app is consistently fast, even on a slow
1894+
or unreliable network.
18991895
19001896
### Offline-First Considerations
19011897
1898+
If you do decide to opt-in to service worker registration, please take the
1899+
following into account:
1900+
19021901
1. Service workers [require HTTPS](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers#you_need_https),
19031902
although to facilitate local testing, that policy
19041903
[does not apply to `localhost`](http://stackoverflow.com/questions/34160509/options-for-testing-service-workers-via-http/34161385#34161385).
@@ -1967,6 +1966,11 @@ icons, names, and branding colors to use when the web app is displayed.
19671966
provides more context about what each field means, and how your customizations
19681967
will affect your users' experience.
19691968
1969+
Progressive web apps that have been added to the homescreen will load faster and
1970+
work offline when there's an active service worker. That being said, the
1971+
metadata from the web app manifest will still be used regardless of whether or
1972+
not you opt-in to service worker registration.
1973+
19701974
## Analyzing the Bundle Size
19711975
19721976
[Source map explorer](https://www.npmjs.com/package/source-map-explorer) analyzes
@@ -2080,27 +2084,20 @@ If you’re using [Apache Tomcat](http://tomcat.apache.org/), you need to follow
20802084
20812085
Now requests to `/todos/42` will be handled correctly both in development and in production.
20822086
2083-
When users install your app to the homescreen of their device the default configuration will make a shortcut to `/`. This may not work if you don't use a client-side router and expect the app to be served from `/index.html`. In this case, the web app manifest at [`public/manifest.json`](public/manifest.json) and change `start_url` to `./index.html`.
2084-
2085-
### Service Worker Considerations
2086-
2087-
[Navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
2088-
for URLs like `/todos/42` will not be intercepted by the
2089-
[service worker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers)
2090-
created by the production build. Navigations for those URLs will always
2091-
require a network connection, as opposed to navigations for `/` and
2092-
`/index.html`, both of which will be served from the cache by the service worker
2093-
and work without requiring a network connection.
2094-
2095-
If you are using the `pushState` history API and would like to enable service
2096-
worker support for navigations to URLs like `/todos/42`, you need to
2097-
[`npm eject`](#npm-run-eject) and enable the [`navigateFallback`](https://github.com/GoogleChrome/sw-precache#navigatefallback-string)
2087+
On a production build, and when you've [opted-in](#why-opt-in),
2088+
a [service worker](https://developers.google.com/web/fundamentals/primers/service-workers/) will automatically handle all navigation requests, like for
2089+
`/todos/42`, by serving the cached copy of your `index.html`. This
2090+
service worker navigation routing can be configured or disabled by
2091+
[`eject`ing](#npm-run-eject) and then modifying the
2092+
[`navigateFallback`](https://github.com/GoogleChrome/sw-precache#navigatefallback-string)
20982093
and [`navigateFallbackWhitelist`](https://github.com/GoogleChrome/sw-precache#navigatefallbackwhitelist-arrayregexp)
20992094
options of the `SWPreachePlugin` [configuration](../config/webpack.config.prod.js).
21002095
2101-
>Note: This is a [change in default behavior](https://github.com/facebook/create-react-app/issues/3248),
2102-
as earlier versions of `create-react-app` shipping with `navigateFallback`
2103-
enabled by default.
2096+
When users install your app to the homescreen of their device the default configuration will make a shortcut to `/index.html`. This may not work for client-side routers which expect the app to be served from `/`. Edit the web app manifest at [`public/manifest.json`](public/manifest.json) and change `start_url` to match the required URL scheme, for example:
2097+
2098+
```js
2099+
"start_url": ".",
2100+
```
21042101
21052102
### Building for Relative Paths
21062103

0 commit comments

Comments
 (0)