Skip to content

Commit c49423c

Browse files
committed
Fix lint error
1 parent 998c3d3 commit c49423c

File tree

2 files changed

+40
-133
lines changed

2 files changed

+40
-133
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Lint
22

3-
on: [push]
3+
on: [push, pull_request]
44

55
jobs:
66
lint:

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

Lines changed: 39 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@ id: making-a-progressive-web-app
33
title: Making a Progressive Web App
44
---
55

6-
The production build has all the tools necessary to generate a first-class
7-
[Progressive Web App](https://developers.google.com/web/progressive-web-apps/),
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:
6+
The production build has all the tools necessary to generate a first-class [Progressive Web App](https://developers.google.com/web/progressive-web-apps/), but **the offline/cache-first behavior is opt-in only**.
7+
8+
Starting with Create React App 4, you can add a `src/service-worker.js` file to your project to use the built-in support for
9+
[Workbox](https://developers.google.com/web/tools/workbox/)'s [`InjectManifest`](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-webpack-plugin.InjectManifest) plugin, which will [compile](https://developers.google.com/web/tools/workbox/guides/using-bundlers) your service worker and inject into it a list of URLs to [precache](https://developers.google.com/web/tools/workbox/guides/precache-files).
10+
11+
If you start a new project using one of the PWA [custom templates](https://create-react-app.dev/docs/custom-templates/), you'll get a `src/service-worker.js` file that serves as a good starting point for an offline-first service worker:
2312

2413
```sh
2514
npx create-react-app my-app --template cra-template-pwa
@@ -31,16 +20,9 @@ The TypeScript equivalent is:
3120
npx create-react-app my-app --template cra-template-pwa-typescript
3221
```
3322

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.
23+
If you know that you won't be using service workers, or if you'd prefer to use a different approach to creating your service worker, don't create a `src/service-worker.js` file. The `InjectManifest` plugin won't be run in that case.
3824

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:
25+
In addition to creating your local `src/service-worker.js` file, it needs to be registered before it will be used. In order to opt-in to the offline-first behavior, developers should look for the following in their [`src/index.js`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/src/index.js) file:
4426

4527
```js
4628
// If you want your app to work offline and load faster, you can change
@@ -49,57 +31,25 @@ file:
4931
serviceWorker.unregister();
5032
```
5133

52-
As the comment states, switching `serviceWorker.unregister()` to
53-
`serviceWorker.register()` will opt you in to using the service worker.
34+
As the comment states, switching `serviceWorker.unregister()` to `serviceWorker.register()` will opt you in to using the service worker.
5435

5536
## Why Opt-in?
5637

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.
82-
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.
38+
Offline-first Progressive Web Apps are faster and more reliable than traditional web pages, and provide an engaging mobile experience:
39+
40+
- All static site assets that are a part of your `webpack` build 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.
41+
- 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.
42+
- 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.
43+
44+
However, they [can make debugging deployments more challenging](https://github.com/facebook/create-react-app/issues/2398).
45+
46+
The [`workbox-webpack-plugin`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin) is integrated into production configuration, and it will take care of compiling a service worker file that will automatically precache all of your `webpack`-generated assets and keep them up to date as you deploy updates. 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) for handling all requests for `webpack`-generated assets, including [navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests) for your HTML, ensuring that your web app is consistently fast, even on a slow or unreliable network.
47+
48+
Note: Resources that are not generated by `webpack`, such as static files that are copied over from your local [`public/` directory](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/public/) or third-party resources, will not be precached. You can optionally set up Workbox [routes](https://developers.google.com/web/tools/workbox/guides/route-requests) to apply the runtime caching strategy of your choice to those resources.
8949

9050
## Customization
9151

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:
52+
Starting with Create React App 4, you have full control over customizing the logic in this service worker, by creating your own `src/service-worker.js` file, or customizing the one added by the `cra-template-pwa` (or `cra-template-pwa-typescript`) template. You can use [additional modules](https://developers.google.com/web/tools/workbox/modules) from the Workbox project, add in a push notification library, or remove some of the default caching logic. The one requirement is that you keep `self.__WB_MANIFEST` somewhere in your file, as the Workbox compilation plugin checks for this value when generating a manifest of URLs to precache. If you would prefer not to use precaching, you can assign `self.__WB_MANIFEST` to a variable that will be ignored, like:
10353

10454
```js
10555
// eslint-disable-next-line no-restricted-globals
@@ -110,67 +60,24 @@ const ignored = self.__WB_MANIFEST;
11060

11161
## Offline-First Considerations
11262

113-
If you do decide to opt-in to service worker registration, please take the
114-
following into account:
115-
116-
1. After the initial caching is done, the [service worker lifecycle](https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle)
117-
controls when updated content ends up being shown to users. In order to guard against
118-
[race conditions with lazy-loaded content](https://github.com/facebook/create-react-app/issues/3613#issuecomment-353467430),
119-
the default behavior is to conservatively keep the updated service worker in the "[waiting](https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting)"
120-
state. This means that users will end up seeing older content until they close (reloading is not
121-
enough) their existing, open tabs. See [this blog post](https://jeffy.info/2018/10/10/sw-in-c-r-a.html)
122-
for more details about this behavior.
123-
124-
1. Users aren't always familiar with offline-first web apps. It can be useful to
125-
[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)
126-
when the service worker has finished populating your caches (showing a "This web
127-
app works offline!" message) and also let them know when the service worker has
128-
fetched the latest updates that will be available the next time they load the
129-
page (showing a "New content is available once existing tabs are closed." message). Showing
130-
these messages is currently left as an exercise to the developer, but as a
131-
starting point, you can make use of the logic included in [`src/serviceWorker.js`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/src/serviceWorker.js), which
132-
demonstrates which service worker lifecycle events to listen for to detect each
133-
scenario, and which as a default, only logs appropriate messages to the
134-
JavaScript console.
135-
136-
1. Service workers [require HTTPS](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers#you_need_https),
137-
although to facilitate local testing, that policy
138-
[does not apply to `localhost`](https://stackoverflow.com/questions/34160509/options-for-testing-service-workers-via-http/34161385#34161385).
139-
If your production web server does not support HTTPS, then the service worker
140-
registration will fail, but the rest of your web app will remain functional.
141-
142-
1. The service worker is only enabled in the [production environment](deployment.md),
143-
e.g. the output of `npm run build`. It's recommended that you do not enable an
144-
offline-first service worker in a development environment, as it can lead to
145-
frustration when previously cached assets are used and do not include the latest
146-
changes you've made locally.
147-
148-
1. If you _need_ to test your offline-first service worker locally, build
149-
the application (using `npm run build`) and run a standard http server from your
150-
build directory. After running the build script, `create-react-app` will give
151-
instructions for one way to test your production build locally and the [deployment instructions](deployment.md) have
152-
instructions for using other methods. _Be sure to always use an
153-
incognito window to avoid complications with your browser cache._
154-
155-
1. By default, the generated service worker file will not intercept or cache any
156-
cross-origin traffic, like HTTP [API requests](integrating-with-an-api-backend.md),
157-
images, or embeds loaded from a different domain. Starting with Create
158-
React App 4, this can be customized, as explained above.
63+
If you do decide to opt-in to service worker registration, please take the following into account:
64+
65+
1. After the initial caching is done, the [service worker lifecycle](https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle) controls when updated content ends up being shown to users. In order to guard against [race conditions with lazy-loaded content](https://github.com/facebook/create-react-app/issues/3613#issuecomment-353467430), the default behavior is to conservatively keep the updated service worker in the "[waiting](https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting)" state. This means that users will end up seeing older content until they close (reloading is not enough) their existing, open tabs. See [this blog post](https://jeffy.info/2018/10/10/sw-in-c-r-a.html) for more details about this behavior.
66+
67+
1. Users aren't always familiar with offline-first web apps. It can be useful to [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) when the service worker has finished populating your caches (showing a "This web app works offline!" message) and also let them know when the service worker has fetched the latest updates that will be available the next time they load the page (showing a "New content is available once existing tabs are closed." message). Showing these messages is currently left as an exercise to the developer, but as a starting point, you can make use of the logic included in [`src/serviceWorker.js`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/src/serviceWorker.js), which demonstrates which service worker lifecycle events to listen for to detect each scenario, and which as a default, only logs appropriate messages to the JavaScript console.
68+
69+
1. Service workers [require HTTPS](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers#you_need_https), although to facilitate local testing, that policy [does not apply to `localhost`](https://stackoverflow.com/questions/34160509/options-for-testing-service-workers-via-http/34161385#34161385). If your production web server does not support HTTPS, then the service worker registration will fail, but the rest of your web app will remain functional.
70+
71+
1. The service worker is only enabled in the [production environment](deployment.md), e.g. the output of `npm run build`. It's recommended that you do not enable an offline-first service worker in a development environment, as it can lead to frustration when previously cached assets are used and do not include the latest changes you've made locally.
72+
73+
1. If you _need_ to test your offline-first service worker locally, build the application (using `npm run build`) and run a standard http server from your build directory. After running the build script, `create-react-app` will give instructions for one way to test your production build locally and the [deployment instructions](deployment.md) have instructions for using other methods. _Be sure to always use an incognito window to avoid complications with your browser cache._
74+
75+
1. By default, the generated service worker file will not intercept or cache any cross-origin traffic, like HTTP [API requests](integrating-with-an-api-backend.md), images, or embeds loaded from a different domain. Starting with Create React App 4, this can be customized, as explained above.
15976

16077
## Progressive Web App Metadata
16178

162-
The default configuration includes a web app manifest located at
163-
[`public/manifest.json`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/public/manifest.json), that you can customize with
164-
details specific to your web application.
165-
166-
When a user adds a web app to their homescreen using Chrome or Firefox on
167-
Android, the metadata in [`manifest.json`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/public/manifest.json) determines what
168-
icons, names, and branding colors to use when the web app is displayed.
169-
[The Web App Manifest guide](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/)
170-
provides more context about what each field means, and how your customizations
171-
will affect your users' experience.
172-
173-
Progressive web apps that have been added to the homescreen will load faster and
174-
work offline when there's an active service worker. That being said, the
175-
metadata from the web app manifest will still be used regardless of whether or
176-
not you opt-in to service worker registration.
79+
The default configuration includes a web app manifest located at [`public/manifest.json`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/public/manifest.json), that you can customize with details specific to your web application.
80+
81+
When a user adds a web app to their homescreen using Chrome or Firefox on Android, the metadata in [`manifest.json`](https://github.com/facebook/create-react-app/blob/master/packages/cra-template/template/public/manifest.json) determines what icons, names, and branding colors to use when the web app is displayed. [The Web App Manifest guide](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/) provides more context about what each field means, and how your customizations will affect your users' experience.
82+
83+
Progressive web apps that have been added to the homescreen will load faster and work offline when there's an active service worker. That being said, the metadata from the web app manifest will still be used regardless of whether or not you opt-in to service worker registration.

0 commit comments

Comments
 (0)