mirror of
https://github.com/Brandon-Rozek/website-theme.git
synced 2024-10-22 15:02:08 -04:00
Offline page URL fix
This commit is contained in:
parent
5ae3c63834
commit
31dfeaaa0b
1 changed files with 14 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
let version = 'v1::';
|
let version = 'v1::';
|
||||||
let cacheName = 'website';
|
let cacheName = 'website';
|
||||||
let offlinePage = '/offline';
|
let offlinePage = '/offline/';
|
||||||
let offlineFundamentals = [offlinePage, '/'];
|
let offlineFundamentals = [offlinePage, '/'];
|
||||||
let maxItems = 100;
|
let maxItems = 100;
|
||||||
|
|
||||||
|
@ -89,21 +89,25 @@ if (registration.navigationPreload) {
|
||||||
self.addEventListener('fetch', function (event) {
|
self.addEventListener('fetch', function (event) {
|
||||||
const request = event.request;
|
const request = event.request;
|
||||||
|
|
||||||
// Always fetch non-GET requests from the network
|
|
||||||
if (request.method !== 'GET') {
|
|
||||||
event.respondWith(
|
|
||||||
fetch(request)
|
|
||||||
.catch(() => caches.match(offlinePage))
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let isRequestType = function(name) {
|
let isRequestType = function(name) {
|
||||||
return request.headers
|
return request.headers
|
||||||
.get('Accept')
|
.get('Accept')
|
||||||
.includes(name);
|
.includes(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Always fetch non-GET requests from the network
|
||||||
|
if (request.method !== 'GET') {
|
||||||
|
// Present offline page when failed to
|
||||||
|
// fetch a HTML page
|
||||||
|
if (isRequestType('text/html')) {
|
||||||
|
event.respondWith(
|
||||||
|
fetch(request)
|
||||||
|
.catch(() => caches.match(offlinePage))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Network-first Approach
|
// Network-first Approach
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
// Attepmt to grab the latest copy from the network
|
// Attepmt to grab the latest copy from the network
|
||||||
|
|
Loading…
Reference in a new issue