3

When you bootstrap your React app with create-react-app there is a file named registerServiceWorker.js which is imported and used in index.js as:

import React from 'react'
import ReactDOM from 'react-dom'
import App from 'App'
import registerServiceWorker from 'registerServiceWorker'

ReactDOM.render(<App />, document.getElementById('root'))
registerServiceWorker()

The official create-react-app documentation suggests to keep this service worker for production and says:

By default, it also includes a service worker so that your app loads from local cache on future visits.

An stackoverflow question also suggests the same.

But then there is this medium article which says not to use it with the default configuration:

Turn off service workers

If you’ve used something like create-react-app to bootstrap your project, you’ll want to turn off the built-in service worker if you haven’t specifically integrated it to work with your app. While usually harmless, it can cause some issues, so it’s best to just get rid of it up front

As I can't configure the service worker (new to React), should I use registerServiceWorker in production or not?

Also if my React app is an interactive authentication based app (which somehow makes it useless offline) do I need it at all?

Community
  • 1
  • 1
Ahmad Maleki
  • 1,415
  • 3
  • 21
  • 35

1 Answers1

0

The registerServiceWorker is a JavaScript file used to cache certain assets of your application. This doesn't just create offline availability, but also increases the performance of your site. I'm not sure of what problems would come about if you left it in (probably none) but if your site was that complex, you may want to consider looking into creating a custom service worker.

So no, the registerServiceWorker isn't NECESSARY if your app is useless offline, though it'll affect performance. Regardless, my advice would be to register a response code (unless it already returns 503) and put some sort of landing page that tells the user they need an internet connection, if you didn't already.

David S
  • 383
  • 5
  • 6