Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit bb9ce1f

Browse files
authored
Merge pull request #9 from topcoder-platform/dev
Hotfix for prod: change way of updating index.html
2 parents 2e0755f + 143ec6b commit bb9ce1f

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

package-lock.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"webpack-dev-server": "^3.9.0",
3737
"webpack-merge": "^4.2.2",
3838
"axios": "^0.21.0",
39-
"path": "^0.12.7"
39+
"path": "^0.12.7",
40+
"node-html-parser": "^2.0.0"
4041
},
4142
"dependencies": {
4243
"express": "^4.17.1",

server.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require("path");
55
const axios = require('axios');
66
const fs = require('fs');
77
const fsPromises = require('fs').promises;
8+
const htmlParse = require('node-html-parser');
89
const APP_CONST = require('./config/constants');
910

1011
let distPath = path.resolve(__dirname, "./dist");
@@ -54,25 +55,30 @@ app.get("*", async function (req, res) {
5455
res.send({'error': { message: "Check application environment", code: 500 }})
5556
break;
5657
}
57-
if (mfeIndex.indexOf("<!-- Routes Start -->") > -1) {
58-
let mfeRoutes
59-
switch (process.env.APPENV.toLowerCase()) {
60-
case APP_CONST.APP_ENV_DEV.toLowerCase() :
61-
case APP_CONST.APP_ENV_PROD.toLowerCase() :
62-
mfeRoutes = await axios.get(env_config.mfeRoutesPath)
63-
mfeRoutes = mfeRoutes.data
64-
break;
65-
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
66-
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
67-
mfeRoutes = await fsPromises.readFile(path.join(configPath + env_config.mfeRoutesPath))
68-
break;
69-
default :
70-
res.send({'error': { message: "Check application environment", code: 500 }})
71-
break;
72-
}
73-
const mfe = mfeIndex.toString().replace(/<!-- Routes Start -->([\s\S]*?)<!-- Routes End -->/, "<!-- Routes Start -->" + mfeRoutes + "<!-- Routes End -->");
74-
await fsPromises.writeFile(path.join(distPath + "/index.html"), mfe);
58+
let mfeRoutes
59+
switch (process.env.APPENV.toLowerCase()) {
60+
case APP_CONST.APP_ENV_DEV.toLowerCase() :
61+
case APP_CONST.APP_ENV_PROD.toLowerCase() :
62+
mfeRoutes = await axios.get(env_config.mfeRoutesPath)
63+
mfeRoutes = mfeRoutes.data
64+
break;
65+
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
66+
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
67+
mfeRoutes = await fsPromises.readFile(path.join(configPath + env_config.mfeRoutesPath))
68+
break;
69+
default :
70+
res.send({'error': { message: "Check application environment", code: 500 }})
71+
break;
7572
}
73+
let mfeIndexHtml = htmlParse.parse(mfeIndex)
74+
let singleSpaMain = mfeIndexHtml.querySelector('#single-spa-main')
75+
singleSpaMain.remove()
76+
let singleSpaRouter = mfeIndexHtml.querySelector('single-spa-router')
77+
singleSpaRouter.insertAdjacentHTML('beforeend', '<div id="single-spa-main"></div>')
78+
singleSpaMain = mfeIndexHtml.querySelector('#single-spa-main')
79+
singleSpaMain.insertAdjacentHTML('beforeend', mfeRoutes)
80+
81+
await fsPromises.writeFile(path.join(distPath + "/index.html"), mfeIndexHtml.toString());
7682
res.sendFile(path.join(distPath + "/index.html"));
7783
});
7884

src/index.ejs

+1-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@
7272
<nav>
7373
<application name="@topcoder/micro-frontends-navbar-app"></application>
7474
</nav>
75-
<div id="single-spa-main">
76-
<!-- Routes Start -->
77-
<!-- Routes End -->
78-
</div>
75+
<div id="single-spa-main"></div>
7976
</single-spa-router>
8077
</template>
8178
</head>

0 commit comments

Comments
 (0)