What does registerServiceWorker do in React JS?

user7232873 picture user7232873 · Dec 23, 2017 · Viewed 44.6k times · Source

I'm a newbie in React and I was wondering what is the purpose of registerServiceWorker() in the following code?

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

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

Answer

mostafa tourad picture mostafa tourad · Dec 23, 2017

The service worker is a web API that helps you cache your assets and other files so that when the user is offline or on slow network, he/she can still see results on the screen, as such, it helps you build a better user experience, that's what you should know about service worker's for now. It's all about adding offline capabilities to your site.

React creates a service worker for you without your configuration by default. To learn more;