From deeded580df1469cbc545abd84d6464b749eb883 Mon Sep 17 00:00:00 2001 From: Ivan Fedorov Date: Sat, 24 Aug 2024 22:29:43 +0300 Subject: [PATCH] update workbox from 4.3.1 to 6.5.4 --- project.clj | 7 +- src/page_renderer/api.clj | 6 +- src/page_renderer/core.clj | 2 +- .../service_worker_generator.clj | 27 ++++-- .../service_worker_lifecycle.clj | 93 ++++++++++++++----- test/page_renderer/core_test.clj | 46 +++++---- test/page_renderer/ethalon-sw-2.js | 27 ++++-- test/page_renderer/ethalon-sw.js | 27 ++++-- .../service_worker_generator_test.clj | 26 +++--- 9 files changed, 180 insertions(+), 81 deletions(-) diff --git a/project.clj b/project.clj index b047610..964bf09 100644 --- a/project.clj +++ b/project.clj @@ -1,5 +1,8 @@ -(defproject page-renderer "0.4.7" - :description "Clojure PWA made easy. An HTML renderer ready for social networks and PWA" +(defproject page-renderer "0.4.8" + :description + "A holistic approach for rendering a modern HTML web app frame. + Has bindings for common page meta, PWA, and social meta tags." + :url "https://github.com/spacegangster/page-renderer" :license {:name "The MIT License" :url "http://opensource.org/licenses/MIT"} diff --git a/src/page_renderer/api.clj b/src/page_renderer/api.clj index 391ed80..a7cc843 100644 --- a/src/page_renderer/api.clj +++ b/src/page_renderer/api.clj @@ -1,7 +1,7 @@ (ns page-renderer.api (:gen-class) (:require [page-renderer.core :as core] - [page-renderer.service-worker-generator :as swg] + [page-renderer.service-worker-generator :as swg-v6] [page-renderer.cachebusting :as fu]) (:import (java.util Map))) @@ -89,11 +89,11 @@ See render-page for docs on renderable." ^String [^Map renderable] - (swg/generate-script renderable)) + (swg-v6/generate-script renderable)) (defn respond-service-worker "Generates a Ring response map containing a service worker script as a body. See generate-service-worker for more docs" ^Map [^Map renderable] - (swg/generate-ring-response renderable)) + (swg-v6/generate-ring-response renderable)) diff --git a/src/page_renderer/core.clj b/src/page_renderer/core.clj index 3042b75..2389528 100644 --- a/src/page_renderer/core.clj +++ b/src/page_renderer/core.clj @@ -203,7 +203,7 @@ (on-dom-interactive-fragment on-dom-interactive-js)) (if service-worker - (swl/sw-script2 service-worker)) + (swl/sw-script-v6 service-worker)) (seq head-tags) ; diff --git a/src/page_renderer/service_worker_generator.clj b/src/page_renderer/service_worker_generator.clj index af2d698..dbaae96 100644 --- a/src/page_renderer/service_worker_generator.clj +++ b/src/page_renderer/service_worker_generator.clj @@ -5,17 +5,30 @@ (:import (java.util Map))) (def ^:private template - "importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js') + "importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js') + +console.log('Service Worker: ', self); workbox.precaching.precacheAndRoute([ ${precache-assets} -], { ignoreURLParametersMatching: [/hash/] }) +], { ignoreURLParametersMatching: [/hash/] }); -workbox.routing.registerNavigationRoute( - workbox.precaching.getCacheKeyForURL('${default-url}'), { - whitelist: [ /${whitelist-regex}/ ], - blacklist: [ /${blacklist-regex}/ ] - } + +// default handler +const defaultHandler = new workbox.strategies.CacheFirst({ + cacheName: 'default-handler-cache', +}); + + +// routing +workbox.routing.registerRoute( + new workbox.routing.NavigationRoute( + defaultHandler, + workbox.precaching.getCacheKeyForURL('${default-url}'), { + whitelist: [ /${whitelist-regex}/ ], + blacklist: [ /${blacklist-regex}/ ] + } + ) ) workbox.routing.setCatchHandler(({event}) => { diff --git a/src/page_renderer/service_worker_lifecycle.clj b/src/page_renderer/service_worker_lifecycle.clj index 93f9292..960ccb9 100644 --- a/src/page_renderer/service_worker_lifecycle.clj +++ b/src/page_renderer/service_worker_lifecycle.clj @@ -1,37 +1,86 @@ (ns page-renderer.service-worker-lifecycle - (:require [page-renderer.cachebusting :as fu] - [page-renderer.util :as u])) + (:require [page-renderer.util :as u])) -(defn sw-script2 [script-url] +(defn sw-script [script-url] (u/compile-template "" + {:service-worker-url script-url})) + + +(defn sw-script-v6 [script-url] + (u/compile-template +"" {:service-worker-url script-url})) + + + diff --git a/test/page_renderer/core_test.clj b/test/page_renderer/core_test.clj index 83ec41c..c7d2606 100644 --- a/test/page_renderer/core_test.clj +++ b/test/page_renderer/core_test.clj @@ -64,38 +64,46 @@ document.head.appendChild(link); (def ethalon-page-4 "Page
a page
") + (def ethalon-page-5 "Page
a page
") + (deftest sanity (testing "Sanity test" (is (= ethalon-page diff --git a/test/page_renderer/ethalon-sw-2.js b/test/page_renderer/ethalon-sw-2.js index 019f300..3bf3126 100644 --- a/test/page_renderer/ethalon-sw-2.js +++ b/test/page_renderer/ethalon-sw-2.js @@ -1,17 +1,30 @@ -importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js') +importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js') + +console.log('Service Worker: ', self); workbox.precaching.precacheAndRoute([ { url: '/', revision: 'file-hash' }, { url: '/fonts/icomoon.woff', revision: 'file-hash' }, { url: '/lightpad/compiled/app.js', revision: 'file-hash' }, { url: '/favicon.png', revision: 'file-hash' } -], { ignoreURLParametersMatching: [/hash/] }) +], { ignoreURLParametersMatching: [/hash/] }); -workbox.routing.registerNavigationRoute( - workbox.precaching.getCacheKeyForURL('/'), { - whitelist: [ /^\// ], - blacklist: [ /^\/service-worker\.js/ ] - } + +// default handler +const defaultHandler = new workbox.strategies.CacheFirst({ + cacheName: 'default-handler-cache', +}); + + +// routing +workbox.routing.registerRoute( + new workbox.routing.NavigationRoute( + defaultHandler, + workbox.precaching.getCacheKeyForURL('/'), { + whitelist: [ /^\// ], + blacklist: [ /^\/service-worker\.js/ ] + } + ) ) workbox.routing.setCatchHandler(({event}) => { diff --git a/test/page_renderer/ethalon-sw.js b/test/page_renderer/ethalon-sw.js index d564d71..30a8d13 100644 --- a/test/page_renderer/ethalon-sw.js +++ b/test/page_renderer/ethalon-sw.js @@ -1,4 +1,6 @@ -importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js') +importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js') + +console.log('Service Worker: ', self); workbox.precaching.precacheAndRoute([ { url: '/heavy-stuff.css', revision: 'file-hash' }, @@ -6,13 +8,24 @@ workbox.precaching.precacheAndRoute([ { url: '/lightpad/compiled/app.js', revision: 'file-hash' }, { url: '/favicon.png', revision: 'file-hash' }, { url: '/app', revision: 'file-hash' } -], { ignoreURLParametersMatching: [/hash/] }) +], { ignoreURLParametersMatching: [/hash/] }); -workbox.routing.registerNavigationRoute( - workbox.precaching.getCacheKeyForURL('/app'), { - whitelist: [ /^\/app/ ], - blacklist: [ /^\/app\/service-worker\.js/ ] - } + +// default handler +const defaultHandler = new workbox.strategies.CacheFirst({ + cacheName: 'default-handler-cache', +}); + + +// routing +workbox.routing.registerRoute( + new workbox.routing.NavigationRoute( + defaultHandler, + workbox.precaching.getCacheKeyForURL('/app'), { + whitelist: [ /^\/app/ ], + blacklist: [ /^\/app\/service-worker\.js/ ] + } + ) ) workbox.routing.setCatchHandler(({event}) => { diff --git a/test/page_renderer/service_worker_generator_test.clj b/test/page_renderer/service_worker_generator_test.clj index 3339875..b1bd675 100644 --- a/test/page_renderer/service_worker_generator_test.clj +++ b/test/page_renderer/service_worker_generator_test.clj @@ -5,32 +5,32 @@ (def sw-params - {:sw-default-url "/app" - :sw-add-assets ["/fonts/icomoon.woff"] + {:sw-default-url "/app" + :sw-add-assets ["/fonts/icomoon.woff"] :stylesheet-async "/heavy-stuff.css" - :script "/lightpad/compiled/app.js"}) + :script "/lightpad/compiled/app.js"}) (def sw-params-2 {:sw-default-url "/" - :sw-add-assets ["/fonts/icomoon.woff"] - :script "/lightpad/compiled/app.js"}) + :sw-add-assets ["/fonts/icomoon.woff"] + :script "/lightpad/compiled/app.js"}) (defn- slash-revision [script-str] (s/replace script-str #"revision: '[a-z\d]+'" "revision: 'file-hash'")) -(def ethalon-src "test/page_renderer/ethalon-sw.js") -(def ethalon-2-src "test/page_renderer/ethalon-sw-2.js") +(def expected-src "test/page_renderer/ethalon-sw.js") +(def expected-2-src "test/page_renderer/ethalon-sw-2.js") -(def ethalon (slurp ethalon-src)) -(def ethalon-2 (slurp ethalon-2-src)) +(def expected-str (slurp expected-src)) +(def expected-str-2 (slurp expected-2-src)) (deftest test-service-worker-generator (testing "Sanity test" - (is (= ethalon (slash-revision (swg/generate-script sw-params))))) + (is (= expected-str (slash-revision (swg/generate-script sw-params))))) (testing "Sanity test for root url" - (is (= ethalon-2 (slash-revision (swg/generate-script sw-params-2)))))) + (is (= expected-str-2 (slash-revision (swg/generate-script sw-params-2)))))) (comment (run-tests 'page-renderer.service-worker-generator-test) - - (spit ethalon-src (slash-revision (swg/generate-script sw-params)))) + (spit expected-src (slash-revision (swg/generate-script sw-params))) + (spit expected-2-src (slash-revision (swg/generate-script sw-params-2))))