Comments (7)
ServiceWorkerRegistration
ServiceWorkerRegistration
控制ServiceWorker,如作用范围等。
注册对象是WorkerServiceGlobalScope和WindowScope沟通的桥梁。在两个上下文中都可以操作ServiceWorkerRegistration对象控制ServiceWorker。
- 通过ServiceWorkerContainer.register方法用给定的scriptURL创建或者更新一个注册对象(ServiceWorkerRegistration)。
- 浏览器会持久化已激活的注册对象。
ServiceWorkerRegistration会被持久化的,多次注册相同的SW,其对应的Registraction对象是相等的。
unregister
到底这个操作是干嘛的?试了只是导致SW的运行状态改成STOPPED,本身的状态没变,并且页面依旧被SW控制着。再试试,是可以把SW变成redundant状态的。
from note.
ServiceWorker
ServiceWorker本身很简单,没有方法,只有属性和事件监听方法。
- ServiceWorker的父对象是Worker
- terminater方法不能用
from note.
WindowScope和ServiceWorkerGlobalScope通讯
ServiceWorkerContainer和ServiceWorkerGlobalScope都可以绑定message事件,来监听来自postMessage的消息
The ServiceWorkerGlobalScope.onmessage event of the ServiceWorkerGlobalScope interface is called whenever incoming messages are received.
但是到底在监听谁调用的postMessage方法呢?
1. WindowScope向ServiceWorkerGlobalScope发送信息
通过ServiceWorker.postMssage方法,如:
// page js
navigator.serviceWorker.ready.then(function(registration) {
registration.active.postMessage('hello');
})
// sw js
self.addEventListener('message', function(event) {
event.source.postMessage(`Hi, I got you with "${event.data}"`)
})
还可以在ServiceWorkerContainer.register方法获取ServiceWorker对象。
注意:
通过ServiceWorkerGlobalScope.registration也可以获取ServiceWorker, 其postMessage方法也是向当前ServiceWorkerGlobalScope发送消息。
2. ServiceWorkerGlobalScope向WindowScope发送信息
在ServiceWorkerGlobalScope中使用WindowClient.postMessage向WindowScope发送信息。在WindowScope中通过ServiceWorkerContainer监听message事件。
ServiceWorkerGlobalScope可以有多个客户端(clients属性),所以可以实现对指定的Client发送信息也可以向多个client广播信息。
// pageJS
navigator.serviceWorker.addEventListener('message', function(event) {
console.log('Msg from sw: ' + event.data)
})
// sw js
self.addEventListener('message', function(event) {
console.log(`Msg from Client ${event.source.id}: "${event.data}"`)
event.source.postMessage(`Hi, I got you with "${event.data}"`)
})
当然了在ServiceWorkerGlobalScope中有很多场景下使用WindowClient,见clients属性, 如:
- Demo1 在fetch回调函数里使用
// sw js
self.addEventListener('fetch', event => {
self.clients.get(event.clientId).then(client => {
client && client.postMessage('Wow! SW 增长处理我发出的请求')
})
})
注意:
- ServiceWorkerContainer和ServiceWorkerGlobalScope的message事件的event对象的类型是不一样的:前者是MessageEvent后者是ExtendableMessageEvent。
from note.
Issues
- 浏览器是如何加载scriptsURL脚本的?缓存测试时怎样的?
常见的是scripts标签 - 如何评估性能指标?chrome-devtools
- 兼容性
- ServiceWorkerGlobalScope不准许同步IO?那importScripts函数呢?
参考:
from note.
SW的功能
- 代理功能(fetch, 缓存)
- 推送 ?
- 通知 ?
- 同步 ?
参考
from note.
from note.
参考
1. Google Dev搜索
1.1. Introduction to Service Worker
1.2. 服务工作线程生命周期
1.3. 服务工作线程:简介
from note.
Related Issues (20)
- Base64
- IOS App Extension HOT 1
- Monorepos: Lerna VS npm/yarn/pnpm workspace HOT 1
- UIImage vs. CIImage vs. CGImage
- NSArray, NSMutableArray HOT 1
- nil, Nil, NULL, NSNull
- IOS 开发 HOT 5
- UIViewController HOT 3
- Formik 引发的Out of Memory异常
- 浏览器禁用JS之后
- App Clips & Google Play Instant HOT 2
- RN布局 & 适配 HOT 2
- PWA HOT 1
- App内置浏览器
- 实现RN轮播(走马灯/Swiper/Carousel/Snap/Tab )组件 HOT 1
- Deferred Deep Link
- Lottie
- RN-SafeAreaView
- RN-Image & ImageBackground HOT 2
- 修改`python3`版本 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from note.