We're storing JSON data in the cache - but that only applies to HTTP GET requests - and not to POST or DELETE requests.

We'll add a fetch event listener to the service worker, where we can intercept all fetch requests in our app. We'll forward them to the server - but if they fail, then we'll return an error back to the app.

In the app, we can detect that error, and respond by showing an alert that the functionality isn't available in offline mode.

console.log("IN sw.js")

workbox.skipWaiting();
workbox.clientsClaim(); workbox.routing.registerRoute(
new RegExp('https:.*min\.(css|js)'),
workbox.strategies.staleWhileRevalidate({
cacheName: 'cdn-cache'
})
) workbox.routing.registerRoute(
new RegExp('(http|https|localhost)://.*:4567.*\.json'),
workbox.strategies.networkFirst()
) // Handle the POST and DELETE requests by SW
self.addEventListener('fetch', event => {
if (event.request.method === "POST" || event.request.method === "DELETE") {
event.respondWith (
fetch(event.request).catch(err => {
return new Response(
JSON.stringify({error: "This action disabled while app is offline"}),
{headers: {
'Content-Type': 'application/json'
}}
)
})
)
}
}) workbox.precaching.precacheAndRoute(self.__precacheManifest || [])

[PWA] Show an Error when a POST or DELETE Fails in an Offline PWA的更多相关文章

  1. [转]Syntax error on token "Invalid Character", delete this token 的解决

    原文  http://blog.csdn.net/actsai/article/details/24256987 主题 Eclipse Unicode Java eclipse 中遇到了Syntax ...

  2. Eclipse 中 Syntax error on token "Invalid Character", delete this token 的解决

    eclipse中遇到了Syntax error on token "Invalid Character", delete this token(令牌“无效字符”上的语法错误,删除此 ...

  3. PWA学习心得

    PWA学习心得 一.什么是PWA Progressive  Web  App , (渐进式增强 WEB 应用) 简称 PWA ,是提升WebApp的体验的一种新方法,能给用户原生应用的体验. PWA ...

  4. 重识 PWA 进阶到 workbox3

    看到PWA,似曾相识,但又感觉很模糊,于是乎,又重新翻阅文档,学习了一遍,顺便把相关知识学习了一下,比如service worker,workbox3. PWA 概念: 全称:Progressive ...

  5. PWA入门:手把手教你制作一个PWA应用

    摘要: PWA图文教程 原文:PWA入门:手把手教你制作一个PWA应用 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 简介 Web前端的同学是否想过学习app开发,以弥补自 ...

  6. PWA渐进式web应用

    PWA(Progressive Web App)是一种理念,使用多种技术来增强web app的功能,可以让网站的体验变得更好,能够模拟一些原生功能,比如通知推送.在移动端利用标准化框架,让网页应用呈现 ...

  7. 前端性能和加载体验优化实践(附:PWA、离线包、内存优化、预渲染)

    一.背景:页面为何会卡? 1.1 等待时间长(性能) 项目本身包/第三方脚本比较大. JavaScript 执行阻塞页面加载. 图片体积大且多. 特别是对于首屏资源加载中的白屏时间,用户等待的时间就越 ...

  8. uafxcwd.lib(afxmem.obj) : error LNK2005 解决方法

    项目为非MFC,但要用到MFC的库,使用过程中遇到的问题总结一下 uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl opera ...

  9. error LNK2005 new,delete 等已经在LIBCMT.lib(delete.obj) 中定义 错误修正

    http://blog.csdn.net/funnyskyf/article/details/5938597 1>uafxcw.lib(afxmem.obj) : error LNK2005: ...

随机推荐

  1. jquery使用--常见前端效果实现

    一.选中左右侧内容到另一侧:选中左侧内容到右侧,选中右侧内容到左侧 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/ ...

  2. Runtime.getRuntime().exec方法

    Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...

  3. 在AppCode中的razor调用HtmlHelper方法和UrlHelper方法

    原文发布时间为:2011-05-17 -- 来源于本人的百度文章 [由搬家工具导入] 可以写一个帮助类,如下 using System.Web.WebPages;using System.Web.Mv ...

  4. SSM+redis整合(mybatis整合redis做二级缓存)

    SSM:是Spring+Struts+Mybatis ,另外还使用了PageHelper 前言: 这里主要是利用redis去做mybatis的二级缓存,mybaits映射文件中所有的select都会刷 ...

  5. centos 解决:Another app is currently holding the yum lock; waiting for it to exit

    centos执行yum时出现错误: Loaded plugins: fastestmirror, refresh-packagekit, security Existing lock /var/run ...

  6. 【QC】安装

    QC不支持win8 1. 开启win8自带的IIS服务. 在控制面板-程序-启用或关闭Windows功能-Internet Information Service

  7. CI中SESSION的用法及其注意

    销毁 Session要清除当前 session: $this->session->sess_destroy(); 注意: 此函数应该是最后被调用的.即使闪出变量已不再有效.如果你只想让某几 ...

  8. javascript原型理解一种

    http://www.jianshu.com/p/15ac7393bc1f 这个系列值得好好学习的.. // 声明构造函数 function Person(name, age) { this.name ...

  9. 牛客网 牛客练习赛7 A.骰子的游戏

    A.骰⼦的游戏 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 在Alice和Bob面前的是两个骰 ...

  10. kibana-metric

    1. Visualize 新建图形 2. 选择图形类型 3. 选择索引 4. 设置metric参数 4.1 count 4.2 unique count 5. 保存图形