|
| 1 | +import { type CacheControlValue, parse, format } from '@tusbar/cache-control' |
| 2 | +import { type HeadersArgs } from 'react-router' |
| 3 | + |
| 4 | +/** |
| 5 | + * A utility for handling route headers, merging common use-case headers. |
| 6 | + * |
| 7 | + * This function combines headers by: |
| 8 | + * 1. Forwarding headers from the route's loader or action. |
| 9 | + * 2. Inheriting headers from the parent. |
| 10 | + * 3. Falling back to parent headers (if any) when headers are missing. |
| 11 | + */ |
| 12 | +export function pipeHeaders({ |
| 13 | + parentHeaders, |
| 14 | + loaderHeaders, |
| 15 | + actionHeaders, |
| 16 | + errorHeaders, |
| 17 | +}: HeadersArgs) { |
| 18 | + const headers = new Headers() |
| 19 | + |
| 20 | + // get the one that's actually in use |
| 21 | + let currentHeaders: Headers |
| 22 | + if (errorHeaders !== undefined) { |
| 23 | + currentHeaders = errorHeaders |
| 24 | + } else if (loaderHeaders.entries().next().done) { |
| 25 | + currentHeaders = actionHeaders |
| 26 | + } else { |
| 27 | + currentHeaders = loaderHeaders |
| 28 | + } |
| 29 | + |
| 30 | + // take in useful headers route loader/action |
| 31 | + // pass this point currentHeaders can be ignored |
| 32 | + const forwardHeaders = ['Cache-Control', 'Vary', 'Server-Timing'] |
| 33 | + for (const headerName of forwardHeaders) { |
| 34 | + const header = currentHeaders.get(headerName) |
| 35 | + if (header) { |
| 36 | + headers.set(headerName, header) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + headers.set( |
| 41 | + 'Cache-Control', |
| 42 | + getConservativeCacheControl( |
| 43 | + parentHeaders.get('Cache-Control'), |
| 44 | + headers.get('Cache-Control'), |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + // append useful parent headers |
| 49 | + const inheritHeaders = ['Vary', 'Server-Timing'] |
| 50 | + for (const headerName of inheritHeaders) { |
| 51 | + const header = parentHeaders.get(headerName) |
| 52 | + if (header) { |
| 53 | + headers.append(headerName, header) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // fallback to parent headers if loader don't have |
| 58 | + const fallbackHeaders = ['Cache-Control', 'Vary'] |
| 59 | + for (const headerName of fallbackHeaders) { |
| 60 | + if (headers.has(headerName)) { |
| 61 | + continue |
| 62 | + } |
| 63 | + const fallbackHeader = parentHeaders.get(headerName) |
| 64 | + if (fallbackHeader) { |
| 65 | + headers.set(headerName, fallbackHeader) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return headers |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Given multiple Cache-Control headers, merge them and get the most conservative one. |
| 74 | + */ |
| 75 | +export function getConservativeCacheControl( |
| 76 | + ...cacheControlHeaders: Array<string | null> |
| 77 | +): string { |
| 78 | + return format( |
| 79 | + cacheControlHeaders |
| 80 | + .filter(Boolean) |
| 81 | + .map((header) => parse(header)) |
| 82 | + .reduce<CacheControlValue>((acc, current) => { |
| 83 | + for (const key in current) { |
| 84 | + const directive = key as keyof Required<CacheControlValue> // keyof CacheControl includes functions |
| 85 | + |
| 86 | + const currentValue = current[directive] |
| 87 | + |
| 88 | + switch (typeof currentValue) { |
| 89 | + case 'boolean': { |
| 90 | + if (currentValue) { |
| 91 | + acc[directive] = true as any |
| 92 | + } |
| 93 | + |
| 94 | + break |
| 95 | + } |
| 96 | + case 'number': { |
| 97 | + const accValue = acc[directive] as number | undefined |
| 98 | + |
| 99 | + if (accValue === undefined) { |
| 100 | + acc[directive] = currentValue as any |
| 101 | + } else { |
| 102 | + const result = Math.min(accValue, currentValue) |
| 103 | + acc[directive] = result as any |
| 104 | + } |
| 105 | + |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return acc |
| 112 | + }, {}), |
| 113 | + ) |
| 114 | +} |
0 commit comments