zoukankan      html  css  js  c++  java
  • [PWA] Show an Error when a POST or DELETE Fails in an Offline PWA

    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 || [])
  • 相关阅读:
    Myeclipse安装svn插件
    Hudson+Maven+Svn搭建持续集成环境
    svn merge和branch分析
    Linux下安装subversion1.6.5和apache2
    C语言中,为什么字符串可以赋值给字符指针变量
    Loadrunner C 编程_1
    oracle解决多表关联分组查询问题
    学习英语
    使用 JMeter 完成常用的压力测试 [转]
    Jmeter零起点学习
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10199432.html
Copyright © 2011-2022 走看看