Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/danielkvist/hugo-piercer-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authordanielkvist <danielkvist@protonmail.com>2019-04-20 12:18:16 +0300
committerdanielkvist <danielkvist@protonmail.com>2019-04-20 12:18:16 +0300
commitaddb30270072b51124ddf6c6e3427f5bc5627003 (patch)
tree94e0fe83689c75ecb944e64843cf75b5031ed5d2 /assets
parentfd76f140fa05688c0cc5eeee9f0bcd13e604753f (diff)
add service worker
Diffstat (limited to 'assets')
-rw-r--r--assets/js/header.js7
-rw-r--r--assets/js/main.js15
-rw-r--r--assets/sw.js42
3 files changed, 57 insertions, 7 deletions
diff --git a/assets/js/header.js b/assets/js/header.js
deleted file mode 100644
index 12de881..0000000
--- a/assets/js/header.js
+++ /dev/null
@@ -1,7 +0,0 @@
-const header = document.querySelector("header");
-
-function paddingHeader() {
- document.body.style.paddingTop = `${header.offsetHeight}px`;
-}
-
-window.addEventListener("load", paddingHeader); \ No newline at end of file
diff --git a/assets/js/main.js b/assets/js/main.js
new file mode 100644
index 0000000..8549696
--- /dev/null
+++ b/assets/js/main.js
@@ -0,0 +1,15 @@
+// Header
+const header = document.querySelector('header');
+
+window.addEventListener("load", () => {
+ document.body.style.paddingTop = `${header.offsetHeight}px`;
+});
+
+// Service Worker
+if ('serviceWorker' in navigator) {
+ navigator.serviceWorker.register('/sw.min.js', { scope: '/' }).then((registration) => {
+ console.log('ServiceWorker registration successful with scope: ', registration.scope);
+ }, (err) => {
+ console.log('ServiceWorker registration failed: ', err);
+ });
+} \ No newline at end of file
diff --git a/assets/sw.js b/assets/sw.js
new file mode 100644
index 0000000..e40529f
--- /dev/null
+++ b/assets/sw.js
@@ -0,0 +1,42 @@
+let cacheVersion = 'v1';
+let urlsToCache = [
+ '/images/',
+ '/css/bundle.min.css',
+ '/js/main.min.js',
+];
+
+self.addEventListener('install', (e) => {
+ e.waitUntil(
+ caches.open('v1').then(function (c) {
+ return c.addAll(urlsToCache);
+ })
+ );
+});
+
+self.addEventListener('fetch', (e) => {
+ e.respondWith(
+ caches.match(e.request)
+ .then((response) => {
+ if (response) {
+ return response;
+ }
+
+ let fetchRequest = e.request.clone();
+ return fetch(fetchRequest).then(
+ (response) => {
+ if (!response || response.status !== 200 || response.type !== 'basic') {
+ return response;
+ }
+
+ let responseToCache = response.clone();
+ caches.open(cacheVersion)
+ .then((cache) => {
+ cache.put(e.request, responseToCache);
+ });
+
+ return response;
+ }
+ );
+ })
+ );
+}); \ No newline at end of file