diff --git a/.eslintrc.js b/.eslintrc.js index 3817d7b1..5341c4e0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,6 +15,7 @@ module.exports = { "no-unused-vars": "warn" }, "globals": { - "HYDROGEN_VERSION": "readonly" + "HYDROGEN_VERSION": "readonly", + "HYDROGEN_GLOBAL_HASH": "readonly" } }; diff --git a/scripts/build-plugins/service-worker.js b/scripts/build-plugins/service-worker.js index e7a77048..541fdcad 100644 --- a/scripts/build-plugins/service-worker.js +++ b/scripts/build-plugins/service-worker.js @@ -8,7 +8,7 @@ function contentHash(str) { return hasher.digest(); } -module.exports = function injectServiceWorker(swFile, otherUncachedFiles) { +module.exports = function injectServiceWorker(swFile, otherUncachedFiles, globalHashChunkReplaceMap) { const swName = path.basename(swFile); let root; let version; @@ -43,6 +43,7 @@ module.exports = function injectServiceWorker(swFile, otherUncachedFiles) { const globalHash = getBuildHash(cachedFileNames, uncachedFileContentMap); const sw = bundle[swName]; sw.code = replaceConstsInServiceWorker(sw.code, version, globalHash, assets); + replaceGlobalHashPlaceholderInChunks(assets, globalHashChunkReplaceMap, globalHash); console.log(`\nBuilt ${version} (${globalHash})`); } }; @@ -115,3 +116,14 @@ function replaceConstsInServiceWorker(swSource, version, globalHash, assets) { swSource = replaceStringInSource("NOTIFICATION_BADGE_ICON", assets.find(a => a.name === "icon.png").fileName); return swSource; } + +function replaceGlobalHashPlaceholderInChunks(assets, globalHashChunkReplaceMap, globalHash) { + for (const [name, placeholder] of Object.entries(globalHashChunkReplaceMap)) { + const chunk = assets.find(a => a.type === "chunk" && a.name === name); + if (!chunk) { + throw new Error(`could not find chunk ${name} to replace global hash placeholder`); + } + console.log(placeholder, globalHash); + chunk.code = chunk.code.replaceAll(placeholder, `"${globalHash}"`); + } +} diff --git a/src/platform/web/ui/login/common.js b/src/platform/web/ui/login/common.js index 73da92c9..a247a2ba 100644 --- a/src/platform/web/ui/login/common.js +++ b/src/platform/web/ui/login/common.js @@ -15,10 +15,10 @@ limitations under the License. */ export function hydrogenGithubLink(t) { - if (HYDROGEN_VERSION) { + if (HYDROGEN_VERSION && HYDROGEN_GLOBAL_HASH) { return t.a({target: "_blank", href: `https://github.com/vector-im/hydrogen-web/releases/tag/v${HYDROGEN_VERSION}`}, - `Hydrogen v${HYDROGEN_VERSION} (${window.HYDROGEN_GLOBAL_HASH}) on Github`); + `Hydrogen v${HYDROGEN_VERSION} (${HYDROGEN_GLOBAL_HASH}) on Github`); } else { return t.a({target: "_blank", href: "https://github.com/vector-im/hydrogen-web"}, "Hydrogen on Github"); diff --git a/vite.config.js b/vite.config.js index ef41babf..9a4e66f9 100644 --- a/vite.config.js +++ b/vite.config.js @@ -4,6 +4,7 @@ const flexbugsFixes = require("postcss-flexbugs-fixes"); const fs = require("fs"); const path = require("path"); +const GLOBAL_HASH_PLACEHOLDER = "hydrogen-global-hash-placeholder-4cf32306-5d61-4262-9a57-c9983f472c3c"; const injectWebManifest = require("./scripts/build-plugins/manifest"); const injectServiceWorker = require("./scripts/build-plugins/service-worker"); @@ -50,10 +51,14 @@ export default { // important this comes before service worker // otherwise the manifest and the icons it refers to won't be cached injectWebManifest("assets/manifest.json"), - injectServiceWorker("./src/platform/web/sw.js", ["index.html"]), + injectServiceWorker("./src/platform/web/sw.js", ["index.html"], { + // replace global hash placeholder in index chunk + "index": JSON.stringify(GLOBAL_HASH_PLACEHOLDER) + }), ], define: { - "HYDROGEN_VERSION": JSON.stringify(version) + "HYDROGEN_VERSION": JSON.stringify(version), + "HYDROGEN_GLOBAL_HASH": JSON.stringify(GLOBAL_HASH_PLACEHOLDER) }, css: { postcss: {