Skip to content

Commit ea2d657

Browse files
committed
changes to accomodate a view frontend
1 parent b0a6236 commit ea2d657

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
REACT_APP_MICROFRONTEND_1_HOST=http://localhost:3031
22
REACT_APP_MICROFRONTEND_2_HOST=http://localhost:3032
3+
REACT_APP_MICROFRONTEND_VUE_HOST=http://localhost:8080
34
REACT_APP_STATIC_COMMON_CONTENT_HOST=http://localhost:5030

src/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import logo from './logo.svg'
44
import Header from './Header'
55
import Footer from './Footer'
66
import Microfrontend from './Microfrontend'
7+
import MicrofrontendVue from './MicrofrontendVue'
78

89
const {
910
REACT_APP_MICROFRONTEND_1_HOST: microfrontend1Host,
1011
REACT_APP_MICROFRONTEND_2_HOST: microfrontend2Host,
12+
REACT_APP_MICROFRONTEND_VUE_HOST: microfrontendVueHost,
1113
} = process.env;
1214

1315

@@ -19,6 +21,10 @@ const Microfrontend2 = ({ history }) => (
1921
<Microfrontend history={history} host={microfrontend2Host} name="Microfrontend2" />
2022
);
2123

24+
const MicrofrontendVueMF = ({ history }) => (
25+
<MicrofrontendVue history={history} host={microfrontendVueHost} name="MicrofrontendVue" />
26+
);
27+
2228
function App() {
2329
return (
2430
<BrowserRouter>
@@ -27,6 +33,7 @@ function App() {
2733
<h1>Goda's microfrontend page</h1>
2834
<Route exact path="/microfrontend1" component={Microfrontend1} />
2935
<Route exact path="/microfrontend2" component={Microfrontend2} />
36+
<Route exact path="/microfrontendVue" component={MicrofrontendVueMF} />
3037
<Footer/>
3138

3239
</React.Fragment>

src/MicrofrontendVue.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
3+
class MicrofrontendVue extends React.Component {
4+
componentDidMount() {
5+
const { name, host, document } = this.props;
6+
const scriptId = `micro-frontend-script-${name}`;
7+
if (document.getElementById(scriptId)) {
8+
this.renderMicroFrontend();
9+
return;
10+
}
11+
12+
fetch(`${host}/asset-manifest.json`)
13+
.then(res => res.json())
14+
.then(manifest => {
15+
let script = document.createElement('script');
16+
script.id = scriptId;
17+
script.crossOrigin = '';
18+
script.src = `${host}/${manifest['app.js']}`;
19+
document.head.appendChild(script);
20+
console.log(script)
21+
22+
script = document.createElement('script');
23+
script.id = scriptId;
24+
script.crossOrigin = '';
25+
script.src = `${host}/${manifest['chunk-vendors.js']}`;
26+
script.onload = this.renderMicrofrontend;
27+
document.head.appendChild(script);
28+
console.log(script)
29+
});
30+
31+
}
32+
33+
componentWillUnmount() {
34+
const { name, window } = this.props;
35+
window[`unmount${name}`](`${name}-container`);
36+
}
37+
38+
renderMicrofrontend = () => {
39+
40+
const { name, window, history } = this.props;
41+
window[`render${name}`](`${name}-container`);
42+
}
43+
44+
render() {
45+
return <main id={`${this.props.name}-container`} />;
46+
}
47+
48+
}
49+
50+
MicrofrontendVue.defaultProps = {
51+
document,
52+
window,
53+
};
54+
55+
export default MicrofrontendVue;

0 commit comments

Comments
 (0)