[PWA] sw-precache
Link to CodeLab
In this codelab, we'll retrace those steps but this time we'll use a tool called sw-precache to add offline functionality with only six lines of code. It's never been easier to add service worker support to an existing app, and we'll show you how in this codelab.
Learning:
- What the sw-precache tool is and how it can help you be more productive
- How to add a basic service worker to an existing project using sw-precache
Install:
git clone https://github.com/GoogleChrome/airhorn.git
cd airhorn
git checkout code-lab
npm install
npm install --save-dev sw-precache
cd app
python -m SimpleHTTPServer
Gulp:
// Generate sw.js
gulp.task('generate-service-worker', function(callback) {
var path = require('path');
var swPrecache = require('sw-precache');
var rootDir = 'app'; swPrecache.write(path.join(rootDir, 'sw.js'), {
staticFileGlobs: [rootDir + '/**/*.{js,html,css,png,jpg,gif,mp3}'],
stripPrefix: rootDir
}, callback);
});
Run:
gulp generate-service-worker
Then in app folder, you should see sw.js file generated.
HTML: inisde index.html:
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(function() {
console.log("Service Worker Registered");
});
}
</script>
Verify Your App:
cd app
python -m SimpleHTTPServer
Open chrome://serviceworker-internals/ in Chrome. This will show you a list of all the registered service workers, which you can use to verify your service worker has indeed properly registered.
Kill the serve and reload the page, everything should work as the same.
[PWA] sw-precache的更多相关文章
- Progressive Web Applications
Progressive Web Applications take advantage of new technologies to bring the best of mobile sites an ...
- 调研pwa和sw
概述 处于好奇,最近我调研了一下pwa和service worker,有些新的,记录下来,供以后开发时参考,相信对其他人也有用.pwa主要是通过service worker实现的,它主要包括桌面图标, ...
- [PWA] 8.Unobtrusive update: Delete old cache and only keep one, hard refresh to let new SW to take control
So once you modify the code, service worker will auto create a new one and it won't take control ove ...
- 网站PWA升级
前面的话 渐进式网络应用 ( Progressive Web Apps ),即我们所熟知的 PWA,是 Google 提出的用前沿的 Web 技术为网页提供 App 般使用体验的一系列方案.PWA 本 ...
- 重识 PWA 进阶到 workbox3
看到PWA,似曾相识,但又感觉很模糊,于是乎,又重新翻阅文档,学习了一遍,顺便把相关知识学习了一下,比如service worker,workbox3. PWA 概念: 全称:Progressive ...
- 使用react全家桶制作博客后台管理系统 网站PWA升级 移动端常见问题处理 循序渐进学.Net Core Web Api开发系列【4】:前端访问WebApi [Abp 源码分析]四、模块配置 [Abp 源码分析]三、依赖注入
使用react全家桶制作博客后台管理系统 前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基 ...
- PWA之serviceWorker应用
1.serviceWorker介绍service worker是一段运行在浏览器后台的JavaScript脚本,在页面中注册并安装成功后,它可以拦截和处理网络请求,实现缓存资源并可在离线时响应用户的请 ...
- PWA 推送实践
PWA 推送实践 最近公司内录任务的系统总是忘记录任务,而那个系统又没有通知,所以想要实现一个浏览器的通知功能,免得自己忘记录入任务. 前端实现通知的几种方式 想要实现通知,我们就需要有个客户端,对于 ...
- 轻松把你的项目升级到PWA
什么是PWA PWA(Progressive Web Apps,渐进式网页应用)是Google在2015年推出的项目,致力于通过web app获得类似native app体验的网站. 优点 1.无需客 ...
- PWA学习心得
PWA学习心得 一.什么是PWA Progressive Web App , (渐进式增强 WEB 应用) 简称 PWA ,是提升WebApp的体验的一种新方法,能给用户原生应用的体验. PWA ...
随机推荐
- muduo网络库学习笔记(10):定时器的实现
传统的Reactor通过控制select和poll的等待时间来实现定时,而现在在Linux中有了timerfd,我们可以用和处理IO事件相同的方式来处理定时,代码的一致性更好. 一.为什么选择time ...
- Cinder-1 TinderBox
Cinder:http://libcinder.org/,当前版本是0.8.5,代码托管位置:https://github.com/cinder/Cinder.git 下载Cinder之后,其目录结构 ...
- double类型字符串转换成一个纯数字字符串和一个小数点位数的c++代码
今天工作中遇到一个要不一个double型的字符串转换成一个纯字数字符串和一个标志这个数字字符串的小数点有几位的int类型 例如:“23.123”--->“23123” + 3 比较简单.就是 ...
- main函数的参数
一.main的参数 形式:int main(int argc,char *argv[]) 参数argc.argv可以被看做是main函数的形参,argc是整型变量,代表的是参数的个数:argv是指向字 ...
- service httpd restart失败解决方法(小记)
1.首先查看错误日志 /var/log/message看看具体问题.如果一时难以判定直接使用下面一招 2.直接中断http服务,killall -9 httpd, 然后重启http,service ...
- Application(全局对象,ViewState
Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApplication类对象Application被自动创建,直到没有一个网页被访问时结束, ...
- Codeforces Round #154 (Div. 2) : B
一个很简单的题: 方法一: 二分. 代码: #include<cstdio> #include<algorithm> #define maxn 100005 using nam ...
- 用 Webgoat 撬动地球,看安全测试的引路石!
测试工程师是很多人迈进软件行业的起点.从负责很小的局部到把握整个产品的质量,每个人花费的时间长短不一--从功能到性能.可用性到容错性.从兼容性到扩展性.稳定性到健壮性--方方面面逐渐做广做深. 不过, ...
- 【 UVALive - 5095】Transportation(费用流)
Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...
- Haskell函数的语法
本章讲的就是 Haskell 那套独特的语法结构,先从模式匹配开始.模式匹配通过检查数据的特定结构来检查其是否匹配,并按模式从中取得数据. 在定义函数时,你可以为不同的模式分别定义函数本身,这就让代码 ...