[PWA] Show an Error when a POST or DELETE Fails in an Offline PWA
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 || [])
以上是关于[PWA] Show an Error when a POST or DELETE Fails in an Offline PWA的主要内容,如果未能解决你的问题,请参考以下文章
TMS Error when opening an RFC connection
Make jQuery throw error when it doesn't match an element
Getting an error when running tophat-fusion-post
[symonfy] An error occurred when executing the "'cache:clear --no-warmup'"
spark报Got an error when resolving hostNames. Falling back to /default-rack for all
IIS报错-An error occurred on the server when processing the URL. Please contact the system administrat