Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* HTTP Cache Header Utilities
*
* Provides functions to add proper cache headers to API responses.
* Supports Cache-Control, ETag, and conditional requests (304 Not Modified).
*/
import { NextResponse } from "next/server";
import * as crypto from "crypto";
export interface CacheOptions {
// Max age in seconds for browser/CDN cache
maxAge?: number;
// Stale-while-revalidate seconds
staleWhileRevalidate?: number;
// Whether this is private (user-specific) data
private?: boolean;
// Optional ETag for conditional requests
etag?: string;
// Allow response to be stored in shared caches
public?: boolean;
// Response must not be cached
noStore?: boolean;
// Response must not be served stale
mustRevalidate?: boolean;
}
/**
* Add cache headers to a NextResponse
* @param response The response to add headers to
* @param options Cache configuration options
* @returns The response with cache headers
*/
export function withCacheHeaders(
response: NextResponse,
options: CacheOptions
): NextResponse {
const {
maxAge = 0,
staleWhileRevalidate = 0,
private: isPrivate = false,
public: isPublic = false,
noStore = false,
mustRevalidate = false,
etag} = options;
// Build Cache-Control header
const directives: string[] = [];
if (noStore) {
directives.push("no-store");
} else {
if (isPrivate) {
directives.push("private");
} else if (isPublic) {
directives.push("public");
}
directives.push(`max-age=${maxAge}`);
if (staleWhileRevalidate > 0) {
directives.push(`stale-while-revalidate=${staleWhileRevalidate}`);
}
if (mustRevalidate) {
directives.push("must-revalidate");
}
}
response.headers.set("Cache-Control", directives.join(", "));
// Add ETag if provided
if (etag) {
response.headers.set("ETag", `"${etag}"`);
}
return response;
}
/**
* Generate an ETag from data
* @param data Any serializable data
* @returns A short hash string suitable for ETag
*/
export function generateETag(data: unknown): string {
const hash = crypto
.createHash("md5")
.update(JSON.stringify(data))
.digest("hex");
return hash.substring(0, 16);
}
/**
* Check if the client's If-None-Match header matches our ETag
* @param request The incoming request (can be undefined in tests)
* @param etag The current ETag value
* @returns true if the ETags match (client can use cached version)
*/
export function checkETagMatch(request: Request | undefined | null, etag: string): boolean {
if (!request?.headers) {
return false;
}
const ifNoneMatch = request.headers.get("If-None-Match");
return ifNoneMatch === `"${etag}"`;
}
/**
* Create a 304 Not Modified response
*/
export function notModifiedResponse(): NextResponse {
return new NextResponse(null, { status: 304 });
}
/**
* Helper to create a cached JSON response with ETag support
* @param request The incoming request (can be undefined in tests)
* @param data The data to send
* @param options Cache options
* @returns A NextResponse with cache headers, or 304 if ETag matches
*/
export function cachedJsonResponse<T>(
request: Request | undefined | null,
data: T,
options: CacheOptions
): NextResponse {
const etag = generateETag(data);
// Check if client has current version
if (checkETagMatch(request, etag)) {
return notModifiedResponse();
}
// Return new response with cache headers
const response = NextResponse.json(data);
return withCacheHeaders(response, { ...options, etag });
}
// Preset cache configurations for common use cases
export const CACHE_PRESETS = {
// Public static data (categories, filters) - cache for 1 hour
publicStatic: {
public: true,
maxAge: 3600,
staleWhileRevalidate: 86400,
} as CacheOptions,
// Product listings - cache for 1 minute, allow stale for 5 minutes
productList: {
public: true,
maxAge: 60,
staleWhileRevalidate: 300,
} as CacheOptions,
// Product details - cache for 5 minutes
productDetail: {
public: true,
maxAge: 300,
staleWhileRevalidate: 600,
} as CacheOptions,
// User-specific data - private cache only
userPrivate: {
private: true,
maxAge: 60,
} as CacheOptions,
// Search results - short cache
search: {
public: true,
maxAge: 60,
staleWhileRevalidate: 300,
} as CacheOptions,
// No caching
noCache: {
noStore: true,
} as CacheOptions,
};
|