Javascript registerServiceWorker 在 React JS 中做了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47953732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does registerServiceWorker do in React JS?
提问by mostafa tourad
I'm a newbie in React and I was wondering what is the purpose of registerServiceWorker() in the following code?
我是 React 的新手,我想知道以下代码中 registerServiceWorker() 的目的是什么?
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
回答by mostafa tourad
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.
Service Worker 是一个 Web API,可以帮助您缓存您的资产和其他文件,以便当用户离线或网络缓慢时,他/她仍然可以在屏幕上看到结果,因此,它可以帮助您构建更好的用户体验,这就是你现在应该了解的关于 service worker 的内容。这一切都是为了向您的站点添加离线功能。
React creates a service worker for you without your configuration by default. To learn more;
默认情况下,React 为您创建一个 service worker,无需您进行配置。了解更多;

