Skip to content

Commit

Permalink
fix(1.0.0-beta.4): fix bug of vue-router@4 when disable memory-router
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Apr 25, 2023
1 parent ee83566 commit f1b96b5
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dev/children/vite2/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let router = null
let history = null
// 将渲染操作放入 mount 函数
window.mount = (data) => {
history = createWebHistory(import.meta.env.BASE_URL)
history = createWebHistory(window.__MICRO_APP_BASE_ROUTE__ || import.meta.env.BASE_URL)
router = createRouter({
history,
routes,
Expand Down
2 changes: 1 addition & 1 deletion dev/children/vite4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"start": "vite",
"start": "vite --force",
"build": "vite build",
"preview": "vite preview"
},
Expand Down
3 changes: 2 additions & 1 deletion dev/children/vite4/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ let router = null
let history = null
// 将渲染操作放入 mount 函数
window.mount = (data) => {
history = createWebHistory(import.meta.env.BASE_URL)
// console.log(1212121212121212, location.href)
history = createWebHistory(window.__MICRO_APP_BASE_ROUTE__ || import.meta.env.BASE_URL)
router = createRouter({
history,
routes,
Expand Down
7 changes: 7 additions & 0 deletions dev/main-react16/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export default [
component: './vite2/vite2',
exact: false,
},
{
path: '/vite4',
name: 'vite4',
icon: 'RocketOutlined',
component: './vite4/vite4',
exact: false,
},
{
path: '/angular11',
name: 'angular11',
Expand Down
2 changes: 2 additions & 0 deletions dev/main-react16/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let config = {
vue2: 'http://localhost:4001/',
vue3: 'http://localhost:4002/',
vite2: 'http://localhost:7001/',
vite4: 'http://localhost:7002/',
angular11: 'http://localhost:6001/',
angular14: 'http://localhost:6002/',
}
Expand All @@ -20,6 +21,7 @@ if (isEnvPro) {
angular11: locationOrigin,
angular14: locationOrigin,
vite2: locationOrigin,
vite4: locationOrigin,
}
}

Expand Down
1 change: 1 addition & 0 deletions dev/main-react16/src/locales/zh-CN/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default {
'menu.multiple': '多个应用',
'menu.self': '自带页面',
'menu.vite2': 'vite2应用',
'menu.vite4': 'vite4应用',
};
10 changes: 5 additions & 5 deletions dev/main-react16/src/pages/react16/react16.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ export default class App extends React.Component {

microApp.addGlobalDataListener(this.handleGlobalDataForBaseApp)

setTimeout(() => {
this.setState({
showMicroApp: !this.state.showMicroApp,
})
}, 0);
// setTimeout(() => {
// this.setState({
// showMicroApp: !this.state.showMicroApp,
// })
// }, 0);

// this.releaseBeforeEach1 = microApp.router.beforeEach((to, from, appName) => {
// // const a = document.createElement('div')
Expand Down
3 changes: 2 additions & 1 deletion dev/main-react16/src/pages/vite2/vite2.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ function vite2 (props) {
// disableSandbox
iframe
keep-router-state
// disable-memory-router
disable-memory-router
// disable-patch-request
// keep-alive
// default-page='/micro-app/vite2/page2'
baseroute='/micro-app/demo/vite2'
>
</micro-app>

Expand Down
120 changes: 120 additions & 0 deletions dev/main-react16/src/pages/vite4/vite4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/** @jsxRuntime classic */
/** @jsx jsxCustomEvent */
import jsxCustomEvent from '@micro-zoe/micro-app/polyfill/jsx-custom-event'
import { useState, useEffect } from 'react'
import { Button, Spin, Col } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'
// import { EventCenterForMicroApp } from '@micro-zoe/micro-app'
import config from '../../config'
import './vite4.less'
import microApp from '@micro-zoe/micro-app'

// 注册子应用vite4的数据通信对象
// window.eventCenterForVite = new EventCenterForMicroApp('vite4')

const antIcon = <LoadingOutlined style={{ fontSize: 30 }} spin />

function vite4 (props) {
const [data, changeData] = useState({from: '来自基座的初始化数据'})
const [showLoading, hideLoading] = useState(true)

function handleMounted () {
console.timeEnd('vite4')
hideLoading(false)
console.log('主应用-生命周期:mounted -- vite4')
}

function handleDataChange (e) {
console.log('来自 vite4 子应用的数据', e.detail.data)
}

function jumpToHome () {
microApp.router.push({name: 'vite4', path: '/micro-app/vite4/'})
}

function jumpToPage2 () {
microApp.router.push({name: 'vite4', path: '/micro-app/vite4/element-plus'})
}

function jumpToPage3 () {
microApp.router.push({name: 'vite4', path: '/micro-app/vite4/ant-design-vue'})
}

useEffect(() => {
console.time('vite4')
// const releaseBeforeEach1 = microApp.router.beforeEach((to, from, appName) => {
// console.log('全局 beforeEach: ', to, from, appName)
// })

// const releaseBeforeEach2 = microApp.router.beforeEach({
// vite4 (to, from) {
// console.log('指定 beforeEach: ', to, from)
// }
// })

// const releaseAfterEach1 = microApp.router.afterEach((to, from, appName) => {
// console.log('全局 afterEach: ', to, from, appName)
// })

// const releaseAfterEach2 = microApp.router.afterEach({
// vite4 (to, from) {
// console.log('指定 afterEach: ', to, from)
// }
// })

microApp.router.setBaseAppRouter(props.history)

return () => {
// releaseBeforeEach1()
// releaseBeforeEach2()
// releaseAfterEach1()
// releaseAfterEach2()
}
}, [])

return (
<div>
<div className='btn-con'>
<Col span={6} className='btn-con'>
<Button type='primary' onClick={() => changeData({from: '来自基座的数据' + (+new Date())})}>
向子应用发送数据
</Button>
<Button type="primary" onClick={jumpToHome}>控制子应用跳转首页</Button>
<Button type="primary" onClick={jumpToPage2}>控制子应用跳转element-plus</Button>
<Button type="primary" onClick={jumpToPage3}>控制子应用跳转ant-design-vue</Button>
</Col>
</div>
{
showLoading && <Spin indicator={antIcon} />
}
<micro-app
name='vite4'
url={`${config.vite4}micro-app/vite4/`}
// url={`http://127.0.0.1:8080/micro-app/vite4/`}
data={data}
// onBeforemount={() => hideLoading(false)}
onMounted={handleMounted}
onDataChange={handleDataChange}
onAfterhidden={() => console.log('基座:keep-alive:Afterhidden 已推入后台')}
onBeforeshow={() => console.log('基座:keep-alive:Beforeshow 即将推入前台')}
onAftershow={() => {console.log('基座:keep-alive:Aftershow 已经推入前台'); hideLoading(false)}}
onError={() => console.log('渲染出错')}
// destroy
// inline
// disableSandbox
iframe
keep-router-state
disable-memory-router
// disable-patch-request
// keep-alive
// default-page='/micro-app/vite4/page2'
baseroute='/micro-app/demo/vite4'
>
</micro-app>

{/* <iframe src={`${config.vite4}micro-app/vite4/`} width='100%' height='600px'></iframe> */}
</div>
)
}

export default vite4
3 changes: 3 additions & 0 deletions dev/main-react16/src/pages/vite4/vite4.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test-vite4 {
color: red;
}
6 changes: 6 additions & 0 deletions src/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ export default class CreateApp implements AppInterface {
this.sandBox?.recordAndReleaseEffect({ keepAlive: true })
}
}
/**
* TODO: 这里增加一个处理,如果渲染完成时已经卸载,则进行一些操作
* 如果是默认模式:删除所有事件和定时器
* 如果是umd模式:重新记录和清空事件
* 补充:非必需,优先级低
*/
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/sandbox/iframe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import {
} from '../../interact'
import {
patchIframeRoute,
actionsForDisableMemoryRoute,
} from './route'
import {
router,
initRouteStateWithURL,
clearRouteStateFromURL,
addHistoryListener,
removeStateAndPathFromBrowser,
removePathFromBrowser,
updateBrowserURLWithLocation,
patchHistory,
releasePatchHistory,
Expand Down Expand Up @@ -166,7 +167,12 @@ export default class IframeSandbox {
this.microAppWindow.__MICRO_APP_NAME__,
)
} else {
this.microAppWindow.__MICRO_APP_BASE_ROUTE__ = this.microAppWindow.__MICRO_APP_BASE_URL__ = baseroute
// actions when memory-route disable
actionsForDisableMemoryRoute(
this.microAppWindow.__MICRO_APP_NAME__,
this.microAppWindow,
baseroute,
)
}

/**
Expand Down Expand Up @@ -486,7 +492,7 @@ export default class IframeSandbox {
}

public removeRouteInfoForKeepAliveApp (): void {
removeStateAndPathFromBrowser(this.microAppWindow.__MICRO_APP_NAME__)
removePathFromBrowser(this.microAppWindow.__MICRO_APP_NAME__)
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/sandbox/iframe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
assign,
} from '../../libs/utils'
import globalEnv from '../../libs/global_env'

export function patchIframeRoute (
appName: string,
Expand Down Expand Up @@ -51,3 +52,34 @@ export function patchIframeRoute (
childHost,
)
}

/**
* actions when memory-route disable
* @param appName app name
* @param microAppWindow iframeWindow
* @param baseroute base route for child app
*/
export function actionsForDisableMemoryRoute (
appName: string,
microAppWindow: microAppWindowType,
baseroute: string,
): void {
microAppWindow.__MICRO_APP_BASE_ROUTE__ = microAppWindow.__MICRO_APP_BASE_URL__ = baseroute

/**
* Sync browser router info to iframe when disable memory-router
* e.g.:
* [email protected] get target path by remove the base section from location.pathname
* code: window.location.pathname.slice(base.length) || '/'; (base is baseroute)
* NOTE:
* 1. iframe router and browser router are separated, we should update iframe router manually
* 2. withSandbox location is browser location when disable memory-router, so no need to do anything
*/
const rawLocation = globalEnv.rawWindow.location
updateMicroLocation(
appName,
rawLocation.href,
rawLocation,
'prevent'
)
}
7 changes: 4 additions & 3 deletions src/sandbox/router/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
setMicroState,
getMicroState,
getMicroPathFromURL,
isMemoryRouterEnabled,
} from './core'
import {
logError,
Expand Down Expand Up @@ -99,7 +100,7 @@ function createRouterApi (): RouterApi {
const appName = formatAppName(to.name)
if (appName && isString(to.path)) {
const app = appInstanceMap.get(appName)
if (app && (!app.sandBox || !app.useMemoryRouter)) {
if (app && !isMemoryRouterEnabled(appName)) {
return logError(`navigation failed, memory router of app ${appName} is closed`)
}
// active apps, include hidden keep-alive app
Expand Down Expand Up @@ -204,8 +205,8 @@ function createRouterApi (): RouterApi {
* 2. useMemoryRouter is false
*/
function commonHandlerForAttachToURL (appName: string): void {
const app = appInstanceMap.get(appName)!
if (app.sandBox && app.useMemoryRouter) {
if (isMemoryRouterEnabled(appName)) {
const app = appInstanceMap.get(appName)!
attachRouteToBrowserURL(
appName,
setMicroPathToURL(appName, app.sandBox.proxyWindow.location as MicroLocation),
Expand Down
Loading

0 comments on commit f1b96b5

Please sign in to comment.