乐闻世界logo
搜索文章和话题

What is service worker in react js?

5 个月前提问
3 个月前修改
浏览次数89

4个答案

1
2
3
4

Service Worker 在 React JS 中是一个运行在浏览器背后的脚本,它不依赖于网页而独立工作,能够为应用提供不依赖网络的特性,比如离线内容的访问、背景同步和推送通知等。它相当于一个位于浏览器与网络之间的代理,可以截获和处理网络请求,以及根据需求管理缓存。

Service Worker 在 React 应用中的一个典型用途是创建渐进式网络应用(PWA)。PWA 是一种通过网络技术构建的应用程序,它可以提供类似原生应用的用户体验。通过使用 Service Worker,React 应用可以在用户的设备上缓存应用的核心文件,这样即使在没有网络连接的情况下,用户也能加载应用的基本界面和功能。

一个实际的例子是,当开发者使用 create-react-app 工具新建一个 React 项目时,生成的模板项目中自带了 Service Worker 的配置。这个配置默认是不启用的,但是开发者可以选择启用它,并根据实际需要对其进行配置,从而让应用具备 PWA 的特性。

启用 Service Worker 后,当用户首次访问 React 应用时,Service Worker 被安装并开始缓存应用的资源,如 HTML、CSS、JavaScript 文件和图片等。当用户再次访问该应用时,即使在离线状态下,Service Worker 也可以通过拦截请求并提供缓存的资源来加载应用。

Service Worker 还允许开发者通过编程方式精细控制缓存策略,例如决定哪些资源需要缓存、何时更新缓存、如何响应资源请求等。通过这种方式,可以优化应用的性能并提高用户体验。

2024年6月29日 12:07 回复

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.

2024年6月29日 12:07 回复

In simple and plain words, it’s a script that browser runs in the background and has whatsoever no relation with web pages or the DOM, and provide out of the box features. It also helps you cache your assets and other files so that when the user is offline or on slow network.

Some of these features are proxying network requests, push notifications and background sync. Service workers ensure that the user has a rich offline experience.

You can think of the service worker as someone who sits between the client and server and all the requests that are made to the server pass through the service worker. Basically, a middle man. Since all the request pass through the service worker, it is capable to intercept these requests on the fly.

enter image description here

2024年6月29日 12:07 回复

I'd like to add 2 important considerations about Service Workers to take into account:

  1. Service Workers require HTTPS. But to enable local testing, this restriction doesn't apply to localhost. This is for security reasons as a Service Worker acts like a man in the middle between the web application and the server.

  2. With Create React App Service Worker is only enabled in the production environment, for example when running npm run build.

Service Worker is here to help developing a Progressive Web App. A good resource about it in the context of Create React App can be found in their website here.

2024年6月29日 12:07 回复

你的答案