From ee210e8507ce06f7052328f28ba14451f51b3db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Thu, 2 Feb 2023 12:15:46 +0100 Subject: [PATCH] feat(core): Set custom Cache-Control headers for static assets (#5322) --- packages/cli/src/Server.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 37875490fe..beb458cac3 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -35,6 +35,7 @@ import { createHmac } from 'crypto'; import { promisify } from 'util'; import cookieParser from 'cookie-parser'; import express from 'express'; +import type { ServeStaticOptions } from 'serve-static'; import type { FindManyOptions } from 'typeorm'; import { In } from 'typeorm'; import type { AxiosRequestConfig } from 'axios'; @@ -1416,8 +1417,22 @@ class Server extends AbstractServer { ); } + const staticOptions: ServeStaticOptions = { + cacheControl: false, + setHeaders: (res: express.Response, path: string) => { + const isIndex = path === pathJoin(GENERATED_STATIC_DIR, 'index.html'); + const cacheControl = isIndex + ? 'no-cache, no-store, must-revalidate' + : 'max-age=86400, immutable'; + res.header('Cache-Control', cacheControl); + }, + }; if (!config.getEnv('endpoints.disableUi')) { - this.app.use('/', express.static(GENERATED_STATIC_DIR), express.static(EDITOR_UI_DIST_DIR)); + this.app.use( + '/', + express.static(GENERATED_STATIC_DIR, staticOptions), + express.static(EDITOR_UI_DIST_DIR, staticOptions), + ); const startTime = new Date().toUTCString(); this.app.use('/index.html', (req, res, next) => { @@ -1425,7 +1440,7 @@ class Server extends AbstractServer { next(); }); } else { - this.app.use('/', express.static(GENERATED_STATIC_DIR)); + this.app.use('/', express.static(GENERATED_STATIC_DIR, staticOptions)); } } }