Skip to content

Commit

Permalink
🐛 fix the dynamic append script not be managed by qiankun while in si…
Browse files Browse the repository at this point in the history
…ngular mode (#682)
  • Loading branch information
kuitos committed Jun 20, 2020
1 parent 45ceb67 commit a0a0cda
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions src/sandbox/patchers/dynamicAppend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,29 @@ function getNewAppendChild(args: {
proxy = storedContainerInfo.proxy;
}

// have storedContainerInfo means it invoked by a micro app
const invokedByMicroApp = storedContainerInfo && !singular;

switch (element.tagName) {
case LINK_TAG_NAME:
case STYLE_TAG_NAME: {
const stylesheetElement: HTMLLinkElement | HTMLStyleElement = newChild as any;

if (invokedByMicroApp) {
// eslint-disable-next-line no-shadow
dynamicStyleSheetElements.push(stylesheetElement);
return rawAppendChild.call(appWrapperGetter(), stylesheetElement) as T;
}

// check if the currently specified application is active
const invokedByMicroApp = singular
? // check if the currently specified application is active
// While we switch page from qiankun app to a normal react routing page, the normal one may load stylesheet dynamically while page rendering,
// but the url change listener must to wait until the current call stack is flushed.
// This scenario may cause we record the stylesheet from react routing page dynamic injection,
// and remove them after the url change triggered and qiankun app is unmouting
// see https://github.com/ReactTraining/history/blob/master/modules/createHashHistory.js#L222-L230
const activated = checkActivityFunctions(window.location).some(name => name === appName);
// only hijack dynamic style injection when app activated
if (activated) {
dynamicStyleSheetElements.push(stylesheetElement);
checkActivityFunctions(window.location).some(name => name === appName)
: // have storedContainerInfo means it invoked by a micro app in multiply mode
!!storedContainerInfo;

return rawAppendChild.call(appWrapperGetter(), stylesheetElement) as T;
switch (element.tagName) {
case LINK_TAG_NAME:
case STYLE_TAG_NAME: {
const stylesheetElement: HTMLLinkElement | HTMLStyleElement = newChild as any;

if (!invokedByMicroApp) {
return headOrBodyAppendChild.call(this, element) as T;
}

return headOrBodyAppendChild.call(this, element) as T;
// eslint-disable-next-line no-shadow
dynamicStyleSheetElements.push(stylesheetElement);
return rawAppendChild.call(appWrapperGetter(), stylesheetElement) as T;
}

case SCRIPT_TAG_NAME: {
Expand Down Expand Up @@ -215,32 +209,25 @@ function getNewInsertBefore(args: {
dynamicStyleSheetElements = storedContainerInfo.dynamicStyleSheetElements;
}

// have storedContainerInfo means it invoked by a micro app
const invokedByMicroApp = storedContainerInfo && !singular;
const invokedByMicroApp = singular
? checkActivityFunctions(window.location).some(name => name === appName)
: !!storedContainerInfo;

switch (element.tagName) {
case LINK_TAG_NAME:
case STYLE_TAG_NAME: {
const stylesheetElement: HTMLLinkElement | HTMLStyleElement = newChild as any;

// have storedContainerInfo means it invoked by a micro app
if (invokedByMicroApp) {
// eslint-disable-next-line no-shadow
dynamicStyleSheetElements.push(stylesheetElement);
return rawAppendChild.call(appWrapperGetter(), stylesheetElement) as T;
}

const activated = checkActivityFunctions(window.location).some(name => name === appName);

if (activated) {
dynamicStyleSheetElements.push(stylesheetElement);
const wrapper = appWrapperGetter();
const referenceNode = wrapper.contains(refChild) ? refChild : null;
return rawInsertBefore.call(wrapper, stylesheetElement, referenceNode) as T;
if (!invokedByMicroApp) {
return headOrBodyInsertBefore.call(this, element, refChild) as T;
}

return headOrBodyInsertBefore.call(this, element, refChild) as T;
dynamicStyleSheetElements.push(stylesheetElement);
const wrapper = appWrapperGetter();
const referenceNode = wrapper.contains(refChild) ? refChild : null;
return rawInsertBefore.call(wrapper, stylesheetElement, referenceNode) as T;
}

case SCRIPT_TAG_NAME: {
if (!invokedByMicroApp) {
return headOrBodyInsertBefore.call(this, element, refChild) as T;
Expand Down

1 comment on commit a0a0cda

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for qiankun ready!

Built with commit a0a0cda

https://qiankun-cxomyz0wc.vercel.app

Please sign in to comment.