mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Allow overriding the s-maxage cache header (#6294)
* Allow overriding the s-maxage cache header * Only load expiry / set headers when cache exists
This commit is contained in:
@@ -2,6 +2,7 @@ import { RequestHandler } from 'express';
|
||||
import cache from '../cache';
|
||||
import env from '../env';
|
||||
import asyncHandler from '../utils/async-handler';
|
||||
import { getCacheControlHeader } from '../utils/get-cache-headers';
|
||||
import { getCacheKey } from '../utils/get-cache-key';
|
||||
|
||||
const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
@@ -17,18 +18,11 @@ const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next)
|
||||
const cachedData = await cache.get(key);
|
||||
|
||||
if (cachedData) {
|
||||
// Set cache-control header, but only for the public role
|
||||
if (env.CACHE_AUTO_PURGE !== true && !!req.accountability?.role === false) {
|
||||
const expiresAt = await cache.get(`${key}__expires_at`);
|
||||
const maxAge = `max-age=${expiresAt - Date.now()}`;
|
||||
res.setHeader('Cache-Control', `public, ${maxAge}`);
|
||||
} else {
|
||||
// This indicates that the browser/proxy is allowed to cache, but has to revalidate with
|
||||
// the server before use. At this point, we don't include Last-Modified, so it'll always
|
||||
// recreate the local cache. This does NOT mean that cache is disabled all together, as
|
||||
// Directus is still pulling the value from it's internal cache.
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
}
|
||||
const cacheExpiryDate = (await cache.get(`${key}__expires_at`)) as number | null;
|
||||
const cacheTTL = cacheExpiryDate ? cacheExpiryDate - Date.now() : null;
|
||||
|
||||
res.setHeader('Cache-Control', getCacheControlHeader(req, cacheTTL));
|
||||
res.setHeader('Vary', 'Origin, Cache-Control');
|
||||
|
||||
return res.json(cachedData);
|
||||
} else {
|
||||
|
||||
@@ -7,6 +7,7 @@ import env from '../env';
|
||||
import asyncHandler from '../utils/async-handler';
|
||||
import { getCacheKey } from '../utils/get-cache-key';
|
||||
import { parse as toXML } from 'js2xmlparser';
|
||||
import { getCacheControlHeader } from '../utils/get-cache-headers';
|
||||
|
||||
export const respond: RequestHandler = asyncHandler(async (req, res) => {
|
||||
if (
|
||||
@@ -19,20 +20,12 @@ export const respond: RequestHandler = asyncHandler(async (req, res) => {
|
||||
const key = getCacheKey(req);
|
||||
await cache.set(key, res.locals.payload, ms(env.CACHE_TTL as string));
|
||||
await cache.set(`${key}__expires_at`, Date.now() + ms(env.CACHE_TTL as string));
|
||||
|
||||
const noCacheRequested =
|
||||
req.headers['cache-control']?.includes('no-cache') || req.headers['Cache-Control']?.includes('no-cache');
|
||||
|
||||
// Set cache-control header
|
||||
if (env.CACHE_AUTO_PURGE !== true && noCacheRequested === false) {
|
||||
const maxAge = `max-age=${ms(env.CACHE_TTL as string)}`;
|
||||
const access = !!req.accountability?.role === false ? 'public' : 'private';
|
||||
res.setHeader('Cache-Control', `${access}, ${maxAge}`);
|
||||
}
|
||||
|
||||
if (noCacheRequested) {
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
}
|
||||
res.setHeader('Cache-Control', getCacheControlHeader(req, ms(env.CACHE_TTL as string)));
|
||||
res.setHeader('Vary', 'Origin, Cache-Control');
|
||||
} else {
|
||||
// Don't cache anything by default
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Vary', 'Origin, Cache-Control');
|
||||
}
|
||||
|
||||
if (req.sanitizedQuery.export) {
|
||||
|
||||
Reference in New Issue
Block a user