Skip to content

Commit

Permalink
PWA Fixes, Options, Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jieiku committed Sep 11, 2023
1 parent 4d89eb0 commit c44af9d
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 79 deletions.
11 changes: 9 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,15 @@ webmanifest = "manifest.min.json" # Required for PWAs
### 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.
### The options below other than pwa=true, only come into play when the npm/node script is ran.
###############################################################################

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)
pwa_VER = '3.11.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
pwa_LONG_TTL = 0 # 604800 is reasonable.

### list of files that overrides TTL_LONG/TTL_EXEMPT to be a NORM TTL.
pwa_TTL_NORM = '"sw.min.js", "sw_load.min.js"'
Expand All @@ -260,6 +261,12 @@ pwa_TTL_LONG = '"jpg", "jpeg", "png", "gif", "webp", "avif", "ico", "svg", "xsl"
### 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"'

### If set to true then the entire site is cached. (useful for making an entire site usable while offline)
pwa_cache_all = true

### List of Files for the PWA to initially Cache, used if pwa_cache_all = false
pwa_BASE_CACHE_FILES = "'/js/theme.min.js','/js/theme_light.min.js','/abridge.css','/js/abridge.min.js','/','/404.html','/offline/','/manifest.min.json'"

###############################################################################
### Favicons, comment out a line to disable loading some or all of these if needed.
###############################################################################
Expand Down
4 changes: 2 additions & 2 deletions content/overview-showdata.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Ce sont les paramètres, actuellement tous les 3 sont requis.

### Usage

Ce thème nécessite une version {{ showdata(src="https://raw.githubusercontent.com/Jieiku/abridge/master/theme.toml" type="toml" key="min_version") }} ou plus tard de Zola.
Ce thème nécessite une version {{ showdata(src="../theme.toml" type="toml" key="min_version") }} ou plus tard de Zola.

### Sortir

Ce thème nécessite une version {{ showdata(src="https://raw.githubusercontent.com/Jieiku/abridge/master/theme.toml" type="toml" key="min_version") }} ou plus tard de Zola.
Ce thème nécessite une version {{ showdata(src="../theme.toml" type="toml" key="min_version") }} ou plus tard de Zola.
4 changes: 2 additions & 2 deletions content/overview-showdata.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ These are the parameters, currently all 3 are required.

### Usage

This theme requires version {{ showdata(src="https://raw.githubusercontent.com/Jieiku/abridge/master/theme.toml" type="toml" key="min_version") }} or later of Zola.
This theme requires version {{ showdata(src="../theme.toml" type="toml" key="min_version") }} or later of Zola.

### Output

This theme requires version {{ showdata(src="https://raw.githubusercontent.com/Jieiku/abridge/master/theme.toml" type="toml" key="min_version") }} or later of Zola.
This theme requires version {{ showdata(src="../theme.toml" type="toml" key="min_version") }} or later of Zola.
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
"license": "MIT",
"homepage": "https://github.com/Jieiku/abridge",
"scripts": {
"abridge": "node package_abridge.js",
"build": "zola build && npm run abridge && zola build",
"serve": "zola build && npm run abridge && zola serve",
"tinysearch": "mkdir -p tmp && tinysearch --optimize --path tmp public/data_tinysearch/index.html && rsync -avz tmp/*.wasm static/ && rm -rf tmp",
"stork": "stork build --input public/data_stork/index.html --output static/stork.st"
"abridge": "node -e \"if ( require('fs').existsSync('./themes/abridge/package_abridge.js')) {require('fs').copyFileSync('./themes/abridge/package_abridge.js', './package_abridge.js')}\" && node package_abridge.js"
},
"dependencies": {
"jq": "^1.7.2",
"fast-toml": "^0.5.4",
"uglify-js": "^3.17.4",
"jsonminify": "^0.4.2",
Expand Down
113 changes: 61 additions & 52 deletions package_abridge.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require('fs');
const crypto = require('crypto');
const path = require("path");
const TOML = require('fast-toml');
const UglifyJS = require('uglify-js');
const jsonminify = require("jsonminify");
const replace = require('replace-in-file');
//const crypto = require('crypto'); (disabled for now, because unused)

// check if abridge is used directly or as a theme.
bpath = '';
Expand All @@ -22,6 +22,7 @@ const js_copycode = data.extra.js_copycode;
const search_library = data.extra.search_library;
const index_format = data.search.index_format;
const uglyurls = data.extra.uglyurls;
const js_bundle = data.extra.js_bundle;
const offline = data.extra.offline;
const online_url = data.extra.online_url;
const sitePath = path.resolve('public');// used for offline builds
Expand All @@ -32,6 +33,8 @@ 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;
const pwa_cache_all = data.extra.pwa_cache_all;
const pwa_BASE_CACHE_FILES = data.extra.pwa_BASE_CACHE_FILES;

if (offline === false) {
replace.sync({files: 'config.toml', from: /base_url.*=.*/g, to: "base_url=\""+online_url+"\""});
Expand All @@ -44,11 +47,12 @@ if (base_url.slice(-1) == "/") {
base_url = base_url.slice(0, -1);
}

function checksum(input) {
var hash = crypto.createHash('sha384').update(input, 'utf8');
var hashBase64 = hash.digest('base64');
return 'sha384-' + hashBase64;
}
// disabled for now, because unused
// function checksum(input) {
// var hash = crypto.createHash('sha384').update(input, 'utf8');
// var hashBase64 = hash.digest('base64');
// return 'sha384-' + hashBase64;
// }

function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa) {
minify_files = [];
Expand Down Expand Up @@ -153,15 +157,16 @@ if (search_library === 'elasticlunr') {
}

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.
// update from abridge theme.
fs.copyFileSync(bpath+'static/sw.js', 'static/sw.js');
fs.copyFileSync(bpath+'static/js/sw_load.js', 'static/js/sw_load.js');
// Update settings in PWA javascript file, using options parsed from config.toml. sw.min.js?v=3.10.0", "++"
if (fs.existsSync('static/js/sw_load.js')) {
replace.sync({files: 'static/js/sw_load.js', from: /v=.*/g, to: "v="+pwa_VER+"\","});
sw_load_min = '.js?v=';
if (js_bundle) {
sw_load_min = '.min.js?v=';
}
replace.sync({files: 'static/js/sw_load.js', from: /sw.*v=.*/g, to: "sw"+sw_load_min+pwa_VER+"\","});
}
if (fs.existsSync('static/sw.js')) {
replace.sync({files: 'static/sw.js', from: /NORM_TTL.*=.*/g, to: "NORM_TTL = "+pwa_NORM_TTL+";"});
Expand All @@ -171,45 +176,49 @@ if (pwa) {// Update pwa settings, file list, and hashes.
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);
buff = fs.readFileSync('public/js/abridge.min.js');
const h_abridge_js = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);
buff = fs.readFileSync('public/js/abridge_nosearch.min.js');
const h_abridge_nosearch_js = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);
buff = fs.readFileSync('public/js/katexbundle.min.js');
const h_katexbundle_js = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);

