Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone): content-hash built chunks so a deploy switches over atomically

Elian Doran7a9adcb83f55
diff --git a/apps/standalone/public/_headers b/apps/standalone/public/_headers
index 190ae031ef..9796b6a30d 100644
--- a/apps/standalone/public/_headers
+++ b/apps/standalone/public/_headers
@@ -1,2 +1,13 @@
/*
- Cross-Origin-Opener-Policy: same-origin
\ No newline at end of file
+ Cross-Origin-Opener-Policy: same-origin
+
+# Everything under src/ is content-hashed by the build (see vite.config.mts), so a given
+# URL never changes meaning and may be cached indefinitely. index.html is left on the
+# host's default (revalidate-always) so a deployment is picked up immediately.
+/src/*
+ Cache-Control: public, max-age=31536000, immutable
+
+# The service worker keeps a stable URL, so it must never be served stale: a cached copy
+# would keep an outdated worker in control of the page after a deployment.
+/sw.js
+ Cache-Control: public, max-age=0, must-revalidate
diff --git a/apps/standalone/vite.config.mts b/apps/standalone/vite.config.mts
index 999ce5ab0a..bab636e230 100644
--- a/apps/standalone/vite.config.mts
+++ b/apps/standalone/vite.config.mts
@@ -337,15 +337,22 @@ export default defineConfig(() => ({
'local-bridge': join(__dirname, 'src', 'local-bridge.ts'),
},
output: {
+ // Everything below `src/` carries a content hash so a deployment switches over
+ // atomically: `index.html` is served uncached and can only ever reference chunks
+ // from its own build. Without the hash, a browser holding a still-fresh copy of
+ // one chunk mixes it with newly fetched ones — and since the minifier reassigns
+ // single-letter export names every build, a cross-build import silently binds to
+ // the wrong value ("X is not a function") until the cache expires.
entryFileNames: (chunkInfo) => {
- // Service worker and other workers should be at root level
- if (chunkInfo.name === 'sw') {
- return '[name].js';
+ // The service worker must keep a stable URL: `main.ts` registers it as
+ // `./sw.js`, and a hash would orphan the previously registered worker.
+ if (chunkInfo.name === "sw") {
+ return "[name].js";
}
- return 'src/[name].js';
+ return "src/[name]-[hash].js";
},
- chunkFileNames: "src/[name].js",
- assetFileNames: "src/[name].[ext]"
+ chunkFileNames: "src/[name]-[hash].js",
+ assetFileNames: "src/[name]-[hash].[ext]"
}
}
},