Skip to content

Commit

Permalink
PWA Network First by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Jieiku committed Sep 10, 2023
1 parent 9c6f4e0 commit 4d89eb0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 19 deletions.
27 changes: 25 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ researchgate = "your-profile-id"
### online_url is used to restore the base_url after generating an Offline site.
### When you set offline = true and run the npm script, the base_url is set to the absolute path on disk.
### Once you set offline = false, the base_url will be set back to the value of online_url when you run the npm script again.

###############################################################################

# do NOT include a trailing slash on the online URL
Expand All @@ -234,9 +233,33 @@ js_switcher_default = "dark" # default nojs switcher mode: dark, light (make sur
search_library = 'elasticlunr'
stylesheets = ["abridge.css"]

pwa = true # true to load the service worker
webmanifest = "manifest.min.json" # Required for PWAs

###############################################################################
### PWA (Progressive Web Application)
### By default Abridge has pwa_NORM_TTL and pwa_LONG_TTL set to 0, this essential turns the PWA cache strategy into network first.
### Abridge uses cachebust hashing on js and css files, so anytime a page cache is updated, these resources would also get updated if changed.
### Media files rarely change, especially font files, so it is a good idea cache indefinitely.
### For pwa_TTL_EXEMPT indefinitely cached resources, you can force a new cache by incrementing the pwa_VER (cache version number).
### If you would like to try a cache first strategy then set a value higher than 0 for pwa_NORM_TTL and pwa_LONG_TTL.
###############################################################################

pwa = true # true to load the service worker
pwa_VER = '3.10.0' # Service Worker cache version. (increment if you need to force a new cache)

### 3600=1hour, 28800=8hours, 86400=1day, 604800=1week, 1209600=2weeks
pwa_NORM_TTL = 0 # 86400 is reasonable. html, json, xml, anything else undefined
pwa_LONG_TTL = 0 # 604800 is reasonable. js, css

### list of files that overrides TTL_LONG/TTL_EXEMPT to be a NORM TTL.
pwa_TTL_NORM = '"sw.min.js", "sw_load.min.js"'

### TTL_LONG file extensions will be cached for the LONG_TTL duration.
pwa_TTL_LONG = '"jpg", "jpeg", "png", "gif", "webp", "avif", "ico", "svg", "xsl", "txt"'

### TTL_EXEMPT file extensions will be cached indefinitely unless sw_load version is incremented, which would invalidate any existing cache. (and a new cache would be started)
pwa_TTL_EXEMPT = '"js", "css", "otf", "eot", "ttf", "woff", "woff2", "mp4", "webm", "mp3", "ogg"'

###############################################################################
### Favicons, comment out a line to disable loading some or all of these if needed.
###############################################################################
Expand Down
35 changes: 28 additions & 7 deletions package_abridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ const index_format = data.search.index_format;
const uglyurls = data.extra.uglyurls;
const offline = data.extra.offline;
const online_url = data.extra.online_url;
const sitePath = path.resolve('public');// used for offline builds
const pwa = data.extra.pwa;
const sitePath = path.resolve('public');// used for offline builds integrity = true
const pwa_VER = data.extra.pwa_VER;
const pwa_NORM_TTL = data.extra.pwa_NORM_TTL;
const pwa_LONG_TTL = data.extra.pwa_LONG_TTL;
const pwa_TTL_NORM = data.extra.pwa_TTL_NORM;
const pwa_TTL_LONG = data.extra.pwa_TTL_LONG;
const pwa_TTL_EXEMPT = data.extra.pwa_TTL_EXEMPT;

if (offline === false) {
replace.sync({files: 'config.toml', from: /base_url.*=.*/g, to: "base_url=\""+online_url+"\""});
Expand Down Expand Up @@ -79,7 +85,7 @@ function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search
}
}
if (pwa) {
minify_files.push(bpath+'static/js/sw_load.js');
minify_files.push('static/js/sw_load.js');
}
return minify_files;
}
Expand Down Expand Up @@ -146,10 +152,25 @@ if (search_library === 'elasticlunr') {
// zola build && stork build --input public/data_stork/index.html --output static/stork.st
}

if (pwa) {// Update pwa file list and hashes
if (pwa) {// Update pwa settings, file list, and hashes.
if (!fs.existsSync('static/sw.js')) {// static/sw.js file is missing, copy from abridge theme.
fs.copyFileSync(bpath+'static/sw.js', 'static/sw.js',fs.constants.COPYFILE_EXCL);
}
if (!fs.existsSync('static/js/sw_load.js')) {// static/sw.js file is missing, copy from abridge theme.
fs.copyFileSync(bpath+'static/js/sw_load.js', 'static/js/sw_load.js',fs.constants.COPYFILE_EXCL);
}
// Update settings in PWA javascript file, using options parsed from config.toml.
if (fs.existsSync('static/js/sw_load.js')) {
replace.sync({files: 'static/js/sw_load.js', from: /v=.*/g, to: "v="+pwa_VER+"\","});
}
if (fs.existsSync('static/sw.js')) {
replace.sync({files: 'static/sw.js', from: /NORM_TTL.*=.*/g, to: "NORM_TTL = "+pwa_NORM_TTL+";"});
replace.sync({files: 'static/sw.js', from: /LONG_TTL.*=.*/g, to: "LONG_TTL = "+pwa_LONG_TTL+";"});
replace.sync({files: 'static/sw.js', from: /TTL_NORM.*=.*/g, to: "TTL_NORM = ["+pwa_TTL_NORM+"];"});
replace.sync({files: 'static/sw.js', from: /TTL_LONG.*=.*/g, to: "TTL_LONG = ["+pwa_TTL_LONG+"];"});
replace.sync({files: 'static/sw.js', from: /TTL_EXEMPT.*=.*/g, to: "TTL_EXEMPT = ["+pwa_TTL_EXEMPT+"];"});
}

// Generate hashes for PWA BASE_CACHE_FILES
buff = fs.readFileSync('public/abridge.css');
const h_abridge_css = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);
Expand Down Expand Up @@ -212,8 +233,8 @@ if (bpath === '') {// abridge used directly
minify(['static/js/sw_load.js']);
minify(['static/sw.js']);
} else if (pwa) {
minify([bpath+'static/js/sw_load.js']);
minify([bpath+'static/sw.js']);
minify(['static/js/sw_load.js']);
minify(['static/sw.js']);
}

// Minify the json manifest
Expand All @@ -228,7 +249,7 @@ if (fs.existsSync('static/manifest.json')) {// if manifest.json is present, then
}

abridge_bundle = bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,false);
minify(abridge_bundle,bpath+'static/js/abridge_nopwa.min.js');
minify(abridge_bundle,'static/js/abridge_nopwa.min.js');

abridge_bundle = bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa);
minify(abridge_bundle,bpath+'static/js/abridge.min.js');
minify(abridge_bundle,'static/js/abridge.min.js');
2 changes: 1 addition & 1 deletion static/js/abridge.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/js/abridge_nosearch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion static/js/sw_load.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/sw.min.js?v=3.9.0", { scope: "/" })
.register("/sw.min.js?v=3.10.0",
{ scope: "/" })
.then(() => {
//console.info("SW Loaded");
}, err => console.error("SW error: ", err));
Expand Down
2 changes: 1 addition & 1 deletion static/js/sw_load.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4d89eb0

Please sign in to comment.