Skip to content

Commit 51729f6

Browse files
authored
Merge pull request #1 from facebook/master
pull
2 parents 759696d + bdae9b6 commit 51729f6

File tree

11 files changed

+116
-354
lines changed

11 files changed

+116
-354
lines changed

docusaurus/docs/making-a-progressive-web-app.md

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,42 @@ title: Making a Progressive Web App
55

66
The production build has all the tools necessary to generate a first-class
77
[Progressive Web App](https://developers.google.com/web/progressive-web-apps/),
8-
but **the offline/cache-first behavior is opt-in only**. By default,
9-
the build process will generate a service worker file, but it will not be
10-
registered, so it will not take control of your production web app.
8+
but **the offline/cache-first behavior is opt-in only**.
9+
10+
Starting with Create React App 4, you can add a `src/service-worker.js` file to
11+
your project to use the built-in support for
12+
[Workbox](https://developers.google.com/web/tools/workbox/)'s
13+
[`InjectManifest`](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-webpack-plugin.InjectManifest)
14+
plugin, which will
15+
[compile](https://developers.google.com/web/tools/workbox/guides/using-bundlers)
16+
your service worker and inject into it a list of URLs to
17+
[precache](https://developers.google.com/web/tools/workbox/guides/precache-files).
18+
19+
If you start a new project using one of the PWA [custom
20+
templates](https://create-react-app.dev/docs/custom-templates/), you'll get a
21+
`src/service-worker.js` file that serves as a good starting point for an
22+
offline-first service worker:
23+
24+
```sh
25+
npx create-react-app my-app --template cra-template-pwa
26+
```
27+
28+
The TypeScript equivalent is:
29+
30+
```sh
31+
npx create-react-app my-app --template cra-template-pwa-typescript
32+
```
33+
34+
If you know that you won't be using service workers, or if you'd prefer to use a
35+
different approach to creating your service worker, don't create a
36+
`src/service-worker.js` file. The `InjectManifest` plugin won't be run in that
37+
case.
1138

12-
In order to opt-in to the offline-first behavior, developers should look for the
13-
following in their [`src/index.js`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/src/index.js) file:
39+
In addition to creating your local `src/service-worker.js` file, it needs to be
40+
registered before it will be used. In order to opt-in to the offline-first
41+
behavior, developers should look for the following in their
42+
[`src/index.js`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/src/index.js)
43+
file:
1444

1545
```js
1646
// If you want your app to work offline and load faster, you can change
@@ -24,23 +54,59 @@ As the comment states, switching `serviceWorker.unregister()` to
2454

2555
## Why Opt-in?
2656

27-
Offline-first Progressive Web Apps are faster and more reliable than traditional web pages, and provide an engaging mobile experience:
57+
Offline-first Progressive Web Apps are faster and more reliable than traditional
58+
web pages, and provide an engaging mobile experience:
59+
60+
- All static site assets that are a part of your `webpack` build are cached so
61+
that your page loads fast on subsequent visits, regardless of network
62+
connectivity (such as 2G or 3G). Updates are downloaded in the background.
63+
- Your app will work regardless of network state, even if offline. This means
64+
your users will be able to use your app at 10,000 feet and on the subway.
65+
- On mobile devices, your app can be added directly to the user's home screen,
66+
app icon and all. This eliminates the need for the app store.
67+
68+
However, they [can make debugging deployments more
69+
challenging](https://github.com/facebook/create-react-app/issues/2398).
70+
71+
The
72+
[`workbox-webpack-plugin`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin)
73+
is integrated into production configuration, and it will take care of compiling
74+
a service worker file that will automatically precache all of your
75+
`webpack`-generated assets and keep them up to date as you deploy updates. The
76+
service worker will use a [cache-first
77+
strategy](https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network)
78+
for handling all requests for `webpack`-generated assets, including [navigation
79+
requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
80+
for your HTML, ensuring that your web app is consistently fast, even on a slow
81+
or unreliable network.
2882

29-
- 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.
30-
- 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.
31-
- On mobile devices, your app can be added directly to the user's home screen, app icon and all. This eliminates the need for the app store.
83+
Note: Resources that are not generated by `webpack`, such as static files that are
84+
copied over from your local
85+
[`public/` directory](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/public/)
86+
or third-party resources, will not be precached. You can optionally set up Workbox
87+
[routes](https://developers.google.com/web/tools/workbox/guides/route-requests)
88+
to apply the runtime caching strategy of your choice to those resources.
89+
90+
## Customization
91+
92+
Starting with Create React App 4, you have full control over customizing the
93+
logic in this service worker, by creating your own `src/service-worker.js` file,
94+
or customizing the one added by the `cra-template-pwa` (or
95+
`cra-template-pwa-typescript`) template. You can use [additional
96+
modules](https://developers.google.com/web/tools/workbox/modules) from the
97+
Workbox project, add in a push notification library, or remove some of the
98+
default caching logic. The one requirement is that you keep `self.__WB_MANIFEST`
99+
somewhere in your file, as the Workbox compilation plugin checks for this value
100+
when generating a manifest of URLs to precache. If you would prefer not to use
101+
precaching, you can just assign `self.__WB_MANIFEST` to a variable that will be
102+
ignored, like:
32103

33-
However, they [can make debugging deployments more challenging](https://github.com/facebook/create-react-app/issues/2398) so, starting with Create React App 2, service workers are opt-in.
104+
```js
105+
// eslint-disable-next-line no-restricted-globals
106+
const ignored = self.__WB_MANIFEST;
34107

35-
The [`workbox-webpack-plugin`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin)
36-
is integrated into production configuration,
37-
and it will take care of generating a service worker file that will automatically
38-
precache all of your local assets and keep them up to date as you deploy updates.
39-
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)
40-
for handling all requests for local assets, including
41-
[navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
42-
for your HTML, ensuring that your web app is consistently fast, even on a slow
43-
or unreliable network.
108+
// Your custom service worker code goes here.
109+
```
44110

45111
## Offline-First Considerations
46112

@@ -88,7 +154,8 @@ following into account:
88154

89155
1. By default, the generated service worker file will not intercept or cache any
90156
cross-origin traffic, like HTTP [API requests](integrating-with-an-api-backend.md),
91-
images, or embeds loaded from a different domain.
157+
images, or embeds loaded from a different domain. Starting with Create
158+
React App 4, this can be customized, as explained above.
92159

93160
## Progressive Web App Metadata
94161

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@testing-library/react": "^10.2.1",
2525
"@testing-library/user-event": "^12.0.2",
2626
"alex": "^8.0.0",
27-
"eslint": "^7.3.0",
27+
"eslint": "^7.5.0",
2828
"execa": "1.0.0",
2929
"fs-extra": "^9.0.0",
3030
"get-port": "^5.1.1",

packages/cra-template-typescript/template/src/index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
65
import reportWebVitals from './reportWebVitals';
76

87
ReactDOM.render(
@@ -12,11 +11,6 @@ ReactDOM.render(
1211
document.getElementById('root')
1312
);
1413

15-
// If you want your app to work offline and load faster, you can change
16-
// unregister() to register() below. Note this comes with some pitfalls.
17-
// Learn more about service workers: https://cra.link/PWA
18-
serviceWorker.unregister();
19-
2014
// If you want to start measuring performance in your app, pass a function
2115
// to log results (for example: reportWebVitals(console.log))
2216
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals

packages/cra-template-typescript/template/src/serviceWorker.ts

Lines changed: 0 additions & 149 deletions
This file was deleted.

packages/cra-template/template/src/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
65
import reportWebVitals from './reportWebVitals';
76

87
ReactDOM.render(
@@ -12,11 +11,6 @@ ReactDOM.render(
1211
document.getElementById('root')
1312
);
1413

15-
// If you want your app to work offline and load faster, you can change
16-
// unregister() to register() below. Note this comes with some pitfalls.
17-
// Learn more about service workers: https://cra.link/PWA
18-
serviceWorker.unregister();
19-
2014
// If you want to start measuring performance in your app, pass a function
2115
// to log results (for example: reportWebVitals(console.log))
2216
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals

0 commit comments

Comments
 (0)