Skip to content

Commit

Permalink
update workbox from 4.3.1 to 6.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
spacegangster committed Aug 24, 2024
1 parent 3627818 commit deeded5
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 81 deletions.
7 changes: 5 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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"}
Expand Down
6 changes: 3 additions & 3 deletions src/page_renderer/api.clj
Original file line number Diff line number Diff line change
@@ -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)))

Expand Down Expand Up @@ -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))

2 changes: 1 addition & 1 deletion src/page_renderer/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
Expand Down
27 changes: 20 additions & 7 deletions src/page_renderer/service_worker_generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down
93 changes: 71 additions & 22 deletions src/page_renderer/service_worker_lifecycle.clj
Original file line number Diff line number Diff line change
@@ -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
"<script type=\"module\">
import { Workbox } from 'https://storage.googleapis.com/workbox-cdn/releases/4.1.0/workbox-window.prod.mjs';
import { Workbox } from 'https://storage.googleapis.com/workbox-cdn/releases/5.1.3/workbox-window.prod.mjs';
const promptStr = 'New version of the application is downloaded, do you want to update? May take two reloads.';
function createUIPrompt(opts) {
if (confirm(promptStr)) {
opts.onAccept()
}
if (confirm(promptStr)) {
opts.onAccept()
}
}
if ('serviceWorker' in navigator) {
const wb = new Workbox('${service-worker-url}');
wb.addEventListener('waiting', (event) => {
const prompt = createUIPrompt({
onAccept: async () => {
wb.addEventListener('activated', (event) => {
console.log('sw-init: activated')
const wb = new Workbox('${service-worker-url}');
wb.addEventListener('error', (error) => {
console.error('Service Worker registration failed:', error);
});
wb.addEventListener('waiting', (event) => {
const prompt = createUIPrompt({
onAccept: async () => {
wb.addEventListener('activated', (event) => {
console.log('sw-init: activated')
})
wb.addEventListener('controlling', (event) => {
console.log('sw-init: controlling')
window.location.reload();
});
wb.messageSW({type: 'SKIP_WAITING'});
}
})
});
wb.register();
}
</script>"
{:service-worker-url script-url}))


(defn sw-script-v6 [script-url]
(u/compile-template
"<script type=\"module\">
import { Workbox } from 'https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-window.prod.mjs';
const promptStr = 'New version of the application is downloaded, do you want to update? May take two reloads.';
function createUIPrompt(opts) {
if (confirm(promptStr)) {
opts.onAccept()
}
}
if ('serviceWorker' in navigator) {
const wb = new Workbox('${service-worker-url}');
wb.addEventListener('error', (error) => {
console.error('Service Worker registration failed:', error);
});
wb.addEventListener('waiting', (event) => {
const prompt = createUIPrompt({
onAccept: async () => {
wb.addEventListener('activated', (event) => {
console.log('sw-init: activated')
})
wb.addEventListener('controlling', (event) => {
console.log('sw-init: controlling')
window.location.reload();
});
wb.messageSW({type: 'SKIP_WAITING'});
}
})
wb.addEventListener('controlling', (event) => {
console.log('sw-init: controlling')
window.location.reload();
});
wb.messageSW({type: 'SKIP_WAITING'});
}
})
});
wb.register();
});
wb.register();
}
</script>"
{:service-worker-url script-url}))



46 changes: 27 additions & 19 deletions test/page_renderer/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,46 @@ document.head.appendChild(link);
(def ethalon-page-4
"<!DOCTYPE html><html ><head><meta charset=\"utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=5\"><link href=\"/favicon.png?hash=stub\" rel=\"icon\" type=\"image/png\" /><link href=\"/favicon.png?hash=stub\" rel=\"image_src\" type=\"image/png\" /><link href=\"/favicon.png?hash=stub\" rel=\"apple-touch-icon\" type=\"image/png\" /><link href=\"/favicon.png?hash=stub\" rel=\"apple-touch-startup-image\" type=\"image/png\" /><title>Page</title><meta name=\"theme-color\" content=\"white\"><meta property=\"og:title\" content=\"Page\"><link href=\"large-stuff.css?hash=stub\" rel=\"stylesheet\" type=\"text/css\" /><link href=\"large-stuff2.css?hash=stub\" rel=\"stylesheet\" type=\"text/css\" /></head><body><div class=\"page\">a page</div></body></html>")
(def ethalon-page-5
"<!DOCTYPE html><html ><head><meta charset=\"utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=5\"><link href=\"/favicon.png?hash=stub\" rel=\"icon\" type=\"image/png\" /><link href=\"/favicon.png?hash=stub\" rel=\"image_src\" type=\"image/png\" /><link href=\"/favicon.png?hash=stub\" rel=\"apple-touch-icon\" type=\"image/png\" /><link href=\"/favicon.png?hash=stub\" rel=\"apple-touch-startup-image\" type=\"image/png\" /><script type=\"module\">

import { Workbox } from 'https://storage.googleapis.com/workbox-cdn/releases/4.1.0/workbox-window.prod.mjs';
import { Workbox } from 'https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-window.prod.mjs';

const promptStr = 'New version of the application is downloaded, do you want to update? May take two reloads.';
function createUIPrompt(opts) {
if (confirm(promptStr)) {
opts.onAccept()
}
if (confirm(promptStr)) {
opts.onAccept()
}
}

if ('serviceWorker' in navigator) {
const wb = new Workbox('/sw-2.js');
wb.addEventListener('waiting', (event) => {
const prompt = createUIPrompt({
onAccept: async () => {
wb.addEventListener('activated', (event) => {
console.log('sw-init: activated')
const wb = new Workbox('/sw-2.js');

wb.addEventListener('error', (error) => {
console.error('Service Worker registration failed:', error);
});

wb.addEventListener('waiting', (event) => {
const prompt = createUIPrompt({
onAccept: async () => {
wb.addEventListener('activated', (event) => {
console.log('sw-init: activated')
})
wb.addEventListener('controlling', (event) => {
console.log('sw-init: controlling')
window.location.reload();
});
wb.messageSW({type: 'SKIP_WAITING'});
}
})
wb.addEventListener('controlling', (event) => {
console.log('sw-init: controlling')
window.location.reload();
});
wb.messageSW({type: 'SKIP_WAITING'});
}
})
});
wb.register();
});

wb.register();
}
</script><title>Page</title><meta name=\"theme-color\" content=\"white\"><meta property=\"og:title\" content=\"Page\"></head><body><div class=\"page\">a page</div></body></html>")
(deftest sanity
(testing "Sanity test"
(is (= ethalon-page
Expand Down
27 changes: 20 additions & 7 deletions test/page_renderer/ethalon-sw-2.js
Original file line number Diff line number Diff line change
@@ -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}) => {
Expand Down
27 changes: 20 additions & 7 deletions test/page_renderer/ethalon-sw.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
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' },
{ url: '/fonts/icomoon.woff', revision: 'file-hash' },
{ 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}) => {
Expand Down
26 changes: 13 additions & 13 deletions test/page_renderer/service_worker_generator_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

0 comments on commit deeded5

Please sign in to comment.