Skip to content

Commit 9ac3491

Browse files
committed
feat: 增加 credentials 参数,自动携带跨域 cookie
1 parent 4d1c077 commit 9ac3491

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IRouteProps } from '@umijs/types';
55
import assert from 'assert';
66
import { prefetchApps, registerMicroApps, start } from 'qiankun';
77
// @ts-ignore
8-
import { ApplyPluginsType, plugin, getMicroAppRouteComponent } from 'umi';
8+
import { ApplyPluginsType, getMicroAppRouteComponent, plugin } from 'umi';
99

1010
import { defaultMountContainerId, noop, patchMicroAppRoute, testPathWithPrefix, toArray } from './common';
1111
import { defaultHistoryType } from './constants';
@@ -28,29 +28,51 @@ async function getMasterRuntime() {
2828
let microAppRuntimeRoutes: MicroAppRoute[];
2929

3030
export async function render(oldRender: typeof noop) {
31-
const masterOptions = getMasterOptions();
3231
const runtimeOptions = await getMasterRuntime();
32+
let masterOptions: MasterOptions = { ...getMasterOptions(), ...runtimeOptions };
33+
34+
const credentialsApps = masterOptions.apps.filter(app => app.credentials);
35+
if (credentialsApps.length) {
36+
const defaultFetch = masterOptions.fetch || window.fetch;
37+
const fetchWithCredentials = (url: string, init?: RequestInit) => {
38+
// 如果当前 url 为 credentials 应用的 entry,则为其加上 cors 相关配置
39+
if (credentialsApps.some(app => app.entry === url)) {
40+
return defaultFetch(url, {
41+
...init,
42+
mode: 'cors',
43+
credentials: 'include',
44+
});
45+
}
46+
47+
return defaultFetch(url, init);
48+
};
49+
50+
// 设置新的 fetch
51+
masterOptions = { ...masterOptions, fetch: fetchWithCredentials };
52+
}
3353

34-
setMasterOptions({ ...masterOptions, ...runtimeOptions });
35-
const { apps, routes, ...options } = getMasterOptions() as MasterOptions;
54+
// 更新 master options
55+
setMasterOptions(masterOptions);
56+
57+
const { apps, routes, ...options } = masterOptions;
3658
microAppRuntimeRoutes = routes;
3759

3860
// 主应用相关的配置注册完毕后即可开启渲染
3961
oldRender();
4062

41-
// 使用了 base 配置的应用为可注册应用
42-
const registrableApps = apps.filter(app => app.base);
43-
if (registrableApps.length) {
44-
// 不要在 oldRender 调用之前调用 useRegisterMode 方法,因为里面可能会 await defer promise 从而造成死锁
45-
await useLegacyRegisterMode(registrableApps, options);
46-
}
47-
4863
// 未使用 base 配置的可以认为是路由关联或者使用标签装载的应用
4964
const loadableApps = apps.filter(app => !app.base);
5065
if (loadableApps.length) {
5166
const { prefetch, ...importEntryOpts } = options;
5267
if (prefetch) prefetchApps(loadableApps, importEntryOpts);
5368
}
69+
70+
// 使用了 base 配置的应用为可注册应用
71+
const registrableApps = apps.filter(app => app.base);
72+
if (registrableApps.length) {
73+
// 不要在 oldRender 调用之前调用 useRegisterMode 方法,因为里面可能会 await defer promise 从而造成死锁
74+
await useLegacyRegisterMode(registrableApps, options);
75+
}
5476
}
5577

5678
export function patchRoutes(opts: { routes: IRouteProps[] }) {

packages/plugin-qiankun/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export type App = {
1212
entry: string | { scripts: string[]; styles: string[] };
1313
base?: string | string[];
1414
history?: HistoryType;
15+
// 取 entry 时是否需要开启跨域 credentials
16+
credentials?: boolean;
1517
props?: any;
1618
} & Pick<BaseIConfig, 'mountElementId'>;
1719

0 commit comments

Comments
 (0)