refactor: allow route prefix

This commit is contained in:
Carlos Valente
2024-11-25 20:21:20 +01:00
committed by Carlos Valente
parent 1f998fda74
commit 6e51e5083b
3 changed files with 31 additions and 2 deletions
+1
View File
@@ -2,6 +2,7 @@
<html lang='en'>
<head>
<meta charset='utf-8' />
<base href="/">
<link rel='icon' href='/favicon.ico' />
<meta name='viewport' content='width=device-width, initial-scale=1' />
<meta name='theme-color' content='#101010' />
+5 -2
View File
@@ -10,7 +10,7 @@ import serverTiming from 'server-timing';
// import utils
import { resolve } from 'path';
import { publicDir, srcDir } from './setup/index.js';
import { environment, isProduction } from './externals.js';
import { environment, isProduction, updateRouterPrefix } from './externals.js';
import { ONTIME_VERSION } from './ONTIME_VERSION.js';
import { consoleSuccess, consoleHighlight, consoleError } from './utils/console.js';
@@ -53,9 +53,12 @@ if (!canLog) {
console.log(`Ontime public directory at ${publicDir.root} `);
}
// calls an update to the client router prefix
updateRouterPrefix();
// Create express APP
const app = express();
if (process.env.NODE_ENV === 'development') {
if (!isProduction) {
// log server timings to requests
app.use(serverTiming());
}
+25
View File
@@ -2,6 +2,9 @@
* This file contains a list of constants that may need to be resolved at runtime
*/
import { readFileSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
// =================================================
// resolve running environment
const env = process.env.NODE_ENV || 'production';
@@ -10,3 +13,25 @@ export const isTest = Boolean(process.env.IS_TEST);
export const environment = isTest ? 'test' : env;
export const isDocker = env === 'docker';
export const isProduction = isDocker || (env === 'production' && !isTest);
/**
* Updates the router prefix in the index.html file
* This is only needed in the cloud environment where the client is not at the root segment
* ie: https://cloud.getontime.com/client-hash/timer
*/
export function updateRouterPrefix(prefix: string | undefined = process.env.ROUTER_PREFIX) {
if (!prefix) {
return;
}
const indexFile = resolve('.', 'client', 'index.html');
try {
const data = readFileSync(indexFile, { encoding: 'utf-8', flag: 'r' }).replace(
/<base href="[^"]*">/g,
`<base href="${prefix}>"`,
);
writeFileSync(indexFile, data, { encoding: 'utf-8', flag: 'w' });
} catch (_error) {
/** unhandled */
}
}