-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add util for handling remix headers generally #810
Changes from 3 commits
700dd51
f40e9b0
3f69a4a
8b39737
f0bf6a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { format, parse } from '@tusbar/cache-control' | ||
import { describe, expect, test } from 'vitest' | ||
import { getConservativeCacheControl } from './remix.server.ts' | ||
|
||
describe('getConservativeCacheControl', () => { | ||
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) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { type CacheControlValue, parse, format } from '@tusbar/cache-control' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think calling this file Also, could you add some jsdoc comments to these utilities? Thanks! |
||
import { type HeadersArgs } from 'react-router' | ||
|
||
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 | ||
} | ||
|
||
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 | ||
}, {}), | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to not use
describe
because it's unnecessary. Just put the test blocks at the root of the file.