From c299177f2c421ccb1b3326868d7731c419502d6c Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 27 May 2019 20:18:26 +0200 Subject: [PATCH] service worker --- www/components/layout.jsx | 25 +++++++++++-- www/server.js | 4 ++- www/static/sw.js | 74 +++++++++++++++++++++++++++++++++++++++ www/utils/hooks.js | 26 +++++++++++++- 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 www/static/sw.js diff --git a/www/components/layout.jsx b/www/components/layout.jsx index 8b76f55..82587f6 100755 --- a/www/components/layout.jsx +++ b/www/components/layout.jsx @@ -1,19 +1,31 @@ -import React from 'react' +import React, { useEffect } from 'react' import Head from 'next/head' import Link from 'next/link' import { logout } from '../utils/auth' import { getRandomSlogan } from '../utils/misc' +import { editableWhenOnline } from '../utils/hooks' const Layout = ({ children }) => { const title = getRandomSlogan() + const online = typeof window !== 'undefined' + ? editableWhenOnline() + : true + + useEffect(() => { + if (window && 'navigator' in window && 'serviceWorker' in navigator) { + window.addEventListener('load', () => { + navigator.serviceWorker.register('/sw.js') + }) + } + }, []) + return {title} - {/* https://www.flaticon.com/packs/zoo-20 */} @@ -70,12 +82,21 @@ const Layout = ({ children }) => { + {!online &&
Offline
} + {/* language=CSS */}