From 807c701edd28020a6e8ae2c83a74ac1ef89f1188 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Mon, 20 Aug 2018 16:24:33 -0400 Subject: [PATCH] Getting started with some service workers --- public/js/scripts-authed.min.js | 37 +++++++++++++++-------------- public/js/scripts-authed.min.js.map | 2 +- public/js/scripts.min.js | 7 +++--- public/js/scripts.min.js.map | 2 +- public/js/src/index-authed.js | 2 +- public/js/src/index.js | 9 +++++++ public/tools/build-js.js | 14 ++++++----- sw.js | 36 ++++++++++++++++++++++++++++ 8 files changed, 79 insertions(+), 30 deletions(-) create mode 100644 sw.js diff --git a/public/js/scripts-authed.min.js b/public/js/scripts-authed.min.js index 18b80b3c..56f6e886 100644 --- a/public/js/scripts-authed.min.js +++ b/public/js/scripts-authed.min.js @@ -1,20 +1,21 @@ -var e=e||{};e.scope={};e.ASSUME_ES5=!1;e.ASSUME_NO_NATIVE_MAP=!1;e.ASSUME_NO_NATIVE_SET=!1;e.defineProperty=e.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(c,g,l){c!=Array.prototype&&c!=Object.prototype&&(c[g]=l.value)};e.getGlobal=function(c){return"undefined"!=typeof window&&window===c?c:"undefined"!=typeof global&&null!=global?global:c};e.global=e.getGlobal(this);e.SYMBOL_PREFIX="jscomp_symbol_"; -e.initSymbol=function(){e.initSymbol=function(){};e.global.Symbol||(e.global.Symbol=e.Symbol)};e.Symbol=function(){var c=0;return function(g){return e.SYMBOL_PREFIX+(g||"")+c++}}();e.initSymbolIterator=function(){e.initSymbol();var c=e.global.Symbol.iterator;c||(c=e.global.Symbol.iterator=e.global.Symbol("iterator"));"function"!=typeof Array.prototype[c]&&e.defineProperty(Array.prototype,c,{configurable:!0,writable:!0,value:function(){return e.arrayIterator(this)}});e.initSymbolIterator=function(){}}; -e.arrayIterator=function(c){var g=0;return e.iteratorPrototype(function(){return g { + console.log('Service worker registered', reg.scope); + }).catch(error => { + console.error('Failed to register service worker', error); + }); +} + diff --git a/public/tools/build-js.js b/public/tools/build-js.js index 791174b7..1b4c27c0 100644 --- a/public/tools/build-js.js +++ b/public/tools/build-js.js @@ -11,31 +11,33 @@ const plugins = [ }) ]; +const defaultOutput = { + format: 'iife', + sourcemap: true, +} + export default [{ input: './js/src/index.js', output: { + ...defaultOutput, file: './js/scripts.min.js', - format: 'iife', - sourcemap: true, sourcemapFile: './js/scripts.min.js.map', }, plugins, }, { input: './js/src/index-authed.js', output: { + ...defaultOutput, file: './js/scripts-authed.min.js', - format: 'iife', - sourcemap: true, sourcemapFile: './js/scripts-authed.min.js.map', }, plugins, }, { input: './js/src/base/sort_tables.js', output: { + ...defaultOutput, file: './js/tables.min.js', - format: 'iife', - sourcemap: true, sourcemapFile: './js/tables.min.js.map', }, plugins, diff --git a/sw.js b/sw.js new file mode 100644 index 00000000..75f86d7e --- /dev/null +++ b/sw.js @@ -0,0 +1,36 @@ +self.addEventListener('install', event => { + console.log('Worker installed'); + + event.waitUntil( + caches.open('hummingbird-anime-client') + .then(cache => cache.addAll([ + 'public/images/icons/favicon.ico', + 'public/css/app.min.css', + 'public/js/index.min.js', + 'public/js/index-authed.min.js', + 'public/js/tables.min.js', + ])) + ); +}); + +self.addEventListener('fetch', event => { + + event.respondWith( + caches.match(event.request).then(response => { + if (response !== undefined) { + // Return cached version + return response; + } + + return fetch(event.request).then(response => { + const clone = response.clone(); + + caches.open('hummingbird-anime-client').then(cache => { + cache.put(event.request, clone); + }); + + return response; + }); + }) + ); +}); \ No newline at end of file