Skip to content

Commit

Permalink
Pagefind PWA fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jieiku committed Aug 9, 2024
1 parent 63e4ecd commit 4e062af
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 161 deletions.
144 changes: 119 additions & 25 deletions package_abridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ bpath = '';
if (fs.existsSync('./themes')) {
bpath = 'themes/abridge/';
}
// cleanup pagefind files from old builds.
_rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/);
_rmRecursive(path.join(bpath, "static/js/index"));
_rmRecursive(path.join(bpath, "static/js/fragment"));

async function execWrapper(cmd) {
const { stdout, stderr } = await execPromise(cmd);
Expand All @@ -54,10 +60,10 @@ async function execWrapper(cmd) {
async function abridge() {
await sync();
const { replaceInFileSync } = await import('replace-in-file');
// set index_format for chosen search_library accordingly.
if (search_library === 'offline') {
replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\""});
//replaceInFileSync({files: 'config.toml', from: /base_url.*=.*/g, to: "base_url = \""+__dirname+"\/public\""});
args = args + " -u \""+__dirname+"\/public\""
args = args + " -u \""+__dirname+"\/public\""//set base_url to the path on disk for offline site.
} else if (search_library === 'elasticlunrjava') {
replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\""});
} else if (search_library === 'elasticlunr') {
Expand Down Expand Up @@ -85,24 +91,20 @@ async function abridge() {
}

if (search_library === 'pagefind') {
// Run the pagefind script to generate the index files.
// Has to happen at start otherwise, it happens too late asyncronously.
const createIndex = require('./static/js/pagefind.index.cjs'); // run the pagefind index.js script
await createIndex(); // makes program wait for pagefind build execution
} else {
_rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/);
_rmRecursive(path.join(bpath, "static/js/index"));
_rmRecursive(path.join(bpath, "static/js/fragment"));
// Generate pagefind index at start, otherwise it happens too late asyncronously.
await createPagefindIndex(); // makes program wait for pagefind build execution
_rmRegex(path.join(bpath, "static/js/"),/^pagefind\.js$/);//pagefind temporary intermediate files
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.js$/);//pagefind temporary intermediate files
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.css$/);//pagefind temporary intermediate files

//copy to public so the files are included in the PWA cache list if necessary.
_cpRegex(path.join(bpath, "static/js/"),path.join(bpath, "public/js/"),/^pagefind-entry\.json$/);
_cpRegex(path.join(bpath, "static/js/"),path.join(bpath, "public/js/"),/^pagefind.*pf_meta$/);
_cpRegex(path.join(bpath, "static/js/"),path.join(bpath, "public/js/"),/^wasm.*pagefind$/);
_cpRecursive(path.join(bpath, "static/js/index"),path.join(bpath, "public/js/index"));
_cpRecursive(path.join(bpath, "static/js/fragment"),path.join(bpath, "public/js/fragment"));
}

// cleanup
_rmRegex(path.join(bpath, "static/js/"),/^pagefind\.js$/);//pagefind temporary intermediate files
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.js$/);//pagefind temporary intermediate files
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.css$/);//pagefind temporary intermediate files


