From 8791c5fa92e71e4ffccf493d536bb06c91fbbe85 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Thu, 9 Apr 2020 21:46:43 +0100 Subject: [PATCH] Added basic service-worker support --- .netlify/state.json | 3 ++ img/192.png | Bin 0 -> 852 bytes img/512.png | Bin 0 -> 1746 bytes index.html | 24 ++++++++++++ manifest.json | 19 ++++++++++ service-worker.js | 89 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 .netlify/state.json create mode 100644 img/192.png create mode 100644 img/512.png create mode 100644 manifest.json create mode 100644 service-worker.js diff --git a/.netlify/state.json b/.netlify/state.json new file mode 100644 index 0000000..ac0c201 --- /dev/null +++ b/.netlify/state.json @@ -0,0 +1,3 @@ +{ + "siteId": "492c17c8-1ecc-479f-8abc-7eb13736dfb9" +} \ No newline at end of file diff --git a/img/192.png b/img/192.png new file mode 100644 index 0000000000000000000000000000000000000000..f3d0ebaae948b4ee3b4c5bfac7550a5c49a24bab GIT binary patch literal 852 zcmeAS@N?(olHy`uVBq!ia0vp^2SAvG8AvYpRA>UEqyv0HT+f_2Gi}Z3=e-!gK)v6?hP=;y3;lmG1!=sBzUOWBtyqV=O zC+o^Z=Pa~Xj4gO|4&7I=b#Ts7+%(_)Tw50894%yjF?8iT9dqUsNS?=0-LE%>-|lHJV4oku=PK8TT>$$0-@ z>ap*JyKc|9^XBY;HXf)rq&BxGecr zc8%JGjV_yfTOvl-dfgbIMKZI-tN-DBpL0Iv^PJ~=&hz=Kw4aP{Lye6QBDy z+brb{vQ|SQ@YVjrdI8HB$&e5EqRF*6YlM8g~^~8MZnGcVbWyu zVj1sD>H=|y<9cX$IC=1Xv6m`Z?L6FI zx5jEy$+H8r3*~{AIVAkC#JY%wJ{|jLN1u+`ZV)gnaE~^eLeprP`*?PFZb@w7xtaBF zMw5cdA`fvKlLGuD!CeKRCvhjbl)oKLAC7rh=~J)KPctGsbx8NM|D`ImJE%A)yqxIKUDe$kH<}n4H-CBr4=Xv{a&eNaQPYB6 zTV2hWe@Z)F^r}}!DzWWWO8;3{_0t-S1NGg77#BT3+Wf$KQGi^lV4f7?6gQU41`C>*S3-ez7IJ=kr zed>xW>*xqE34&wVnVMhzn#HH98>tt+{kk^);_l`E=&e8*^F_Z{v?wL{k4i)~Ea#B6 zbO>&%NBp#2-Qc0cw|qm9sH*V2h>^=_2_nA3Z-HM>QT4-N)eS(=$_2)W=i@pRENkef zKxt9lE=@`cSX>a@_{xbM8H>fVrp`$N5dvj(Ov@+JtBi1LN^?TuV{H07>h@aQS3xH_ zq%9i~73na}&R*Dbw@3>=^fh)^c&X86R8K~~(r|6xgb(P_)REG->@%N8&iA08Kc+P* zEX5wt;ZC@D74g2}Z?iv#z6}?T$8;{Pl{>toIdnOcNxiAK7pprrYIa=sv#iy(E6I2HgYGin5rqY&>e}`8qJySa zt+1J`fyC1rI5+K;*X9|UQ6M!3KVwd7_`H**op0L2KWRTYl1&^{q;;5ljet|(oFM<> z`(X&4eGOaKVM&}+M)sAQ1Cl-lm{#efxZEIZ&{qzh+C5!`gugtSvmTW`58XrR#tPr+ znWjzQay0{ppD(RoxJW0T%-{Dvl6+%ufv(m%1+qIR-C6`UC40Z!-jA+wF`5QN$?j~r z&=PjWUp5!|09!1^y=2GPyHt_X%*Dap1PmB~n4Qm2!-N)}Q z1!=(`2Lgzw2i)8AKbtn1scNJab$d@cL*ufeWqCX5p4~EYMDB`6wDqg*;%v|%nrU0r z>g>5=-awG@XRXR{MV!7fH1pq`WbLQ}w8ugZ6H=0vL7wkaR(y)HlsmQvoR+?c(+{ip zWpDtQf(RHWi>ig8U~{F9G<|?KS^QZ8(qC)`I4V?MfCLvrTGoIyQP z)jghl6LD + + @@ -293,6 +295,28 @@ + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..94f06a7 --- /dev/null +++ b/manifest.json @@ -0,0 +1,19 @@ +{ + "name": "Turnip Prophet - ACNH Turnip Tracker", + "short_name": "Turnip Prophet", + "description": "An app to track your Animal Crossing: New Horizons turnip prices daily!", + "start_url": "index.html", + "display": "standalone", + "background_color": "#def2d9", + "theme_color": "#def2d9", + "icons": [ + { + "src": "/img/192.png", + "sizes": "192x192" + }, + { + "src": "/img/512.png", + "sizes": "512x512" + } + ] +} diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 0000000..a0bcf11 --- /dev/null +++ b/service-worker.js @@ -0,0 +1,89 @@ +// PWA Code adapted from https://github.com/pwa-builder/PWABuilder +const CACHE = "pwa-precache"; +const precacheFiles = [ + "/index.html", + "/js/predictions.js", + "/js/scripts.js", + "/css/styles.css", +]; + +self.addEventListener("install", function (event) { + console.log("[PWA] Install Event processing"); + + console.log("[PWA] Skip waiting on install"); + self.skipWaiting(); + + event.waitUntil( + caches.open(CACHE).then(function (cache) { + console.log("[PWA] Caching pages during install"); + return cache.addAll(precacheFiles); + }) + ); +}); + +// Allow sw to control of current page +self.addEventListener("activate", function (event) { + console.log("[PWA] Claiming clients for current page"); + event.waitUntil(self.clients.claim()); +}); + +// If any fetch fails, it will look for the request in the cache and serve it from there first +self.addEventListener("fetch", function (event) { + if (event.request.method !== "GET") return; + + event.respondWith( + fromCache(event.request).then( + (response) => { + // The response was found in the cache so we responde with it and update the entry + + // This is where we call the server to get the newest version of the + // file to use the next time we show view + event.waitUntil( + fetch(event.request).then(function (response) { + return updateCache(event.request, response); + }) + ); + + return response; + }, + () => { + // The response was not found in the cache so we look for it on the server + return fetch(event.request) + .then(function (response) { + // If request was success, add or update it in the cache + event.waitUntil( + updateCache(event.request, response.clone()) + ); + + return response; + }) + .catch(function (error) { + console.log( + "[PWA] Network request failed and no cache." + error + ); + }); + } + ) + ); +}); + +function fromCache(request) { + // Check to see if you have it in the cache + // Return response + // If not in the cache, then return + return caches.open(CACHE).then(function (cache) { + return cache.match(request).then(function (matching) { + if (!matching || matching.status === 404) { + return Promise.reject("no-match"); + } + + return matching; + }); + }); +} + +function updateCache(request, response) { + return caches.open(CACHE).then(function (cache) { + return cache.put(request, response); + }); +}