-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add util for handling remix headers generally (#810)
Co-authored-by: Kent C. Dodds <[email protected]>
- Loading branch information
1 parent
9fac985
commit 5c5328b
Showing
7 changed files
with
168 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { format, parse } from '@tusbar/cache-control' | ||
import { expect, test } from 'vitest' | ||
import { getConservativeCacheControl } from './headers.server.ts' | ||
|
||
test('works for basic usecase', () => { | ||
const result = getConservativeCacheControl( | ||
'max-age=3600', | ||
'max-age=1800, s-maxage=600', | ||
'private, max-age=86400', | ||
) | ||
|
||
expect(result).toEqual( | ||
format({ | ||
maxAge: 1800, | ||
sharedMaxAge: 600, | ||
private: true, | ||
}), | ||
) | ||
}) | ||
test('retains boolean directive', () => { | ||
const result = parse( | ||
getConservativeCacheControl('private', 'no-cache,no-store'), | ||
) | ||
|
||
expect(result.private).toEqual(true) | ||
expect(result.noCache).toEqual(true) | ||
expect(result.noStore).toEqual(true) | ||
}) | ||
test('gets smallest number directive', () => { | ||
const result = parse( | ||
getConservativeCacheControl( | ||
'max-age=10, s-maxage=300', | ||
'max-age=300, s-maxage=600', | ||
), | ||
) | ||
|
||
expect(result.maxAge).toEqual(10) | ||
expect(result.sharedMaxAge).toEqual(300) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { type CacheControlValue, parse, format } from '@tusbar/cache-control' | ||
import { type HeadersArgs } from 'react-router' | ||
|
||
/** | ||
* A utility for handling route headers, merging common use-case headers. | ||
* | ||
* This function combines headers by: | ||
* 1. Forwarding headers from the route's loader or action. | ||
* 2. Inheriting headers from the parent. | ||
* 3. Falling back to parent headers (if any) when headers are missing. | ||
*/ | ||
export function pipeHeaders({ | ||
parentHeaders, | ||
loaderHeaders, | ||
actionHeaders, | ||
errorHeaders, | ||
}: HeadersArgs) { | ||
const headers = new Headers() | ||
|
||
// get the one that's actually in use | ||
let currentHeaders: Headers | ||
if (errorHeaders !== undefined) { | ||
currentHeaders = errorHeaders | ||
} else if (loaderHeaders.entries().next().done) { | ||
currentHeaders = actionHeaders | ||
} else { | ||
currentHeaders = loaderHeaders | ||
} | ||
|
||
// take in useful headers route loader/action | ||
// pass this point currentHeaders can be ignored | ||
const forwardHeaders = ['Cache-Control', 'Vary', 'Server-Timing'] | ||
for (const headerName of forwardHeaders) { | ||
const header = currentHeaders.get(headerName) | ||
if (header) { | ||
headers.set(headerName, header) | ||
} | ||
} | ||
|
||
headers.set( | ||
'Cache-Control', | ||
getConservativeCacheControl( | ||
parentHeaders.get('Cache-Control'), | ||
headers.get('Cache-Control'), | ||
), | ||
) | ||
|
||
// append useful parent headers | ||
const inheritHeaders = ['Vary', 'Server-Timing'] | ||
for (const headerName of inheritHeaders) { | ||
const header = parentHeaders.get(headerName) | ||
if (header) { | ||
headers.append(headerName, header) | ||
} | ||
} | ||
|
||
// fallback to parent headers if loader don't have | ||
const fallbackHeaders = ['Cache-Control', 'Vary'] | ||
for (const headerName of fallbackHeaders) { | ||
if (headers.has(headerName)) { | ||
continue | ||
} | ||
const fallbackHeader = parentHeaders.get(headerName) | ||
if (fallbackHeader) { | ||
headers.set(headerName, fallbackHeader) | ||
} | ||
} | ||
|
||
return headers | ||
} | ||
|
||
/** | ||
* Given multiple Cache-Control headers, merge them and get the most conservative one. | ||
*/ | ||
export function getConservativeCacheControl( | ||
...cacheControlHeaders: Array<string | null> | ||
): string { | ||
return format( | ||
cacheControlHeaders | ||
.filter(Boolean) | ||
.map((header) => parse(header)) | ||
.reduce<CacheControlValue>((acc, current) => { | ||
for (const key in current) { | ||
const directive = key as keyof Required<CacheControlValue> // keyof CacheControl includes functions | ||
|
||
const currentValue = current[directive] | ||
|
||
switch (typeof currentValue) { | ||
case 'boolean': { | ||
if (currentValue) { | ||
acc[directive] = true as any | ||
} | ||
|
||
break | ||
} | ||
case 'number': { | ||
const accValue = acc[directive] as number | undefined | ||
|
||
if (accValue === undefined) { | ||
acc[directive] = currentValue as any | ||
} else { | ||
const result = Math.min(accValue, currentValue) | ||
acc[directive] = result as any | ||
} | ||
|
||
break | ||
} | ||
} | ||
} | ||
|
||
return acc | ||
}, {}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters