From 0c38005ec41a5a5bf83024a82b5156028a7b0a69 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Fri, 3 Jun 2022 11:24:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BE=D1=84=D1=84=D0=BB=D0=B0=D0=B9=D0=BD=20?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/js/sw.js | 56 ++++++++++++++++++++++++++++++++++- src/pages/note.njk | 3 ++ src/pages/offline.11tydata.js | 5 ++++ src/pages/offline.njk | 2 ++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/pages/offline.11tydata.js create mode 100644 src/pages/offline.njk diff --git a/src/js/sw.js b/src/js/sw.js index 0b7907f..21dc07b 100644 --- a/src/js/sw.js +++ b/src/js/sw.js @@ -1,4 +1,58 @@ +/* global self, caches */ import { precacheAndRoute } from 'workbox-precaching' -// eslint-disable-next-line no-undef precacheAndRoute(self.__WB_MANIFEST) + +const CACHE_NAME = 'static-v2' +const OFFLINE_URL = 'offline.html' + +self.addEventListener('install', event => { + console.log('Install!') + + event.waitUntil( + caches.open(CACHE_NAME).then(cache => { + return cache.addAll([ + '/', + '/js/bundle.js', + '/css/index.css', + '/fonts/OpenSans-Bold.ttf', + '/fonts/OpenSans-Bold.woff', + '/fonts/OpenSans-Bold.woff2', + '/fonts/OpenSans-Regular.ttf', + '/fonts/OpenSans-Regular.woff', + '/fonts/OpenSans-Regular.woff2', + '/favicon.ico', + '/favicon.svg', + OFFLINE_URL + ]) + }) + ) + + self.skipWaiting() +}) + +self.addEventListener('fetch', (event) => { + // Нам нужно вызвать функцию event.respondWith(), только если это запрос на переход между + // HTML-страницами. + if (event.request.mode === 'navigate') { + event.respondWith( + (async () => { + try { + const preloadResponse = await event.preloadResponse + if (preloadResponse) { + return preloadResponse + } + + const networkResponse = await fetch(event.request) + return networkResponse + } catch (error) { + console.log('Не удалось получить данные; вместо этого возвращаем страницу для автономного режима.', error) + + const cache = await caches.open(CACHE_NAME) + const cachedResponse = await cache.match(OFFLINE_URL) + return cachedResponse + } + })() + ) + } +}) diff --git a/src/pages/note.njk b/src/pages/note.njk index 6d56ee1..83a8de8 100644 --- a/src/pages/note.njk +++ b/src/pages/note.njk @@ -1,4 +1,7 @@
+ + {{ '/src/images/logo.svg' | svgContents("max-h-12 block w-[50vw]") | safe }} +

{{ note.data.title | safe }}

{{ note.templateContent | safe }} diff --git a/src/pages/offline.11tydata.js b/src/pages/offline.11tydata.js new file mode 100644 index 0000000..e6b8da6 --- /dev/null +++ b/src/pages/offline.11tydata.js @@ -0,0 +1,5 @@ +module.exports = { + layout: 'base.njk', + + permalink: '/offline.html' +} diff --git a/src/pages/offline.njk b/src/pages/offline.njk new file mode 100644 index 0000000..fbb1e84 --- /dev/null +++ b/src/pages/offline.njk @@ -0,0 +1,2 @@ +

Отсутствует подключение к сети

+Вернуться на главную \ No newline at end of file