Short-term fix to the frontend injected config caching

Signed-off-by: David Festal <dfestal@redhat.com>
This commit is contained in:
David Festal
2024-01-16 19:33:34 +01:00
parent 2f0282fd05
commit 9dfd57d778
3 changed files with 22 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-app-backend': patch
---
Do not force caching of the Javascript asset that contains the injected config.
+6 -3
View File
@@ -31,7 +31,9 @@ type InjectOptions = {
/**
* Injects configs into the app bundle, replacing any existing injected config.
*/
export async function injectConfig(options: InjectOptions) {
export async function injectConfig(
options: InjectOptions,
): Promise<string | undefined> {
const { staticDir, logger, appConfigs } = options;
const files = await fs.readdir(staticDir);
@@ -52,7 +54,7 @@ export async function injectConfig(options: InjectOptions) {
injected,
);
await fs.writeFile(path, newContent, 'utf8');
return;
return path;
} else if (content.includes('__APP_INJECTED_CONFIG_MARKER__')) {
logger.info(`Replacing injected env config in ${jsFile}`);
@@ -61,10 +63,11 @@ export async function injectConfig(options: InjectOptions) {
injected,
);
await fs.writeFile(path, newContent, 'utf8');
return;
return path;
}
}
logger.info('Env config not injected');
return undefined;
}
type ReadOptions = {
+11 -3
View File
@@ -114,6 +114,7 @@ export async function createRouter(
logger.info(`Serving static app content from ${appDistDir}`);
let injectedConfigPath: string | undefined;
if (!disableConfigInjection) {
const appConfigs = await readConfigs({
config,
@@ -121,7 +122,7 @@ export async function createRouter(
env: process.env,
});
await injectConfig({ appConfigs, logger, staticDir });
injectedConfigPath = await injectConfig({ appConfigs, logger, staticDir });
}
const router = Router();
@@ -132,8 +133,15 @@ export async function createRouter(
const staticRouter = Router();
staticRouter.use(
express.static(resolvePath(appDistDir, 'static'), {
setHeaders: res => {
res.setHeader('Cache-Control', CACHE_CONTROL_MAX_CACHE);
setHeaders: (res, path) => {
if (path === injectedConfigPath) {
logger.info(
`Serving in the injected Javascript file with max caching disabled`,
);
res.setHeader('Cache-Control', 'no-cache');
} else {
res.setHeader('Cache-Control', CACHE_CONTROL_MAX_CACHE);
}
},
}),
);