Added Service Worker for offline caching

Removed Google Analytics script.
This commit is contained in:
Victor Westerlund 2020-12-14 08:01:37 +01:00
parent a4eae3ec05
commit a2f002dcad
2 changed files with 9 additions and 13 deletions

View file

@ -1,13 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5PBVM28');</script>
<!-- End Google Tag Manager -->
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Full-stack web developer from Stockholm, Sweden."> <meta name="description" content="Full-stack web developer from Stockholm, Sweden.">
@ -17,9 +10,6 @@
<title>Victor Westerlund</title> <title>Victor Westerlund</title>
</head> </head>
<body> <body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5PBVM28" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<main> <main>
<div id="intro"> <div id="intro">
<div class="inner"> <div class="inner">

View file

@ -9,6 +9,7 @@ self.addEventListener("install", event => {
event.waitUntil( event.waitUntil(
caches.open(`content-${version}`).then(cache => cache.addAll([ caches.open(`content-${version}`).then(cache => cache.addAll([
root, root,
root + "offline.txt",
root + "assets/css/style.css", root + "assets/css/style.css",
root + "assets/img/favicon.png", root + "assets/img/favicon.png",
root + "assets/js/script.js", root + "assets/js/script.js",
@ -35,7 +36,7 @@ self.addEventListener("activate", event => {
}); });
// Fetch and save content to bucket // Fetch and save content to bucket
function fetchContent(event) { async function fetchContent(event) {
const networkFetch = fetch(event.request); const networkFetch = fetch(event.request);
event.waitUntil( event.waitUntil(
@ -45,7 +46,8 @@ function fetchContent(event) {
}) })
); );
return caches.match(event.request).then(response => response || networkFetch); const response = await caches.match(event.request);
return response || networkFetch;
} }
self.addEventListener("fetch", event => { self.addEventListener("fetch", event => {
@ -61,7 +63,11 @@ self.addEventListener("fetch", event => {
// Get pattern.gif from generator. Fallback to cache on failure // Get pattern.gif from generator. Fallback to cache on failure
if(origin && url.pathname.endsWith("pattern.gif")) { if(origin && url.pathname.endsWith("pattern.gif")) {
const pattern = new Request(`${location.origin}/${root}assets/img/pattern.php`); const pattern = new Request(`${location.origin}/${root}assets/img/pattern.php`);
event.respondWith(fetch(pattern) || caches.match(url.pathname).then(response => response)); event.respondWith(
fetch(pattern).catch(() => {
return caches.match(event.request);
})
);
return; return;
} }