[PWA] 1. Intro to Service worker

时间:2022-12-30 10:30:36

Service worker stays between our browser and noetwork requests. It can help to fetch data from cache and cache the data from Internet.

[PWA] 1. Intro to Service worker

To get our service worker, we need to :

  1. Register the service worker.

Service worker in our code is just a javascirpt file.

Once the page loaded, we want to register our serivce worker:

IndexController.prototype._registerServiceWorker = function() {
// TODO: register service worker
if(!navigator.serviceWorker) return; // Check whether service worker is available
navigator.serviceWorker.register('/sw.js') // register the service worker, locate in ../sw/index.js
.then( (reg) => {
console.log("SW registered"); // success
})
.catch( (err) => {
console.log("SW faild"); // Wrong
})
};

In register() function, it can take second param, which is scope, for example:

  navigator.serviceWorker.register('/sw.js', {
scope: '/foo/'
})
.then( (reg) => {
console.log("SW registered");
})
.catch( (err) => {
console.log("SW faild");
})

SO service worker will available only in the '/foo/' scope and its child scope, such as: '/foo/bar', but NOT '/foo', so the ending '/' is very important. Normally, we don't need to add scope, we want our service worker listeren to the root scope, so it is available to all the child components.

2. Our service worker file:

// sw.js

self.addEventListener('fetch', function(event) {
console.log(event.request);
});

Once you fetch the page seond time (first time service work did cache stuff), second time you will see in the console, there are lots of requests log out.

[PWA] 1. Intro to Service worker


Of course, service worker have other event listeners:

self.addEventListener('install', function(event) {
// ..
}); self.addEventListener('activate', function(event) {
// ..
}); self.addEventListener('fetch', function(event) {
console.log(event.request);
});

[Notice] Service worker only works for HTTPS and localhost.

Reference: Link

[PWA] 1. Intro to Service worker的更多相关文章

  1. [PWA] 9. Service worker registerion && service work's props, methods and listeners

    In some rare cases, you need to ask user to refresh the browsser to update the version. Maybe becaus ...

  2. [PWA] 2. Service worker life cycle

    Once serive worker is registered, the first time we go to the app, we cannot see the logs from servc ...

  3. [PWA] Show Notifications when a Service Worker is Installed or Updated

    Service Workers get installed and activated in the background, but until we reload the page they don ...

  4. PWA - service worker - Workbox(未完)

    Get Started(开始) 只有get请求才能cache缓存吗? Create and Register a Service Worker File(创建和注册 Service Worker) B ...

  5. Service Worker和HTTP缓存

    很多人,包括我自己,初看Service Worker多一个Cache Storage的时候,就感觉跟HTTP长缓存没什么区别. 例如大家讲的最多的Service Worker能让网页离线使用,但熟悉H ...

  6. Service Worker

    Service Worker 随着前端快速发展,应用的性能已经变得至关重要,关于这一点大佬做了很多统计.你可以去看看. 如何降低一个页面的网络请求成本从而缩短页面加载资源的时间并降低用户可感知的延时是 ...

  7. 转《service worker在移动端H5项目的应用》

    1. PWA和Service Worker的关系 PWA (Progressive Web Apps) 不是一项技术,也不是一个框架,我们可以把她理解为一种模式,一种通过应用一些技术将 Web App ...

  8. 浏览器缓存和Service Worker

    浏览器缓存和Service Worker @billshooting 2018-05-06 字数 6175 Follow me on Github 标签: BOM 1. 传统的HTTP浏览器缓存策略 ...

  9. [Angular] Service Worker Version Management

    If our PWA application has a new version including some fixes and new features. By default, when you ...

随机推荐

  1. h5手机端下拉选择城市

    <!doctype html><html>    <head>            <meta http-equiv="Content-Type& ...

  2. 使用Spire&period;Barcode程序库生成二维码

    使用Spire.Barcode程序库生成二维码 某天浏览网页发现了一个二维码的程序库.它的描述说他可以扫描二维码图像.我很感兴趣,想试试他是不是会有用.所以我就用了些方法扫描二维码图像来测试一下.结果 ...

  3. PHP 中 define&lpar;&rpar; 和 const 定义常量时的区别

    自 PHP 5.3.0 起,有两种方式定义常量,使用 const 关键字或者 define() 函数:   1 2 const FOO = 'BAR'; define('FOO', 'BAR'); 这 ...

  4. C语言编写的简单的电话本管理系统

    #include<stdio.h> #include <string.h> #include<stdlib.h> ; struct person {]; ]; ]; ...

  5. Web利器---fidder使用

    fiddler工具,主要看中其三点优势:1.功能强大,其他工具有的功能它也有,其他工具没有的功能它也有,支持http,https,ftp等协议:2.完全免费,长期免费.3.所有的浏览器可以使用,所有的 ...

  6. hdu 2393&colon;Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. Nginx--&gt&semi;进阶--&gt&semi;Module--&gt&semi;ngx&lowbar;http&lowbar;stub&lowbar;status&lowbar;module

    一.模块介绍 The ngx_http_stub_status_module module provides access to basic status information. This modu ...

  8. CF 1047 C&period; Enlarge GCD

    传送门 [http://codeforces.com/contest/1047/problem/C] 题意 给你n个数,移除最少的数字使剩下的数字GCD大于初始GCD 思路 需要一点暴力的技巧,先求出 ...

  9. Android-加载图片避免OOM

    http://blog.csdn.net/guolin_blog/article/details/9316683 高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有 ...

  10. OpenCV——RGB和HSV颜色空间

    RGB颜色空间 在RGB中,一幅图像有三个独立的图像平面或通道组成:红,绿,蓝(以及第四个通道透明度). RGB颜色表 资料:网络  ◇  编制:王践舜 RGB(255,23,140)是光的三原色,也 ...