[go: nahoru, domu]

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

fix: when getOverwrittenAppendChildOrInsertBefore, will trigger page flashing Occasionally #2533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
fix: when getOverwrittenAppendChildOrInsertBefore, will trigger page …
…flashing Occasionally
  • Loading branch information
wuzequan committed Jun 14, 2023
commit 3ac619e9ee3e734efa1417d3a18f100216da11ec
3 changes: 3 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getContainer,
getDefaultTplWrapper,
getWrapperId,
isEnableProxyStyle,
isEnableScopedCSS,
performanceGetEntriesByName,
performanceMark,
Expand Down Expand Up @@ -285,6 +286,7 @@ export async function loadApp<T extends ObjectType>(
}

const scopedCSS = isEnableScopedCSS(sandbox);
const proxyCSS = isEnableProxyStyle(sandbox);
let initialAppWrapperElement: HTMLElement | null = createElement(
appContent,
strictStyleIsolation,
Expand Down Expand Up @@ -322,6 +324,7 @@ export async function loadApp<T extends ObjectType>(
// FIXME should use a strict sandbox logic while remount, see https://github.com/umijs/qiankun/issues/518
initialAppWrapperGetter,
scopedCSS,
proxyCSS,
useLooseSandbox,
excludeAssetFilter,
global,
Expand Down
12 changes: 11 additions & 1 deletion src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function createSandboxContainer(
appName: string,
elementGetter: () => HTMLElement | ShadowRoot,
scopedCSS: boolean,
proxyCSS: boolean,
useLooseSandbox?: boolean,
excludeAssetFilter?: (url: string) => boolean,
globalContext?: typeof window,
Expand All @@ -55,6 +56,7 @@ export function createSandboxContainer(
elementGetter,
sandbox,
scopedCSS,
proxyCSS,
excludeAssetFilter,
speedySandBox,
);
Expand Down Expand Up @@ -87,7 +89,15 @@ export function createSandboxContainer(

/* ------------------------------------------ 2. 开启全局变量补丁 ------------------------------------------*/
// render 沙箱启动时开始劫持各类全局监听,尽量不要在应用初始化阶段有 事件监听/定时器 等副作用
mountingFreers = patchAtMounting(appName, elementGetter, sandbox, scopedCSS, excludeAssetFilter, speedySandBox);
mountingFreers = patchAtMounting(
appName,
elementGetter,
sandbox,
scopedCSS,
proxyCSS,
excludeAssetFilter,
speedySandBox,
);

/* ------------------------------------------ 3. 重置一些初始化时的副作用 ------------------------------------------*/
// 存在 rebuilder 则表明有些副作用需要重建
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('forStrictSandbox test', () => {
active: noop,
inactive: noop,
};
patchStrictSandbox(appName, () => wrapper, sandbox, true, false, undefined, true);
patchStrictSandbox(appName, () => wrapper, sandbox, true, false, true, undefined, true);

expect(patchedDocument).toBeDefined();
expect(() => patchedDocument?.createTreeWalker(patchedDocument)).not.toThrow();
Expand Down
16 changes: 16 additions & 0 deletions src/sandbox/patchers/dynamicAppend/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export type ContainerConfig = {
dynamicStyleSheetElements: Array<HTMLStyleElement | HTMLLinkElement>;
appWrapperGetter: CallableFunction;
scopedCSS: boolean;
proxyCSS: boolean;
excludeAssetFilter?: CallableFunction;
};

Expand Down Expand Up @@ -218,6 +219,7 @@ function getOverwrittenAppendChildOrInsertBefore(opts: {
speedySandbox,
dynamicStyleSheetElements,
scopedCSS,
proxyCSS,
excludeAssetFilter,
} = containerConfig;

Expand Down Expand Up @@ -258,6 +260,20 @@ function getOverwrittenAppendChildOrInsertBefore(opts: {
} else {
css.process(appWrapper, stylesheetElement, appName);
}
} else if (proxyCSS) {
// exclude link elements like <link rel="icon" href="favicon.ico">
const linkElementUsingStylesheet =
element.tagName?.toUpperCase() === LINK_TAG_NAME &&
(element as HTMLLinkElement).rel === 'stylesheet' &&
(element as HTMLLinkElement).href;
if (linkElementUsingStylesheet) {
const fetch =
typeof frameworkConfiguration.fetch === 'function'
? frameworkConfiguration.fetch
: frameworkConfiguration.fetch?.fn;
stylesheetElement = convertLinkAsStyle(element, () => {}, fetch);
dynamicLinkAttachedInlineStyleMap.set(element, stylesheetElement);
}
}

const mountDOM = target === 'head' ? getAppWrapperHeadElement(appWrapper) : appWrapper;
Expand Down
2 changes: 2 additions & 0 deletions src/sandbox/patchers/dynamicAppend/forLooseSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function patchLooseSandbox(
sandbox: SandBox,
mounting = true,
scopedCSS = false,
proxyCSS = true,
excludeAssetFilter?: CallableFunction,
): Freer {
const { proxy } = sandbox;
Expand All @@ -53,6 +54,7 @@ export function patchLooseSandbox(
strictGlobal: false,
speedySandbox: false,
scopedCSS,
proxyCSS,
dynamicStyleSheetElements,
excludeAssetFilter,
}),
Expand Down
2 changes: 2 additions & 0 deletions src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export function patchStrictSandbox(
sandbox: SandBox,
mounting = true,
scopedCSS = false,
proxyCSS = true,
excludeAssetFilter?: CallableFunction,
speedySandbox = false,
): Freer {
Expand All @@ -242,6 +243,7 @@ export function patchStrictSandbox(
speedySandbox,
excludeAssetFilter,
scopedCSS,
proxyCSS,
};
proxyAttachContainerConfigMap.set(proxy, containerConfig);
}
Expand Down
34 changes: 28 additions & 6 deletions src/sandbox/patchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function patchAtMounting(
elementGetter: () => HTMLElement | ShadowRoot,
sandbox: SandBox,
scopedCSS: boolean,
proxyCSS: boolean,
excludeAssetFilter?: CallableFunction,
speedySandBox?: boolean,
): Freer[] {
Expand All @@ -28,15 +29,25 @@ export function patchAtMounting(
const patchersInSandbox = {
[SandBoxType.LegacyProxy]: [
...basePatchers,
() => patchLooseSandbox(appName, elementGetter, sandbox, true, scopedCSS, excludeAssetFilter),
() => patchLooseSandbox(appName, elementGetter, sandbox, true, scopedCSS, proxyCSS, excludeAssetFilter),
],
[SandBoxType.Proxy]: [
...basePatchers,
() => patchStrictSandbox(appName, elementGetter, sandbox, true, scopedCSS, excludeAssetFilter, speedySandBox),
() =>
patchStrictSandbox(
appName,
elementGetter,
sandbox,
true,
scopedCSS,
proxyCSS,
excludeAssetFilter,
speedySandBox,
),
],
[SandBoxType.Snapshot]: [
...basePatchers,
() => patchLooseSandbox(appName, elementGetter, sandbox, true, scopedCSS, excludeAssetFilter),
() => patchLooseSandbox(appName, elementGetter, sandbox, true, scopedCSS, proxyCSS, excludeAssetFilter),
],
};

Expand All @@ -48,18 +59,29 @@ export function patchAtBootstrapping(
elementGetter: () => HTMLElement | ShadowRoot,
sandbox: SandBox,
scopedCSS: boolean,
proxyCSS: boolean,
excludeAssetFilter?: CallableFunction,
speedySandBox?: boolean,
): Freer[] {
const patchersInSandbox = {
[SandBoxType.LegacyProxy]: [
() => patchLooseSandbox(appName, elementGetter, sandbox, false, scopedCSS, excludeAssetFilter),
() => patchLooseSandbox(appName, elementGetter, sandbox, false, scopedCSS, proxyCSS, excludeAssetFilter),
],
[SandBoxType.Proxy]: [
() => patchStrictSandbox(appName, elementGetter, sandbox, false, scopedCSS, excludeAssetFilter, speedySandBox),
() =>
patchStrictSandbox(
appName,
elementGetter,
sandbox,
false,
scopedCSS,
proxyCSS,
excludeAssetFilter,
speedySandBox,
),
],
[SandBoxType.Snapshot]: [
() => patchLooseSandbox(appName, elementGetter, sandbox, false, scopedCSS, excludeAssetFilter),
() => patchLooseSandbox(appName, elementGetter, sandbox, false, scopedCSS, proxyCSS, excludeAssetFilter),
],
};

Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ export function isEnableScopedCSS(sandbox: FrameworkConfiguration['sandbox']) {
return !!sandbox.experimentalStyleIsolation;
}

export function isEnableProxyStyle(sandbox: FrameworkConfiguration['sandbox']) {
if (typeof sandbox !== 'object') {
return false;
}

if (sandbox.strictStyleIsolation) {
return false;
}

return true;
}

/**
* copy from https://developer.mozilla.org/zh-CN/docs/Using_XPath
* @param el
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { version } from '../package.json';
export const version = '2.10.9';