What is service worker in react js

喜欢而已 提交于 2019-12-03 04:41:11

问题


When creating a react app, service worker is invoked by default. Why service worker is used? What is the reason for default invoking?


回答1:


You may not need a service worker for your application. If you are creating a project with create-react-app it is invoked by default

Service workers are well explained in this article. To Summarise from it

A service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don't need a web page or user interaction. Today, they already include features like push notifications and background sync and have ability to intercept and handle network requests, including programmatically managing a cache of responses.

In the future, service workers might support other things like periodic sync or geofencing.

According to this PR to create-react-app

Service workers are introduced with create-react-app via SWPrecacheWebpackPlugin.

Using a server worker with a cache-first strategy offers performance advantages, since the network is no longer a bottleneck for fulfilling navigation requests. It does mean, however, that developers (and users) will only see deployed updates on the "N+1" visit to a page, since previously cached resources are updated in the background.

The call to register service worker is enabled by default in new apps but you can always remove it and then you’re back to regular behaviour.



来源:https://stackoverflow.com/questions/49314386/what-is-service-worker-in-react-js

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