// Generate array from the list of files, for the entire site.
const path = './public/';
cache = 'this.BASE_CACHE_FILES = [';
files = fs.readdirSync(path, { recursive: true, withFileTypes: false })
.forEach(
(file) => {
// check if is directory, if not then add the path/file
if (!fs.lstatSync(path+file).isDirectory()) {
// format output
item = "/"+file.replace(/index\.html$/i,'');// strip index.html from path
item = item.replace(/^\/sw(\.min)?\.js/i,'');// dont cache service worker

// look for files ending in .js, if its not preceded by .min then skip it.

// if formatted output is not empty line then append it to cache var
if (item != '') {// skip empty lines
cache = cache+"'"+item+"',";
/// Generate hashes for PWA BASE_CACHE_FILES (disabled, seems unsupported, errors on install)
// buff = fs.readFileSync('public/abridge.css');
// const h_abridge_css = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);
// buff = fs.readFileSync('public/js/abridge.min.js');
// const h_abridge_js = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);
// buff = fs.readFileSync('public/js/abridge_nosearch.min.js');
// const h_abridge_nosearch_js = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);
// buff = fs.readFileSync('public/js/katexbundle.min.js');
// const h_katexbundle_js = crypto.createHash('sha256').update(buff).digest('hex').slice(0,20);

if (pwa_cache_all) {
// Generate array from the list of files, for the entire site.
const path = './public/';
cache = 'this.BASE_CACHE_FILES = [';
files = fs.readdirSync(path, { recursive: true, withFileTypes: false })
.forEach(
(file) => {
// check if is directory, if not then add the path/file
if (!fs.lstatSync(path+file).isDirectory()) {
// format output
item = "/"+file.replace(/index\.html$/i,'');// strip index.html from path
item = item.replace(/^\/sw(\.min)?\.js/i,'');// dont cache service worker

// look for files ending in .js, if its not preceded by .min then skip it.

// if formatted output is not empty line then append it to cache var
if (item != '') {// skip empty lines
cache = cache+"'"+item+"',";
}
}
}
}
);
cache = cache.slice(0, -1)+'];'// remove the last comma and close the array
);
cache = cache.slice(0, -1)+'];'// remove the last comma and close the array
} else if (pwa_BASE_CACHE_FILES) {
cache = 'this.BASE_CACHE_FILES = ['+pwa_BASE_CACHE_FILES+'];';
}

// add cachebust hashes
cache = cache.replace(/abridge\.css/i,'abridge.css?h='+h_abridge_css);
cache = cache.replace(/abridge\.min\.js/i,'abridge.min.js?h='+h_abridge_js);
cache = cache.replace(/abridge_nosearch\.min\.js/i,'abridge_nosearch.min.js?h='+h_abridge_nosearch_js);
cache = cache.replace(/katexbundle\.min\.js/i,'katexbundle.min.js?h='+h_katexbundle_js);
//console.log(cache);
/// add cachebust hashes (disabled, seems unsupported, errors on install)
// cache = cache.replace(/abridge\.css/i,'abridge.css?h='+h_abridge_css);
// cache = cache.replace(/abridge\.min\.js/i,'abridge.min.js?h='+h_abridge_js);
// cache = cache.replace(/abridge_nosearch\.min\.js/i,'abridge_nosearch.min.js?h='+h_abridge_nosearch_js);
// cache = cache.replace(/katexbundle\.min\.js/i,'katexbundle.min.js?h='+h_katexbundle_js);
// console.log(cache);

// update the BASE_CACHE_FILES variable in the sw.js service worker file
results = replace.sync({
Expand Down Expand Up @@ -237,8 +246,8 @@ if (bpath === '') {// abridge used directly
minify(['static/sw.js']);
}

// Minify the json manifest
if (fs.existsSync('static/manifest.json')) {// if manifest.json is present, then minify it.
// if manifest.json is present, then minify it.
if (fs.existsSync('static/manifest.json')) {
let out;
try {
out = JSON.minify(fs.readFileSync('static/manifest.json', {encoding:"utf-8"}));
Expand Down
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.

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

navigator.serviceWorker
.ready
.then(() => {
//console.info("SW Ready");
console.info("SW Ready");
});
}
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 c44af9d

Please sign in to comment.