Skip to content

Commit 19ceec7

Browse files
jeffposnickTimer
authored andcommitted
Remove the navigateFallback behavior from the generated service worker (#3419)
* Disables navigateFallback and updates the README * Typos * Updated a URL in a comment.
1 parent 5c81891 commit 19ceec7

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

packages/react-scripts/config/webpack.config.prod.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,12 @@ module.exports = {
344344
console.log(message);
345345
},
346346
minify: true,
347-
// For unknown URLs, fallback to the index page
348-
navigateFallback: publicUrl + '/index.html',
349-
// Ignores URLs starting from /__ (useful for Firebase):
350-
// https://github.com/facebookincubator/create-react-app/issues/2237#issuecomment-302693219
351-
navigateFallbackWhitelist: [/^(?!\/__).*/],
352347
// Don't precache sourcemaps (they're large) and build asset manifest:
353348
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/],
349+
// `navigateFallback` and `navigateFallbackWhitelist` are disabled by default; see
350+
// https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#service-worker-considerations
351+
// navigateFallback: publicUrl + '/index.html',
352+
// navigateFallbackWhitelist: [/^(?!\/__).*/],
354353
}),
355354
// Moment.js is an extremely popular library that bundles large locale files
356355
// by default due to how Webpack interprets its code. This is a practical

packages/react-scripts/template/README.md

+35-12
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
8383
- [Static Server](#static-server)
8484
- [Other Solutions](#other-solutions)
8585
- [Serving Apps with Client-Side Routing](#serving-apps-with-client-side-routing)
86+
- [Service Worker Considerations](#service-worker-considerations)
8687
- [Building for Relative Paths](#building-for-relative-paths)
8788
- [Azure](#azure)
8889
- [Firebase](#firebase)
@@ -1788,8 +1789,14 @@ is integrated into production configuration,
17881789
and it will take care of generating a service worker file that will automatically
17891790
precache all of your local assets and keep them up to date as you deploy updates.
17901791
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)
1791-
for handling all requests for local assets, including the initial HTML, ensuring
1792-
that your web app is reliably fast, even on a slow or unreliable network.
1792+
for handling all requests for local assets, including
1793+
[navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
1794+
for `/` and `/index.html`, ensuring that your web app is consistently fast, even
1795+
on a slow or unreliable network.
1796+
1797+
>Note: If you are using the `pushState` history API and want to enable
1798+
cache-first navigations for URLs other than `/` and `/index.html`, please
1799+
[follow these steps](#service-worker-considerations).
17931800
17941801
### Opting Out of Caching
17951802
@@ -1992,21 +1999,37 @@ If you’re using [Apache Tomcat](http://tomcat.apache.org/), you need to follow
19921999
19932000
Now requests to `/todos/42` will be handled correctly both in development and in production.
19942001
1995-
On a production build, and in a browser that supports [service workers](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers),
1996-
the service worker will automatically handle all navigation requests, like for
1997-
`/todos/42`, by serving the cached copy of your `index.html`. This
1998-
service worker navigation routing can be configured or disabled by
1999-
[`eject`ing](#npm-run-eject) and then modifying the
2000-
[`navigateFallback`](https://github.com/GoogleChrome/sw-precache#navigatefallback-string)
2001-
and [`navigateFallbackWhitelist`](https://github.com/GoogleChrome/sw-precache#navigatefallbackwhitelist-arrayregexp)
2002-
options of the `SWPreachePlugin` [configuration](../config/webpack.config.prod.js).
2003-
2004-
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:
2002+
When users install your app to the homescreen of their device the default
2003+
configuration will make a shortcut to `/index.html`. This may not work for
2004+
client-side routers which expect the app to be served from `/`. Edit the web app
2005+
manifest at [`public/manifest.json`](public/manifest.json) and change
2006+
`start_url` to match the required URL scheme, for example:
20052007
20062008
```js
20072009
"start_url": ".",
20082010
```
20092011
2012+
### Service Worker Considerations
2013+
2014+
[Navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
2015+
for URLs like `/todos/42` will not be intercepted by the
2016+
[service worker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers)
2017+
created by the production build. Navigations for those URLs will always
2018+
require a network connection, as opposed to navigations for `/` and
2019+
`/index.html`, both of which will be served from the cache by the service worker
2020+
and work without requiring a network connection.
2021+
2022+
If you are using the `pushState` history API and would like to enable service
2023+
worker support for navigations to URLs like `/todos/42`, you need to
2024+
[`npm eject`](#npm-run-eject) and enable the
2025+
[`navigateFallback`](https://github.com/GoogleChrome/sw-precache#navigatefallback-string)
2026+
and [`navigateFallbackWhitelist`](https://github.com/GoogleChrome/sw-precache#navigatefallbackwhitelist-arrayregexp)
2027+
options of the `SWPreachePlugin` [configuration](../config/webpack.config.prod.js).
2028+
2029+
>Note: This is a [change in default behavior](https://github.com/facebookincubator/create-react-app/issues/3248),
2030+
as earlier versions of `create-react-app` shipping with `navigateFallback`
2031+
enabled by default.
2032+
20102033
### Building for Relative Paths
20112034
20122035
By default, Create React App produces a build assuming your app is hosted at the server root.<br>

0 commit comments

Comments
 (0)