diff --git a/server.js b/server.js
index 3c23c05..03bdf2b 100644
--- a/server.js
+++ b/server.js
@@ -7,8 +7,6 @@ import express from 'express';
import rateLimit from 'express-rate-limit';
import historyApiFallback from 'connect-history-api-fallback';
-import { handler as ssrHandler } from './dist/server/entry.mjs';
-
// Load environment variables from .env file
dotenv.config();
@@ -80,17 +78,33 @@ fs.readdirSync(dirPath, { withFileTypes: true })
});
});
+const renderPlaceholderPage = async (res, msgId, logs) => {
+ const errorMessages = {
+ notCompiled: 'Looks like the GUI app has not yet been compiled.
'
+ + 'Run yarn build
to continue, then restart the server.',
+ notCompiledSsrHandler: 'Server-side rendering failed to initiate, as SSR handler not found.
'
+ + 'This can be fixed by running yarn build
, then restarting the server.
',
+ disabledGui: 'Web-Check API is up and running!
Access the endpoints at '
+ + `${API_DIR}
`,
+ };
+ const logOutput = logs ? `
${logs}
${API_DIR}
`
- );
- res.status(500).send(htmlContent);
+ renderPlaceholderPage(res, 'disabledGui');
});
} else if (!fs.existsSync(guiPath)) {
app.get('/', async (req, res) => {
- const placeholderContent = await fs.promises.readFile(placeholderFilePath, 'utf-8');
- const htmlContent = placeholderContent.replace(
- '',
- 'Looks like the GUI app has not yet been compiled.yarn build
to continue, then restart the server.'
- );
- res.status(500).send(htmlContent);
+ renderPlaceholderPage(res, 'notCompiled');
});
} else { // GUI enabled, and build files present, let's go!!
app.use(express.static('dist/client/'));
- app.use((req, res, next) => {
- ssrHandler(req, res, next);
+ app.use(async (req, res, next) => {
+ const ssrHandlerPath = path.join(__dirname, 'dist', 'server', 'entry.mjs');
+ import(ssrHandlerPath).then(({ handler: ssrHandler }) => {
+ ssrHandler(req, res, next);
+ }).catch(async err => {
+ renderPlaceholderPage(res, 'notCompiledSsrHandler', err.message);
+ });
});
}