Files
talk/src/core/server/app/middleware/cacheHeaders.ts
T
2018-10-15 14:56:43 -06:00

30 lines
772 B
TypeScript

import { RequestHandler } from "express";
import ms from "ms";
export const noCacheMiddleware: RequestHandler = (req, res, next) => {
// Set cache control headers to prevent browsers/cdn's from caching these
// requests.
res.set({ "Cache-Control": "no-cache, no-store, must-revalidate" });
next();
};
export const cacheHeadersMiddleware = (
duration?: string | false
): RequestHandler => {
const maxAge = duration ? Math.floor(ms(duration) / 1000) : false;
if (!maxAge) {
return noCacheMiddleware;
}
return (req, res, next) => {
// Set cache control headers to encourage browsers/cdn's to cache these
// requests if we aren't in private mode.
res.set({
"Cache-Control": `public, max-age=${maxAge}`,
});
next();
};
};