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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:59:19  来源:igfitidea点击:

What does registerServiceWorker do in React JS?

javascriptreactjsservice-workerreact-dom

提问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,无需您进行配置。了解更多;

回答by Yogesh Devgun

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 PWAto know more about implementation and how to use registerServiceWorker().

service worker就是在没有网络的情况下离线使用网站数据,就像做PWA一样。请参阅此链接PWA以了解有关实现以及如何使用 registerServiceWorker() 的更多信息。