add cache-busting

This commit is contained in:
Nystik
2026-03-22 18:50:23 +01:00
parent 1a0d749c68
commit 50f0263165
3 changed files with 44 additions and 4 deletions

View File

@@ -6,6 +6,7 @@
const fs = require("fs");
const path = require("path");
const { getVersion } = require("../server/version");
const asarDir = process.argv[2];
if (!asarDir) {
@@ -13,7 +14,7 @@ if (!asarDir) {
process.exit(1);
}
function patchHtml(filePath) {
function patchHtml(filePath, version) {
const backupPath = filePath + ".orig";
if (!fs.existsSync(filePath) && !fs.existsSync(backupPath)) {
@@ -46,8 +47,8 @@ function patchHtml(filePath) {
// Inject ignis scripts before the first <script> tag
html = html.replace(
'<script type="text/javascript"',
'<script type="text/javascript" src="ignis-ui.js"></script>\n' +
'<script type="text/javascript" src="shim-loader.js"></script>\n' +
`<script type="text/javascript" src="ignis-ui.js?v=${version}"></script>\n` +
`<script type="text/javascript" src="shim-loader.js?v=${version}"></script>\n` +
'<script type="text/javascript"',
);
@@ -55,4 +56,6 @@ function patchHtml(filePath) {
console.log(`[patch] Patched ${filePath}`);
}
patchHtml(path.join(asarDir, "index.html"));
const version = getVersion();
patchHtml(path.join(asarDir, "index.html"), version);
console.log(`[patch] Injected version: ${version}`);

View File

@@ -72,6 +72,20 @@ app.use("/vault-files", (req, res, next) => {
express.static(vaultPath)(req, res, next);
});
// Serve dist files with cache headers based on version param
app.use((req, res, next) => {
if (req.path.match(/\/(ignis-ui|shim-loader)\.js$/)) {
if (req.query.v) {
// Versioned assets - cache for 1 year
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
} else {
// No version param - short cache for dev/fallback
res.setHeader("Cache-Control", "public, max-age=300");
}
}
next();
});
app.use(express.static(path.join(__dirname, "..", "dist")));
app.use(express.static(config.obsidianAssetsPath));

23
server/version.js Normal file
View File

@@ -0,0 +1,23 @@
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
function getVersion() {
const pkg = JSON.parse(
fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf-8"),
);
const semver = pkg.version;
let hash;
try {
hash = execSync("git rev-parse --short=7 HEAD", {
encoding: "utf-8",
}).trim();
} catch (e) {
hash = Date.now().toString(36).slice(-7);
}
return `${semver}-${hash}`;
}
module.exports = { getVersion };