Skip to content

Commit

Permalink
fix(1.0.0-beta.1): fixed a cross domain issue caused by modifying domain
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Mar 22, 2023
1 parent 2f0c4d6 commit a6dc91b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 44 deletions.
6 changes: 5 additions & 1 deletion dev/children/vite2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
console.log('普通的内联script元素', window)
</script>
<script type="module">
console.log('我不应该执行2遍', '22222222222')
console.log('我不应该执行2遍 -- 内联module script')
</script>
<!-- <script>
window.document.domain = 'localhost'
console.log('子应用修改了 window.document.domain')
</script> -->
</head>
<body>
<div id="vite-app"></div>
Expand Down
4 changes: 4 additions & 0 deletions dev/children/vite2/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,7 @@ if (process.env.NODE_ENV !== 'production') {

// console.log('scopeProperties Vue: ', Vue)
// console.log('scopeProperties window.Vue: ', window.Vue)


/* ---------------------- 特殊操作 --------------------- */
// window.document.domain = 'localhost';
4 changes: 4 additions & 0 deletions dev/main-react16/src/pages/document.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<title>Micro App</title>
<link rel="icon" href="<%= context.config.publicPath +'favicon.ico'%>" type="image/x-icon" />
<!-- <script crossorigin src="https://sgm-static.jd.com/sgm-2.8.0.js" name="SGMH5" sid="xxx" appKey="xxx"></script> -->
<!-- <script>
window.document.domain = 'localhost'
console.log('主应用修改了 window.document.domain')
</script> -->
</head>
<body>
<noscript>Out-of-the-box mid-stage front/design solution!</noscript>
Expand Down
4 changes: 4 additions & 0 deletions dev/main-vue2/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<!-- <script>
window.document.domain = 'localhost'
console.log('主应用修改了 window.document.domain')
</script> -->
</head>
<body>
<noscript>
Expand Down
13 changes: 1 addition & 12 deletions dev/main-vue2/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ microApp.start({
}
},
plugins: {
modules: {
vite: [{
loader(code) {
if (process.env.NODE_ENV === 'development') {
code = code.replace(/(from|import)(\s*['"])(\/micro-app\/vite\/)/g, (all) => {
return all.replace('/micro-app/vite/', 'http://localhost:7001/micro-app/vite/')
})
}
return code
}
}]
}

},
/**
* 自定义fetch
Expand Down
5 changes: 2 additions & 3 deletions dev/main-vue2/src/pages/vite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
<div class="vite">
<micro-app
name='vite'
url='http://localhost:7001/micro-app/vite/'
inline
disableSandbox
url='http://localhost:7001/micro-app/vite2/'
iframe
>
<!-- destroy inline scopecss='false' -->
</micro-app>
Expand Down
2 changes: 1 addition & 1 deletion docs/1.x/zh-cn/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import './public-path'

**描述:子应用的基础路由**

详情见[路由-基础路由](/zh-cn/route?id=基础路由)一章。
详情见[路由-基础路由](/zh-cn/browser-router?id=基础路由)一章。

### `__MICRO_APP_BASE_APPLICATION__`

Expand Down
50 changes: 23 additions & 27 deletions src/sandbox/iframe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,14 @@ export default class IframeSandbox {

this.microAppWindow = this.iframe!.contentWindow

// TODO: 优化代码
// exec before initStaticGlobalKeys
this.createProxyLocation(
appName,
url,
this.microAppWindow,
childStaticLocation,
browserHost,
childHost,
)

this.createProxyWindow(
appName,
this.microAppWindow,
)

this.initStaticGlobalKeys(appName, url)
// get escapeProperties from plugins
this.getSpecialProperties(appName)

this.patchIframe(this.microAppWindow, (resolve: CallableFunction) => {
// TODO: 优化代码
// exec before initStaticGlobalKeys
this.createProxyLocation(appName, url, this.microAppWindow, childStaticLocation, browserHost, childHost)
this.createProxyWindow(appName, this.microAppWindow)
this.initStaticGlobalKeys(appName, url)
// get escapeProperties from plugins
this.getSpecialProperties(appName)
this.createIframeTemplate(this.microAppWindow)
patchIframeRoute(appName, this.microAppWindow, childFullPath)
this.windowEffect = patchIframeWindow(appName, this.microAppWindow)
Expand Down Expand Up @@ -329,6 +316,7 @@ export default class IframeSandbox {
this.microAppWindow.__MICRO_APP_NAME__ = appName
this.microAppWindow.__MICRO_APP_URL__ = url
this.microAppWindow.__MICRO_APP_PUBLIC_PATH__ = getEffectivePath(url)
this.microAppWindow.__MICRO_APP_BASE_ROUTE__ = ''
this.microAppWindow.__MICRO_APP_WINDOW__ = this.microAppWindow
this.microAppWindow.__MICRO_APP_PRE_RENDER__ = false
this.microAppWindow.__MICRO_APP_UMD_MODE__ = false
Expand All @@ -346,17 +334,25 @@ export default class IframeSandbox {

// TODO: RESTRUCTURE
private patchIframe (microAppWindow: microAppWindowType, cb: CallableFunction): void {
const oldMicroDocument = microAppWindow.document
this.sandboxReady = new Promise<void>((resolve) => {
(function iframeLocationReady () {
setTimeout(() => {
if (microAppWindow.location.href === 'about:blank') {
try {
if (microAppWindow.document === oldMicroDocument) {
iframeLocationReady()
} else {
/**
* NOTE:
* 1. microAppWindow will not be recreated
* 2. the properties of microAppWindow may be recreated, such as document
* 3. the variables added to microAppWindow may be cleared
*/
microAppWindow.stop()
cb(resolve)
}
} catch (e) {
iframeLocationReady()
} else {
/**
* microAppWindow.document rebuild
*/
microAppWindow.stop()
cb(resolve)
}
}, 0)
})()
Expand Down
1 change: 1 addition & 0 deletions src/sandbox/with/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ export default class WithSandBox implements WithSandBoxInterface {
microAppWindow.__MICRO_APP_NAME__ = appName
microAppWindow.__MICRO_APP_URL__ = url
microAppWindow.__MICRO_APP_PUBLIC_PATH__ = getEffectivePath(url)
microAppWindow.__MICRO_APP_BASE_ROUTE__ = ''
microAppWindow.__MICRO_APP_WINDOW__ = microAppWindow
microAppWindow.__MICRO_APP_PRE_RENDER__ = false
microAppWindow.__MICRO_APP_UMD_MODE__ = false
Expand Down

0 comments on commit a6dc91b

Please sign in to comment.