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

Hotfix for prod: change way of updating index.html #9

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"webpack-dev-server": "^3.9.0",
"webpack-merge": "^4.2.2",
"axios": "^0.21.0",
"path": "^0.12.7"
"path": "^0.12.7",
"node-html-parser": "^2.0.0"
},
"dependencies": {
"express": "^4.17.1",
Expand Down
42 changes: 24 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require("path");
const axios = require('axios');
const fs = require('fs');
const fsPromises = require('fs').promises;
const htmlParse = require('node-html-parser');
const APP_CONST = require('./config/constants');

let distPath = path.resolve(__dirname, "./dist");
Expand Down Expand Up @@ -54,25 +55,30 @@ app.get("*", async function (req, res) {
res.send({'error': { message: "Check application environment", code: 500 }})
break;
}
if (mfeIndex.indexOf("<!-- Routes Start -->") > -1) {
let mfeRoutes
switch (process.env.APPENV.toLowerCase()) {
case APP_CONST.APP_ENV_DEV.toLowerCase() :
case APP_CONST.APP_ENV_PROD.toLowerCase() :
mfeRoutes = await axios.get(env_config.mfeRoutesPath)
mfeRoutes = mfeRoutes.data
break;
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
mfeRoutes = await fsPromises.readFile(path.join(configPath + env_config.mfeRoutesPath))
break;
default :
res.send({'error': { message: "Check application environment", code: 500 }})
break;
}
const mfe = mfeIndex.toString().replace(/<!-- Routes Start -->([\s\S]*?)<!-- Routes End -->/, "<!-- Routes Start -->" + mfeRoutes + "<!-- Routes End -->");
await fsPromises.writeFile(path.join(distPath + "/index.html"), mfe);
let mfeRoutes
switch (process.env.APPENV.toLowerCase()) {
case APP_CONST.APP_ENV_DEV.toLowerCase() :
case APP_CONST.APP_ENV_PROD.toLowerCase() :
mfeRoutes = await axios.get(env_config.mfeRoutesPath)
mfeRoutes = mfeRoutes.data
break;
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
mfeRoutes = await fsPromises.readFile(path.join(configPath + env_config.mfeRoutesPath))
break;
default :
res.send({'error': { message: "Check application environment", code: 500 }})
break;
}
let mfeIndexHtml = htmlParse.parse(mfeIndex)
let singleSpaMain = mfeIndexHtml.querySelector('#single-spa-main')
singleSpaMain.remove()
let singleSpaRouter = mfeIndexHtml.querySelector('single-spa-router')
singleSpaRouter.insertAdjacentHTML('beforeend', '<div id="single-spa-main"></div>')
singleSpaMain = mfeIndexHtml.querySelector('#single-spa-main')
singleSpaMain.insertAdjacentHTML('beforeend', mfeRoutes)

await fsPromises.writeFile(path.join(distPath + "/index.html"), mfeIndexHtml.toString());
res.sendFile(path.join(distPath + "/index.html"));
});

Expand Down
5 changes: 1 addition & 4 deletions src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@
<nav>
<application name="@topcoder/micro-frontends-navbar-app"></application>
</nav>
<div id="single-spa-main">
<!-- Routes Start -->
<!-- Routes End -->
</div>
<div id="single-spa-main"></div>
</single-spa-router>
</template>
</head>
Expand Down