Comments (1)
Service Worker
Service Worker 是运行在浏览器背后的独立线程,一般可以用来实现缓存功能。使用 Service Worker 的话,传输协议必须为 HTTPS。因为 Service Worker 中涉及到请求拦截,所以必须使用 HTTPS 协议来保障安全。Service Worker 的缓存与浏览器其他内建的缓存机制不同,它可以让我们自由控制缓存哪些文件、如何匹配缓存、如何读取缓存,并且缓存是持续性的。
Service Worker 实现缓存功能一般分为三个步骤:首先需要先注册 Service Worker,然后监听到 install 事件以后就可以缓存需要的文件,那么在下次用户访问的时候就可以通过拦截请求的方式查询是否存在缓存,存在缓存的话就可以直接读取缓存文件,否则就去请求数据。
当 Service Worker 没有命中缓存的时候,我们需要去调用 fetch 函数获取数据。也就是说,如果我们没有在 Service Worker 命中缓存的话,会根据缓存查找优先级去查找数据。但是不管我们是从 Memory Cache 中还是从网络请求中获取的数据,浏览器都会显示我们是从 Service Worker 中获取的内容。
相对 APP Cache
html5 提供了 App cache 来解决静态文件存储的问题,即将要缓存的静态文件声明在一个 manifest 文件清单里,但是也存在如下问题:
-
只要 manifest 文件更新,就会重新请求所有文件,实际上可能只更新了很少量文件(虽然重新请求资源会返回 304)。但也可以只更新需要更新的文件, 比如可以建立一个文件版本或者 MD5 映射,对相同版本或者MD5不再请求。
-
manifest 文件每次都会请求,当然也可以在打开 app 之前预缓存,提前下载文件或者更新 manifest 文件。
from blog.
Related Issues (20)
- 前端工程化相关 HOT 5
- Node.js 学习笔记 HOT 1
- Apollo GraphQL
- 递归和动态规划 HOT 2
- 二叉树 HOT 1
- 深入理解 async/await HOT 1
- 浏览器渲染及性能优化 HOT 4
- 实现 new 操作符 HOT 2
- 图论算法
- 搜索与回溯算法
- 从零扒一扒 promise HOT 1
- Java 学习笔记 HOT 5
- Eslint 插件
- A Recap of Frontend Development in 2019
- 关于编译和执行 HOT 2
- Js bridge & React Native通信机制
- 小程序
- 深入理解现代浏览器架构(part 1)
- 深入理解现代浏览器架构(part 2)
- 深入理解现代浏览器架构(part 3)
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 blog.