From 6e51e5083b9cfc730522c7f353df15057d2be67f Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Mon, 25 Nov 2024 20:21:20 +0100 Subject: [PATCH] refactor: allow route prefix --- apps/client/index.html | 1 + apps/server/src/app.ts | 7 +++++-- apps/server/src/externals.ts | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apps/client/index.html b/apps/client/index.html index 9f627b2bb..7254d879b 100644 --- a/apps/client/index.html +++ b/apps/client/index.html @@ -2,6 +2,7 @@ + diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index d1d47da97..df3a8122e 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -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()); } diff --git a/apps/server/src/externals.ts b/apps/server/src/externals.ts index 36bccf3f2..7593833ff 100644 --- a/apps/server/src/externals.ts +++ b/apps/server/src/externals.ts @@ -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( + //g, + `