Skip to content

Commit

Permalink
fix: microApp route mode default prepend (#11345)
Browse files Browse the repository at this point in the history
* fix: microApp route mode default prepend

* chore: better typings and docs
  • Loading branch information
jaredleechn authored Jun 30, 2023
1 parent 4a60bc5 commit fb17755
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
4 changes: 4 additions & 0 deletions examples/qiankun-master/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const qiankun = {
microApp: 'slave',
mode: 'match',
},
{
path: '/prefix',
microApp: 'slave',
},
],
},
};
8 changes: 8 additions & 0 deletions examples/qiankun-slave/cypress/e2e/example.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ describe('QianKun Plugin', () => {
});
});

describe('microApp route prepend ok', () => {
it('hit microApp route', () => {
cy.visit('/prefix/nav');

cy.contains('goto slave app2');
});
});

describe('MicroAppLink crossing multi apps', function () {
it('jump between slave and slave-app2', () => {
cy.visit('/slave/nav');
Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/libs/qiankun/master/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import React from 'react';
import { Navigate, type IRouteProps } from 'umi';
import { defaultMicroAppRouteMode, MicroAppRouteMode } from './constants';
import { getMicroAppRouteComponent } from './getMicroAppRouteComponent';
import type { MicroAppRoute } from './types';

Expand Down Expand Up @@ -70,8 +71,9 @@ export function patchMicroAppRoute(
}
}

const { mode = defaultMicroAppRouteMode } = route;
// 在前缀模式下,自动追加通配符,匹配子应用的路由
if (route.mode === 'prepend' && !route.path.endsWith('/*')) {
if (mode === MicroAppRouteMode.PREPEND && !route.path.endsWith('/*')) {
route.path = route.path.replace(/\/?$/, '/*');
}

Expand Down
12 changes: 12 additions & 0 deletions packages/plugins/libs/qiankun/master/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ export const defaultMasterRootId = 'root-master';
export const defaultHistoryType = 'browser';
export const qiankunStateForSlaveModelNamespace = '@@qiankunStateForSlave';
export const qiankunStateFromMasterModelNamespace = '@@qiankunStateFromMaster';
export enum MicroAppRouteMode {
/**
* 既作为匹配规则,也作为子应用 router.basename
*/
PREPEND = 'prepend',
/**
* 仅作为匹配规则
*/
MATCH = 'match',
}

export const defaultMicroAppRouteMode = MicroAppRouteMode.PREPEND;
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React from 'react';
import { useMatch } from 'umi';
import { MicroApp } from './MicroApp';
import { defaultMicroAppRouteMode, MicroAppRouteMode } from './constants';

export function getMicroAppRouteComponent(opts: {
appName: string;
base: string;
routePath: string;
routeMode: 'match' | 'prepend';
routeMode: MicroAppRouteMode;
masterHistoryType: string;
routeProps?: any;
}) {
const { base, masterHistoryType, appName, routeProps, routePath, routeMode = 'prepend' } = opts;
const { base, masterHistoryType, appName, routeProps, routePath, routeMode = defaultMicroAppRouteMode } = opts;
const RouteComponent = () => {
const match = useMatch(routePath);
const url = match ? match.pathnameBase : '';
// 默认取静态配置的 base
let umiConfigBase = base === '/' ? '' : base;
// 匹配模式下,routePath 不会作为 prefix
const prefix = routeMode === 'match' ? '' : (url.endsWith('/') ? url.substr(0, url.length - 1) : url);
const prefix = routeMode === MicroAppRouteMode.MATCH ? '' : (url.endsWith('/') ? url.substr(0, url.length - 1) : url);
// 拼接子应用挂载路由
let runtimeMatchedBase = umiConfigBase + prefix;
Expand Down
7 changes: 2 additions & 5 deletions packages/plugins/libs/qiankun/master/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-nocheck
/* eslint-disable */
import { FrameworkConfiguration, FrameworkLifeCycles } from 'qiankun';
import type { MicroAppRouteMode } from './constants';

type BaseIConfig = any;

Expand All @@ -18,11 +19,7 @@ export type App = {
export type MicroAppRoute = {
path: string;
microApp: string;
/**
* prepend 既作为匹配规则,也作为子应用 router.basename
* match 仅作为匹配规则
*/
mode: 'match' | 'prepend';
mode: `${MicroAppRouteMode}`;
} & Record<string, any>;

export type MasterOptions = {
Expand Down

0 comments on commit fb17755

Please sign in to comment.