Skip to content

Commit

Permalink
✨ add internal force cache config (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos committed Oct 22, 2020
1 parent bcd5f27 commit 42bdb04
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
5 changes: 4 additions & 1 deletion examples/main/multiple.js
Expand Up @@ -3,7 +3,10 @@ import { loadMicroApp } from '../../es';
let app;

function mount() {
app = loadMicroApp({ name: 'react15', entry: '//localhost:7102', container: '#react15' });
app = loadMicroApp(
{ name: 'react15', entry: '//localhost:7102', container: '#react15' },
{ sandbox: { experimentalStyleIsolation: true } },
);
}

function unmount() {
Expand Down
37 changes: 25 additions & 12 deletions src/apis.ts
Expand Up @@ -44,7 +44,7 @@ export function registerMicroApps<T extends object = {}>(
});
}

const appConfigPormiseGetterMap = new Map<string, Promise<ParcelConfigObjectGetter>>();
const appConfigPromiseGetterMap = new Map<string, Promise<ParcelConfigObjectGetter>>();

export function loadMicroApp<T extends object = {}>(
app: LoadableApp<T>,
Expand All @@ -62,33 +62,46 @@ export function loadMicroApp<T extends object = {}>(
return undefined;
};

const wrapParcelConfigForRemount = (config: ParcelConfigObject): ParcelConfigObject => {
return {
...config,
// empty bootstrap hook which should not run twice while it calling from cached micro app
bootstrap: () => Promise.resolve(),
};
};

/**
* using name + container xpath as the micro app instance id,
* it means if you rendering a micro app to a dom which have been rendered before,
* the micro app would not load and evaluate its lifecycles again
*/
const memorizedLoadingFn = async (): Promise<ParcelConfigObject> => {
const { $$cacheLifecycleByAppName } = configuration ?? frameworkConfiguration;
const container = 'container' in app ? app.container : undefined;

if (container) {
// using appName as cache for internal experimental scenario
if ($$cacheLifecycleByAppName) {
const parcelConfigGetterPromise = appConfigPromiseGetterMap.get(name);
if (parcelConfigGetterPromise) return wrapParcelConfigForRemount((await parcelConfigGetterPromise)(container));
}

const xpath = getContainerXpath(container);
if (xpath) {
const parcelConfigGetterPromise = appConfigPormiseGetterMap.get(`${name}-${xpath}`);
if (parcelConfigGetterPromise) {
const parcelConfig = (await parcelConfigGetterPromise)(container);
return {
...parcelConfig,
// empty bootstrap hook which should not run twice while it calling from cached micro app
bootstrap: () => Promise.resolve(),
};
}
const parcelConfigGetterPromise = appConfigPromiseGetterMap.get(`${name}-${xpath}`);
if (parcelConfigGetterPromise) return wrapParcelConfigForRemount((await parcelConfigGetterPromise)(container));
}
}

const parcelConfigObjectGetterPromise = loadApp(app, configuration ?? frameworkConfiguration, lifeCycles);

if (container) {
const xpath = getContainerXpath(container);
if (xpath) appConfigPormiseGetterMap.set(`${name}-${xpath}`, parcelConfigObjectGetterPromise);
if ($$cacheLifecycleByAppName) {
appConfigPromiseGetterMap.set(name, parcelConfigObjectGetterPromise);
} else {
const xpath = getContainerXpath(container);
if (xpath) appConfigPromiseGetterMap.set(`${name}-${xpath}`, parcelConfigObjectGetterPromise);
}
}

return (await parcelConfigObjectGetterPromise)(container);
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces.ts
Expand Up @@ -54,6 +54,10 @@ export type PrefetchStrategy =
| ((apps: AppMetadata[]) => { criticalAppNames: string[]; minorAppsName: string[] });

type QiankunSpecialOpts = {
/**
* @deprecated internal api, don't used it as normal, might be removed after next version
*/
$$cacheLifecycleByAppName?: boolean;
prefetch?: PrefetchStrategy;
sandbox?:
| boolean
Expand Down
1 change: 0 additions & 1 deletion src/sandbox/patchers/dynamicAppend/common.ts
Expand Up @@ -353,7 +353,6 @@ export function rebuildCSSRules(
) {
styleSheetElements.forEach((stylesheetElement) => {
// re-append the dynamic stylesheet to sub-app container
// Using document.head.appendChild ensures that appendChild invocation can also directly use the HTMLHeadElement.prototype.appendChild method which is overwritten at mounting phase
reAppendElement(stylesheetElement);

/*
Expand Down

1 comment on commit 42bdb04

@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!

✅ Preview
https://qiankun-jc1z8zes1.vercel.app

Built with commit 42bdb04.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.