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

feat(option): loadMicroApp 增加 disableCache 配置项 #2329

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
2 changes: 2 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ By linking the micro-application to some url rules, the function of automaticall

- excludeAssetFilter - `(assetUrl: string) => boolean` - optional,some special dynamic loaded micro app resources should not be handled by qiankun hijacking

- disableCache - `boolean` - optional,qiankun will not load resources and excute code repeatedly when loading the same micro app multiple times, so it can improve performace and avoid the risk of memory leaks, default is `false`.

- Usage

Start qiankun.
Expand Down
2 changes: 2 additions & 0 deletions docs/api/README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ toc: menu

- excludeAssetFilter - `(assetUrl: string) => boolean` - 可选,指定部分特殊的动态加载的微应用资源(css/js) 不被 qiankun 劫持处理

- disableCache - `boolean` - 可选,是否禁用缓存,在多次加载相同微应用的时候,qiankun 不会重复加载资源和执行代码,这样可以提升性能和避免内存泄漏的风险,默认为 `false`

- 返回值 - `MicroApp` - 微应用实例

- mount(): Promise<null>;
Expand Down
28 changes: 28 additions & 0 deletions docs/faq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,31 @@ As the requests to pull micro-app entry are all cross-domain, when your micro-ap
},
});
```

## Why bootstrap hook only run once when loading the same micro app multi times by using loadMicroApp method?

By default,to improve performace and avoid the risk of memory leaks, qiankun will not load resources and excute code repeatedly when loading the same micro app multiple times, so it will skip bootstrap hook, and remount the micro app directly.

You can disable cache to solve the problem, however, be ware this method may cause performance and memory problems.

```js
import { loadMicroApp } from 'qiankun';

loadMicroApp(app, {
disableCache: true
});
```

## Why global data being cached when loading the same micro app multi times by using loadMicroApp method?

By default,to improve performace and avoid the risk of memory leaks, qiankun will not load resources and excute code repeatedly when loading the same micro app multiple times, so it will reuse the global data cache of last time.

You can disable cache to solve the problem, however, be ware this method may cause performance and memory problems.

```js
import { loadMicroApp } from 'qiankun';

loadMicroApp(app, {
disableCache: true
});
```
28 changes: 28 additions & 0 deletions docs/faq/README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,31 @@ export async function mount(props) {
}
}
```

## 为什么使用 loadMicroApp 多次加载相同微应用时,bootstrap 方法只会执行一次?

默认情况下,为了提升性能和避免内存泄漏的风险,在多次加载相同微应用的时候,qiankun 不会重复加载资源和执行代码,因此会跳过 bootstrap 钩子,直接 mount 微应用。

可以通过禁用缓存解决,但需要注意的是该方法可能会导致性能和内存的问题。

```js
import { loadMicroApp } from 'qiankun';

loadMicroApp(app, {
disableCache: true, // 禁用缓存
});
```

## 为什么使用 loadMicroApp 多次加载相同微应用时,一些全局状态会保留?

默认情况下,为了提升性能和避免内存泄漏的风险,在多次加载相同微应用的时候,qiankun 不会重复加载资源和执行代码,会直接复用缓存在内存里的代码,因此会保留之前的全局状态。

可以通过禁用缓存解决,但需要注意的是该方法可能会导致性能和内存的问题。

```js
import { loadMicroApp } from 'qiankun';

loadMicroApp(app, {
disableCache: true, // 禁用缓存
});
```
7 changes: 4 additions & 3 deletions src/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function loadMicroApp<T extends ObjectType>(
lifeCycles?: FrameworkLifeCycles<T>,
): MicroApp {
const { props, name } = app;
const { disableCache } = configuration || {};

const container = 'container' in app ? app.container : undefined;
// Must compute the container xpath at beginning to keep it consist around app running
Expand Down Expand Up @@ -137,7 +138,7 @@ export function loadMicroApp<T extends ObjectType>(
);
const { $$cacheLifecycleByAppName } = userConfiguration;

if (container) {
if (container && !disableCache) {
// using appName as cache for internal experimental scenario
if ($$cacheLifecycleByAppName) {
const parcelConfigGetterPromise = appConfigPromiseGetterMap.get(name);
Expand All @@ -152,7 +153,7 @@ export function loadMicroApp<T extends ObjectType>(

const parcelConfigObjectGetterPromise = loadApp(app, userConfiguration, lifeCycles);

if (container) {
if (container && !disableCache) {
if ($$cacheLifecycleByAppName) {
appConfigPromiseGetterMap.set(name, parcelConfigObjectGetterPromise);
} else if (containerXPath) appConfigPromiseGetterMap.set(appContainerXPathKey, parcelConfigObjectGetterPromise);
Expand All @@ -171,7 +172,7 @@ export function loadMicroApp<T extends ObjectType>(

microApp = mountRootParcel(memorizedLoadingFn, { domElement: document.createElement('div'), ...props });

if (container) {
if (container && !disableCache) {
if (containerXPath) {
// Store the microApps which they mounted on the same container
const microAppsRef = containerMicroAppsMap.get(appContainerXPathKey) || [];
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ type QiankunSpecialOpts = {
* skip some scripts or links intercept, like JSONP
*/
excludeAssetFilter?: (url: string) => boolean;
/**
* disable the cache when loading the same micro app multi times
*/
disableCache?: boolean;

globalContext?: typeof window;
};
Expand Down