if (pwa) {// Update pwa settings, file list, and hashes.
if (typeof pwa_VER !== 'undefined' && typeof pwa_NORM_TTL !== 'undefined' && typeof pwa_LONG_TTL !== 'undefined' && typeof pwa_TTL_NORM !== 'undefined' && typeof pwa_TTL_LONG !== 'undefined' && typeof pwa_TTL_EXEMPT !== 'undefined') {
// update from abridge theme.
Expand Down Expand Up @@ -239,6 +241,14 @@ function _rmRecursive(targetFiles) {
}
}

function _cpRecursive(source,dest) {
try {
fs.cpSync(source, dest, { recursive: true });
} catch (error) {
console.error("An error occurred:", error);
}
}

function _rmRegex(path,regex) {
try {
fs.readdirSync(path).filter(f => regex.test(f)).forEach(f => fs.unlinkSync(path + f));
Expand All @@ -249,6 +259,15 @@ function _rmRegex(path,regex) {
}
}

function _cpRegex(source,dest,regex) {
try {
fs.readdirSync(source).filter(f => regex.test(f)).forEach(f => fs.copyFileSync(source + f, dest + f));
//fs.copyFileSync(path.join(bpath, "static/js/pagefind-entry.json"), 'public/pagefind-entry.json');
} catch (error) {
console.error("An error occurred:", error);
}
}

function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa) {
minify_files = [];

Expand Down Expand Up @@ -328,13 +347,7 @@ if (args === ' offline') {
searchChange('offline');
} else if (args === ' elasticlunrjava') {
searchChange('elasticlunrjava');
} else if (args === ' elasticlunr') {// Zola default search_library
// I should not need to do cleanup here as well... but this is what works for now to keep the repo clean.
_rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/);
_rmRecursive(path.join(bpath, "static/js/index"));
_rmRecursive(path.join(bpath, "static/js/fragment"));
} else if (args === ' elasticlunr') {
searchChange('elasticlunr');
} else if (args === ' pagefind') {
searchChange('pagefind');
Expand All @@ -344,6 +357,87 @@ if (args === ' offline') {
abridge();
}

async function createPagefindIndex() {
console.log("Creating Pagefind index...");
const pagefind = await import("pagefind");// Dynamically import the pagefind module
const publicFolder = path.join(__dirname, "public");
const files = fs.readdirSync(publicFolder);
let langArray = [];

files.forEach((file) => {
if (file.startsWith("search_index")) {
langArray.push(file.split(".")[1]);
}
});

const { index } = await pagefind.createIndex();
// Assuming index, fs, and path are already defined and properly imported

// Convert each lang in langArray to a promise that performs the desired operations
const promises = langArray.map((lang) =>
(async () => {
const filePath = path.join(__dirname,"public/search_index." + lang + ".json");

// Read the file content synchronously (consider using async readFile for better performance)
const fileContent = fs.readFileSync(filePath);
const data = JSON.parse(fileContent);

// Add each record to the index asynchronously
for (const record of data) {
await index.addCustomRecord({
url: record.url,
content: record.body,
language: lang,
meta: {
title: record.title,
description: record.meta,
},
});
}
})()
);

// Execute all promises concurrently
await Promise.all(promises)
.then(async () => {
// Write the index files to disk
const { errors } = await index.writeFiles({
outputPath: "./static/js/",
});
if (errors.length > 0) {
console.log("Errors: ", errors);
}
})
.then(async () => {
// Edit the pagefind to convert from MJS to CJS
const pagefindPath = path.join(__dirname, "static/js/pagefind.js");//source pagefind from node module
let pagefindContent = fs.readFileSync(pagefindPath, "utf8");
// Remove 'import.meta.url' from the pagefind file and exports
pagefindContent = pagefindContent
.replace(
/initPrimary\(\)\{([^{}]*\{[^{}]*\})*[^{}]*\}/g,
`initPrimary(){}`
) // Remove annoying function
.replace(/;export\{[^}]*\}/g, "");
fs.writeFileSync(pagefindPath, pagefindContent);

// now insert the CJS into the anonymous function within pagefind.search.js
const pagefind_searchPath = path.join(__dirname, "static/js/pagefind.search.js");//file to insert into
const search_pagefindPath = path.join(__dirname, "static/js/pagefind_search.js");//output
let pagefind_searchContent = fs.readFileSync(pagefind_searchPath, "utf8");
// Now insert into pagefind.search.js at this location: //insertHere
pagefind_searchContent = pagefind_searchContent.replace(/\/\/insertHere/g, pagefindContent);
fs.writeFileSync(search_pagefindPath, pagefind_searchContent);

})
.then(async () => {
await pagefind.close();
})
.catch((error) => {
console.error("An error occurred:", error);
});
}

async function sync() {
// Check if the submodule is present, if not skip entire function
if (!fs.existsSync(path.join(__dirname, "themes/abridge"))) {
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.

130 changes: 0 additions & 130 deletions static/js/pagefind.index.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion static/js/sw_load.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/sw.min.js?v=3.12.0",
.register("/sw.min.js?v=3.12.1",
{ scope: "/" })
.then(() => {
console.info("SW Loaded");
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 4e062af

Please sign in to comment.