[PWA] 4. Hijacking Request
We want to do offline first, the first thing we need to do is we should able to catch the browser request and return our cache data from serice worker. So user won't go to the real server to fetch data. So let's see how to do that:
self.addEventListener('fetch', function(event) {
event.respondWith(
new Response('Hello world');
)
});
We use 'respondWith() and Response()' to provdie a custom cache response.
After you reopen the browser, you will see 'Hello World'.
What if we want to show html instead of pain text? The 'Response()' take a second param which can set header.
self.addEventListener('fetch', function(event) {
event.respondWith(
new Response('Hello <b class="a-winner-is-me">world</b>', {
headers: {'Content-Type': 'text/html'}
});
)
});

[PWA] 4. Hijacking Request的更多相关文章
- [PWA] 5. Hijacking one type of request
Previously we saw how to Hijacking all the reqest, but this is not useful. So now we want to see how ...
- [PWA] 6. Hijacking response
For example, if the url is not match to any API endpoint, we want to return 404 error message. So fi ...
- [Rails] 从 Request 到 Response(1)
本文翻译自:Rails from Request to Response 系列:个人选择了自己感兴趣的部分进行翻译,需要阅读原文的同学请戳前面的链接. 第一部分 导言(Introduction) 服务 ...
- 轻松把你的项目升级到PWA
什么是PWA PWA(Progressive Web Apps,渐进式网页应用)是Google在2015年推出的项目,致力于通过web app获得类似native app体验的网站. 优点 1.无需客 ...
- 前端应该了解的PWA
一.传统web 应用 当前web应用在移动时代并没有达到其在桌面设备上流行的程度,下面有张图来对比与原生应用之间的差别. 究其原因,无外乎下面不可避免的几点: 移动设备网络限制-不可忽略的加载时间 w ...
- 渐进式Web应用(PWA)入门教程(下)
上篇文章我们对渐进式Web应用(PWA)做了一些基本的介绍. 渐进式Web应用(PWA)入门教程(上) 在这一节中,我们将介绍PWA的原理是什么,它是如何开始工作的. 第一步:使用HTTPS 渐进式W ...
- PWA的探索与应用
本文由云+社区发表 PWA(Progressive Web App)起源背景 传统的Web网页存在以下几个问题: 进入一个页面必须要记住它的url或者加入书签,入口不便捷: 没网络就没响应,不具备离线 ...
- 干货 | 10分钟玩转PWA
关于PWA PWA(Progressive Web App), 即渐进式web应用.PWA本质上是web应用,目的是通过多项新技术,在安全.性能.体验等方面给用户原生应用的体验.而且无需像原生应用那样 ...
- H5 PWA技术以及小demo
H5 PWA技术 1.原生app优缺点 a.体验好.下载到手机上入口方便 b.开发成本高(ios和安卓) c.软件上线需要审核 d.版本更新需要将新版本上传到不同的应用商店 e.使用前需下载 2.we ...
随机推荐
- stat 的名字接口
File::stat - stat 的名字接口 名字为:dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksiz ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- 入门3:PHP环境开发搭建(windows)
一.环境需要 硬件环境(最低配置): 双核CPU 8G内存 操作系统环境: Windows(64位)7+ Mac OS X 10.10+ Linux 64位(推荐Ubuntu 14 LTS) /**拓 ...
- CodeFirst中DB保存时报错:对一个或多个实体的验证失败。
错误提示如下: 开始以为有字段可能没有添加数据,可是检查了很久,仍然没有任何头绪. 后使用DbEntityValidationException进行调试,问题才得以解决
- 使用jQuery UI方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- study note--(Education)
some good temoplates: A child's education has never been about learning information and basic skills ...
- Rss web 工具 大对比
今天终于神受不了 feedly的链接死掉了..有时候挂代理就好了..但是麻烦. 于是: AOL reader Digg reader feedly 对比下.使用了一天 1.feedly 优: 效果最 ...
- [POJ 1742] Coins 【DP】
题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 D ...
- Struts2 实现分页
1.转自:http://www.cnblogs.com/shiyangxt/archive/2008/11/04/1316737.html环境:MyEclipse6.5+Mysql5+struts2. ...
- Ehcache中配置详解
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...