diff --git a/src/contentScript/contentScript.ts b/src/contentScript/contentScript.ts index b76d6f9324..732e48a544 100644 --- a/src/contentScript/contentScript.ts +++ b/src/contentScript/contentScript.ts @@ -15,9 +15,10 @@ * along with this program. If not, see . */ +// IMPORTANT: do not import anything that has a transitive dependency of the messenger. +// See for more information: https://github.com/pixiebrix/pixiebrix-extension/issues/4058 import "./contentScript.scss"; import { uuidv4 } from "@/types/helpers"; -import { onContextInvalidated } from "@/errors/contextInvalidated"; import { isInstalledInThisSession, isReadyInThisDocument, @@ -47,17 +48,12 @@ async function initContentScript() { setInstalledInThisSession(); - // eslint-disable-next-line promise/prefer-await-to-then -- It's an unrelated event listener - void onContextInvalidated().then(() => { - console.debug("contentScript: invalidated", uuid); - }); - // Keeping the import separate ensures that no side effects are run until this point const { init } = await logPromiseDuration( "contentScript: imported", // "imported" timing includes the parsing of the file, which can take 500-1000ms import(/* webpackChunkName: "contentScriptCore" */ "./contentScriptCore") ); - await init(); + await init(uuid); setReadyInThisDocument(uuid); } diff --git a/src/contentScript/contentScriptCore.ts b/src/contentScript/contentScriptCore.ts index 74405e9233..7ea6272776 100644 --- a/src/contentScript/contentScriptCore.ts +++ b/src/contentScript/contentScriptCore.ts @@ -36,8 +36,10 @@ import { initPartnerIntegrations } from "@/contentScript/partnerIntegrations"; import { isContextInvalidatedError, notifyContextInvalidated, + onContextInvalidated, } from "@/errors/contextInvalidated"; import { uncaughtErrorHandlers } from "@/telemetry/reportUncaughtErrors"; +import { UUID } from "@/core"; function ignoreContextInvalidatedErrors( errorEvent: ErrorEvent | PromiseRejectionEvent @@ -51,7 +53,7 @@ function ignoreContextInvalidatedErrors( // Must come before the default handler for ignoring errors. Otherwise, this handler might not be run uncaughtErrorHandlers.unshift(ignoreContextInvalidatedErrors); -export async function init(): Promise { +export async function init(uuid: UUID): Promise { registerMessenger(); registerExternalMessenger(); registerBuiltinBlocks(); @@ -72,4 +74,11 @@ export async function init(): Promise { // Let the partner page know initPartnerIntegrations(); + + // Put here instead of in the sync contentScript because contextInvalidated has a transitive dependency on the + // messenger (which causes a race on messenger initialization) + // eslint-disable-next-line promise/prefer-await-to-then -- It's an unrelated event listener + void onContextInvalidated().then(() => { + console.debug("contentScript: invalidated", uuid); + }); }