From 63ab7a0b0e53d0fcaada2536e7df040a44264d2f Mon Sep 17 00:00:00 2001 From: Aden Lindsay Date: Tue, 3 Nov 2026 08:38:02 +1030 Subject: [PATCH] fix: fix dev HMR edge case and guard main() call --- src/SEQTA.ts | 10 +++++- src/seqta/boot.ts | 50 ++++++++--------------------- src/seqta/devModuleGraphRecovery.ts | 22 +++++++++++++ 3 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 src/seqta/devModuleGraphRecovery.ts diff --git a/src/SEQTA.ts b/src/SEQTA.ts index 5a54e8bb..bd4a52f0 100644 --- a/src/SEQTA.ts +++ b/src/SEQTA.ts @@ -2,6 +2,8 @@ // is SEQTA and loads the rest of the extension on demand. Importing seqta/boot // statically here made every site download and parse the whole bundle. +import { recoverFromStaleDevModuleGraph } from "@/seqta/devModuleGraphRecovery"; + let hasSEQTAText = false; let isSeqtaPage = false; @@ -18,7 +20,7 @@ if (document.childNodes[1]) { } if (hasSEQTAText) { - void import("@/seqta/boot").then(({ boot }) => boot(isSeqtaPage)); + void import("@/seqta/boot").then(({ boot }) => boot()); } // Standalone 404 documents never pass the SEQTA SPA gate above. @@ -27,3 +29,9 @@ if (!isSeqtaPage) { bootErrorPage(), ); } + +if (import.meta.env.DEV) { + window.addEventListener("unhandledrejection", (event) => { + recoverFromStaleDevModuleGraph(event.reason); + }); +} diff --git a/src/seqta/boot.ts b/src/seqta/boot.ts index e25fda48..02c2a115 100644 --- a/src/seqta/boot.ts +++ b/src/seqta/boot.ts @@ -20,6 +20,7 @@ import { ensureKilledModeSettingsAccess, } from "@/seqta/utils/remoteConfig/runtime"; import { runStartupPopupQueue } from "@/seqta/utils/Openers/StartupPopupQueue"; +import { recoverFromStaleDevModuleGraph } from "@/seqta/devModuleGraphRecovery"; function registerFetchSeqtaAppLinkListener() { browser.runtime.onMessage.addListener((request, _sender, sendResponse) => { @@ -47,24 +48,24 @@ function registerFetchSeqtaAppLinkListener() { var IsSEQTAPage = false; -/** - * Loaded on demand by SEQTA.ts for pages with the SEQTA copyright comment. - * `isSeqtaPage` is true when the title also matched SEQTA Learn/Engage. - */ -export function boot(isSeqtaPage: boolean) { +/** Loaded on demand by SEQTA.ts for pages with the SEQTA copyright comment. */ +export function boot() { installSeqtaMenuColourPatch(); installThemeImagePagePatch(); - if (isSeqtaPage) init(); -} - -if (import.meta.env.DEV) { - window.addEventListener("unhandledrejection", (event) => { - recoverFromStaleDevModuleGraph(event.reason); - }); + void init(); } async function init() { - if (!IsSEQTAPage) { + const hasSEQTAText = + document.childNodes[1]?.textContent?.includes( + "Copyright (c) SEQTA Software", + ) ?? false; + if ( + hasSEQTAText && + (document.title.includes("SEQTA Learn") || + document.title.includes("SEQTA Engage")) && + !IsSEQTAPage + ) { IsSEQTAPage = true; verboseInfo("[BetterSEQTA+] Verified SEQTA Page"); @@ -159,26 +160,3 @@ function replaceIcons() { } }); } - -/** Vite/CRX HMR can leave modules with missing named exports until the graph is cleared. */ -function recoverFromStaleDevModuleGraph(error: unknown) { - if (!import.meta.env.DEV) return; - - const message = error instanceof Error ? error.message : String(error); - if (!/does not provide an export named/.test(message)) return; - - const key = "bsplus-dev-export-recovery"; - if (!sessionStorage.getItem(key)) { - sessionStorage.setItem(key, "1"); - import.meta.hot?.send("bsplus:reset-module-graph"); - setTimeout(() => { - location.reload(); - }, 80); - return; - } - - sessionStorage.removeItem(key); - console.error( - "[BetterSEQTA+] Dev module graph is still stale after recovery. Restart `npm run dev`, then reload this page.", - ); -} diff --git a/src/seqta/devModuleGraphRecovery.ts b/src/seqta/devModuleGraphRecovery.ts new file mode 100644 index 00000000..18371413 --- /dev/null +++ b/src/seqta/devModuleGraphRecovery.ts @@ -0,0 +1,22 @@ +/** Vite/CRX HMR can leave modules with missing named exports until the graph is cleared. */ +export function recoverFromStaleDevModuleGraph(error: unknown) { + if (!import.meta.env.DEV) return; + + const message = error instanceof Error ? error.message : String(error); + if (!/does not provide an export named/.test(message)) return; + + const key = "bsplus-dev-export-recovery"; + if (!sessionStorage.getItem(key)) { + sessionStorage.setItem(key, "1"); + import.meta.hot?.send("bsplus:reset-module-graph"); + setTimeout(() => { + location.reload(); + }, 80); + return; + } + + sessionStorage.removeItem(key); + console.error( + "[BetterSEQTA+] Dev module graph is still stale after recovery. Restart `npm run dev`, then reload this page.", + ); +}