Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 keep dynamic stylesheet inserted order by insertBefore #2574

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/sandbox/patchers/dynamicAppend/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ const LINK_TAG_NAME = 'LINK';
const STYLE_TAG_NAME = 'STYLE';

export const styleElementTargetSymbol = Symbol('target');
export const styleElementRefNodeNo = Symbol('refNodeNo');
const overwrittenSymbol = Symbol('qiankun-overwritten');

type DynamicDomMutationTarget = 'head' | 'body';

declare global {
interface HTMLLinkElement {
[styleElementTargetSymbol]: DynamicDomMutationTarget;
[styleElementRefNodeNo]?: number;
}

interface HTMLStyleElement {
[styleElementTargetSymbol]: DynamicDomMutationTarget;
[styleElementRefNodeNo]?: number;
}

interface Function {
Expand Down Expand Up @@ -156,6 +159,15 @@ function convertLinkAsStyle(
return styleElement;
}

const defineNonEnumerableProperty = (target: any, key: string | symbol, value: any) => {
Object.defineProperty(target, key, {
configurable: true,
enumerable: false,
writable: true,
value,
});
};

const styledComponentCSSRulesMap = new WeakMap<HTMLStyleElement, CSSRuleList>();
const dynamicScriptAttachedCommentMap = new WeakMap<HTMLScriptElement, Comment>();
const dynamicLinkAttachedInlineStyleMap = new WeakMap<HTMLLinkElement, HTMLStyleElement>();
Expand Down Expand Up @@ -230,11 +242,7 @@ function getOverwrittenAppendChildOrInsertBefore(opts: {
return rawDOMAppendOrInsertBefore.call(this, element, refChild) as T;
}

Object.defineProperty(stylesheetElement, styleElementTargetSymbol, {
value: target,
writable: true,
configurable: true,
});
defineNonEnumerableProperty(stylesheetElement, styleElementTargetSymbol, target);

const appWrapper = appWrapperGetter();

Expand Down Expand Up @@ -262,9 +270,23 @@ function getOverwrittenAppendChildOrInsertBefore(opts: {

const mountDOM = target === 'head' ? getAppWrapperHeadElement(appWrapper) : appWrapper;

dynamicStyleSheetElements.push(stylesheetElement);
const referenceNode = mountDOM.contains(refChild) ? refChild : null;
return rawDOMAppendOrInsertBefore.call(mountDOM, stylesheetElement, referenceNode);

let refNo: number | undefined;
if (referenceNode) {
refNo = Array.from(mountDOM.childNodes).indexOf(referenceNode);
}

const result = rawDOMAppendOrInsertBefore.call(mountDOM, stylesheetElement, referenceNode);

// record refNo thus we can keep order while remounting
if (typeof refNo === 'number' && refNo !== -1) {
defineNonEnumerableProperty(stylesheetElement, styleElementRefNodeNo, refNo);
}
// record dynamic style elements after insert succeed
dynamicStyleSheetElements.push(stylesheetElement);

return result as T;
}

case SCRIPT_TAG_NAME: {
Expand Down
16 changes: 14 additions & 2 deletions src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
patchHTMLDynamicAppendPrototypeFunctions,
rebuildCSSRules,
recordStyledComponentsCSSRules,
styleElementRefNodeNo,
styleElementTargetSymbol,
} from './common';

Expand All @@ -35,6 +36,7 @@ Object.defineProperty(nativeGlobal, '__currentLockingSandbox__', {
});

const rawHeadAppendChild = HTMLHeadElement.prototype.appendChild;
const rawHeadInsertBefore = HTMLHeadElement.prototype.insertBefore;

// Share proxyAttachContainerConfigMap between multiple qiankun instance, thus they could access the same record
nativeGlobal.__proxyAttachContainerConfigMap__ =
Expand Down Expand Up @@ -279,8 +281,18 @@ export function patchStrictSandbox(
if (!appWrapper.contains(stylesheetElement)) {
const mountDom =
stylesheetElement[styleElementTargetSymbol] === 'head' ? getAppWrapperHeadElement(appWrapper) : appWrapper;
rawHeadAppendChild.call(mountDom, stylesheetElement);
return true;
const refNo = stylesheetElement[styleElementRefNodeNo];

if (typeof refNo === 'number' && refNo !== -1) {
const refNode = mountDom.childNodes[refNo];
if (refNode) {
rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode);
return true;
}
} else {
rawHeadAppendChild.call(mountDom, stylesheetElement);
return true;
}
}

return false;
Expand Down