chore(pdfjs): use version-based system for cache busting

This commit is contained in:
Elian Doran 2026-01-04 20:06:11 +02:00
parent 757fc7a7fe
commit 971d6ad9e3
No known key found for this signature in database
2 changed files with 27 additions and 4 deletions

View File

@ -3,6 +3,8 @@ import BuildHelper from "../../../scripts/build-utils";
import { build as esbuild } from "esbuild";
import { LOCALES } from "@triliumnext/commons";
import { watch } from "chokidar";
import { readFileSync, writeFileSync } from "fs";
import packageJson from "../package.json" with { type: "json "};
const build = new BuildHelper("packages/pdfjs-viewer");
const watchMode = process.argv.includes("--watch");
@ -16,6 +18,7 @@ async function main() {
for (const file of [ "viewer.css", "viewer.html", "viewer.mjs" ]) {
build.copy(`viewer/${file}`, `web/${file}`);
}
patchCacheBuster(`${build.outDir}/web/viewer.html`);
build.copy(`viewer/images`, `web/images`);
// Copy the custom files.
@ -60,6 +63,21 @@ async function rebuildCustomFiles() {
build.copy("src/custom.css", "web/custom.css");
}
function patchCacheBuster(htmlFilePath: string) {
const version = packageJson.version;
console.log(`Versioned URLs: ${version}.`)
let html = readFileSync(htmlFilePath, "utf-8");
html = html.replace(
`<link rel="stylesheet" href="custom.css" />`,
`<link rel="stylesheet" href="custom.css?v=${version}" />`);
html = html.replace(
`<script src="custom.mjs" type="module"></script>` ,
`<script src="custom.mjs?v=${version}" type="module"></script>`
);
writeFileSync(htmlFilePath, html);
}
function watchForChanges() {
console.log("Watching for changes in src directory...");
const watcher = watch(join(build.projectDir, "src"), {

View File

@ -11,9 +11,9 @@
*
*/
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import fs from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
function processVersion(version) {
// Remove the beta suffix if any.
@ -42,14 +42,19 @@ function patchPackageJson(packageJsonPath) {
function main() {
const scriptDir = dirname(fileURLToPath(import.meta.url));
const rootPackageJson = join(scriptDir, "..", "package.json");
patchPackageJson(rootPackageJson);
for (const app of ["server", "client"]) {
const appPackageJsonPath = join(scriptDir, "..", "apps", app, "package.json");
patchPackageJson(appPackageJsonPath);
}
for (const packageName of [ "pdfjs-viewer" ]) {
const packageJsonPath = join(scriptDir, "..", "packages", packageName, "package.json");
patchPackageJson(packageJsonPath);
}
}
main();