Skip to content

Commit b6aebb9

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

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)
@@ -1791,8 +1792,14 @@ is integrated into production configuration,
17911792
and it will take care of generating a service worker file that will automatically
17921793
precache all of your local assets and keep them up to date as you deploy updates.
17931794
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)
1794-
for handling all requests for local assets, including the initial HTML, ensuring
1795-
that your web app is reliably fast, even on a slow or unreliable network.
1795+
for handling all requests for local assets, including
1796+
[navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
1797+
for `/` and `/index.html`, ensuring that your web app is consistently fast, even
1798+
on a slow or unreliable network.
1799+
1800+
>Note: If you are using the `pushState` history API and want to enable
1801+
cache-first navigations for URLs other than `/` and `/index.html`, please
1802+
[follow these steps](#service-worker-considerations).
17961803
17971804
### Opting Out of Caching
17981805
@@ -1995,21 +2002,37 @@ If you’re using [Apache Tomcat](http://tomcat.apache.org/), you need to follow
19952002
19962003
Now requests to `/todos/42` will be handled correctly both in development and in production.
19972004
1998-
On a production build, and in a browser that supports [service workers](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers),
1999-
the service worker will automatically handle all navigation requests, like for
2000-
`/todos/42`, by serving the cached copy of your `index.html`. This
2001-
service worker navigation routing can be configured or disabled by
2002-
[`eject`ing](#npm-run-eject) and then modifying the
2003-
[`navigateFallback`](https://github.com/GoogleChrome/sw-precache#navigatefallback-string)
2004-
and [`navigateFallbackWhitelist`](https://github.com/GoogleChrome/sw-precache#navigatefallbackwhitelist-arrayregexp)
2005-
options of the `SWPreachePlugin` [configuration](../config/webpack.config.prod.js).
2006-
2007-
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:
2005+
When users install your app to the homescreen of their device the default
2006+
configuration will make a shortcut to `/index.html`. This may not work for
2007+
client-side routers which expect the app to be served from `/`. Edit the web app
2008+
manifest at [`public/manifest.json`](public/manifest.json) and change
2009+
`start_url` to match the required URL scheme, for example:
20082010
20092011
```js
20102012
"start_url": ".",
20112013
```
20122014
2015+
### Service Worker Considerations
2016+
2017+
[Navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
2018+
for URLs like `/todos/42` will not be intercepted by the
2019+
[service worker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers)
2020+
created by the production build. Navigations for those URLs will always
2021+
require a network connection, as opposed to navigations for `/` and
2022+
`/index.html`, both of which will be served from the cache by the service worker
2023+
and work without requiring a network connection.
2024+
2025+
If you are using the `pushState` history API and would like to enable service
2026+
worker support for navigations to URLs like `/todos/42`, you need to
2027+
[`npm eject`](#npm-run-eject) and enable the
2028+
[`navigateFallback`](https://github.com/GoogleChrome/sw-precache#navigatefallback-string)
2029+
and [`navigateFallbackWhitelist`](https://github.com/GoogleChrome/sw-precache#navigatefallbackwhitelist-arrayregexp)
2030+
options of the `SWPreachePlugin` [configuration](../config/webpack.config.prod.js).
2031+
2032+
>Note: This is a [change in default behavior](https://github.com/facebookincubator/create-react-app/issues/3248),
2033+
as earlier versions of `create-react-app` shipping with `navigateFallback`
2034+
enabled by default.
2035+
20132036
### Building for Relative Paths
20142037
20152038
By default, Create React App produces a build assuming your app is hosted at the server root.<br>

0 commit comments

Comments
 (0)