diff --git a/src/sandbox/patchers/dynamicAppend/common.ts b/src/sandbox/patchers/dynamicAppend/common.ts index 22fe001b5..eea4acb11 100644 --- a/src/sandbox/patchers/dynamicAppend/common.ts +++ b/src/sandbox/patchers/dynamicAppend/common.ts @@ -22,12 +22,12 @@ type DynamicDomMutationTarget = 'head' | 'body'; declare global { interface HTMLLinkElement { [styleElementTargetSymbol]: DynamicDomMutationTarget; - [styleElementRefNodeNo]?: number; + [styleElementRefNodeNo]?: Exclude; } interface HTMLStyleElement { [styleElementTargetSymbol]: DynamicDomMutationTarget; - [styleElementRefNodeNo]?: number; + [styleElementRefNodeNo]?: Exclude; } interface Function { diff --git a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts index 4d8622dfd..b86abbe8d 100644 --- a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts +++ b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts @@ -284,11 +284,10 @@ export function patchStrictSandbox( const refNo = stylesheetElement[styleElementRefNodeNo]; if (typeof refNo === 'number' && refNo !== -1) { - const refNode = mountDom.childNodes[refNo]; - if (refNode) { - rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode); - return true; - } + // the reference node may be dynamic script comment which is not rebuilt while remounting thus reference node no longer exists + const refNode = mountDom.childNodes[refNo] || null; + rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode); + return true; } else { rawHeadAppendChild.call(mountDom, stylesheetElement); return true;