|
| 1 | +// src/middlewares/cache-control.middleware.ts |
| 2 | +import { Request, Response, NextFunction } from 'express'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Cache control options for different types of endpoints. |
| 6 | + */ |
| 7 | +export interface CacheControlOptions { |
| 8 | + /** |
| 9 | + * Max age in seconds. Default: 300 (5 minutes) |
| 10 | + */ |
| 11 | + maxAge?: number; |
| 12 | + /** |
| 13 | + * Whether the cache is public (CDN can cache) or private (browser only). |
| 14 | + * Default: 'public' |
| 15 | + */ |
| 16 | + type?: 'public' | 'private'; |
| 17 | + /** |
| 18 | + * Whether to include must-revalidate directive. |
| 19 | + * Default: false |
| 20 | + */ |
| 21 | + mustRevalidate?: boolean; |
| 22 | + /** |
| 23 | + * Whether to include no-cache directive (requires revalidation). |
| 24 | + * Default: false |
| 25 | + */ |
| 26 | + noCache?: boolean; |
| 27 | + /** |
| 28 | + * Whether to disable caching entirely. |
| 29 | + * Default: false |
| 30 | + */ |
| 31 | + noStore?: boolean; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Middleware factory that adds Cache-Control headers to responses. |
| 36 | + * |
| 37 | + * Applies only to GET requests to avoid caching mutations. |
| 38 | + * Keeps cache behavior explicit and easy to understand in code. |
| 39 | + * |
| 40 | + * @param options - Cache control configuration |
| 41 | + * |
| 42 | + * @example |
| 43 | + * // Public endpoint with 5-minute cache |
| 44 | + * router.get('/creators', cacheControl({ maxAge: 300 }), listCreators); |
| 45 | + * |
| 46 | + * @example |
| 47 | + * // No caching for sensitive data |
| 48 | + * router.get('/profile', cacheControl({ noStore: true }), getProfile); |
| 49 | + */ |
| 50 | +export function cacheControl(options: CacheControlOptions = {}) { |
| 51 | + const { |
| 52 | + maxAge = 300, |
| 53 | + type = 'public', |
| 54 | + mustRevalidate = false, |
| 55 | + noCache = false, |
| 56 | + noStore = false, |
| 57 | + } = options; |
| 58 | + |
| 59 | + return (req: Request, res: Response, next: NextFunction): void => { |
| 60 | + // Only apply cache headers to GET requests |
| 61 | + // Mutation routes (POST, PUT, DELETE, PATCH) remain unaffected |
| 62 | + if (req.method !== 'GET') { |
| 63 | + return next(); |
| 64 | + } |
| 65 | + |
| 66 | + // Build Cache-Control header value |
| 67 | + const directives: string[] = []; |
| 68 | + |
| 69 | + if (noStore) { |
| 70 | + directives.push('no-store'); |
| 71 | + } else if (noCache) { |
| 72 | + directives.push('no-cache'); |
| 73 | + } else { |
| 74 | + directives.push(type); |
| 75 | + directives.push(`max-age=${maxAge}`); |
| 76 | + if (mustRevalidate) { |
| 77 | + directives.push('must-revalidate'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + res.setHeader('Cache-Control', directives.join(', ')); |
| 82 | + next(); |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Preset cache configurations for common use cases. |
| 88 | + */ |
| 89 | +export const CachePresets = { |
| 90 | + /** |
| 91 | + * Short cache for frequently updated public data (5 minutes) |
| 92 | + */ |
| 93 | + publicShort: { maxAge: 300, type: 'public' as const }, |
| 94 | + |
| 95 | + /** |
| 96 | + * Medium cache for moderately stable public data (1 hour) |
| 97 | + */ |
| 98 | + publicMedium: { maxAge: 3600, type: 'public' as const }, |
| 99 | + |
| 100 | + /** |
| 101 | + * Long cache for stable public data (24 hours) |
| 102 | + */ |
| 103 | + publicLong: { maxAge: 86400, type: 'public' as const }, |
| 104 | + |
| 105 | + /** |
| 106 | + * Private cache for user-specific data (5 minutes) |
| 107 | + */ |
| 108 | + private: { maxAge: 300, type: 'private' as const }, |
| 109 | + |
| 110 | + /** |
| 111 | + * No caching for sensitive or dynamic data |
| 112 | + */ |
| 113 | + noCache: { noStore: true }, |
| 114 | +}; |
0 commit comments