What does registerServiceWorker do in React JS?

给你一囗甜甜゛ 提交于 2019-11-27 05:16:02

问题


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();

回答1:


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;

  • Service Workers, MDN
  • Service Workers, Google Web Fundamentals
  • Simple offline site using service worker, CSS Tricks
  • Server worker: A case study, Smashing Magazine



回答2:


The service worker is to use the website data offline in case there is no network, it's like making PWA. Refer to this link PWA to know more about implementation and how to use registerServiceWorker().



来源:https://stackoverflow.com/questions/47953732/what-does-registerserviceworker-do-in-react-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!