feat(core): Set custom Cache-Control headers for static assets (#5322)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-02-02 12:15:46 +01:00 committed by GitHub
parent 52dea08003
commit ee210e8507
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,6 +35,7 @@ import { createHmac } from 'crypto';
import { promisify } from 'util'; import { promisify } from 'util';
import cookieParser from 'cookie-parser'; import cookieParser from 'cookie-parser';
import express from 'express'; import express from 'express';
import type { ServeStaticOptions } from 'serve-static';
import type { FindManyOptions } from 'typeorm'; import type { FindManyOptions } from 'typeorm';
import { In } from 'typeorm'; import { In } from 'typeorm';
import type { AxiosRequestConfig } from 'axios'; 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')) { 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(); const startTime = new Date().toUTCString();
this.app.use('/index.html', (req, res, next) => { this.app.use('/index.html', (req, res, next) => {
@ -1425,7 +1440,7 @@ class Server extends AbstractServer {
next(); next();
}); });
} else { } else {
this.app.use('/', express.static(GENERATED_STATIC_DIR)); this.app.use('/', express.static(GENERATED_STATIC_DIR, staticOptions));
} }
} }
} }