@@ -5,7 +5,7 @@ import { IRouteProps } from '@umijs/types';
55import assert from 'assert';
66import { prefetchApps, registerMicroApps, start } from 'qiankun';
77// @ts-ignore
8- import { ApplyPluginsType, plugin, getMicroAppRouteComponent } from 'umi';
8+ import { ApplyPluginsType, getMicroAppRouteComponent, plugin } from 'umi';
99
1010import { defaultMountContainerId, noop, patchMicroAppRoute, testPathWithPrefix, toArray } from './common';
1111import { defaultHistoryType } from './constants';
@@ -28,29 +28,51 @@ async function getMasterRuntime() {
2828let microAppRuntimeRoutes: MicroAppRoute[];
2929
3030export 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
5678export function patchRoutes(opts: { routes: IRouteProps[] } ) {
0 commit comments