-
Notifications
You must be signed in to change notification settings - Fork 409
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
- Loading branch information
Showing
6 changed files
with
171 additions
and
13 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,53 @@ | ||
import cacheControl from 'cache-control-parser' | ||
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( | ||
cacheControl.stringify({ | ||
'max-age': 1800, | ||
's-maxage': 600, | ||
private: true, | ||
}), | ||
) | ||
}) | ||
test('retains boolean directive', () => { | ||
const result = cacheControl.parse( | ||
getConservativeCacheControl('private', 'no-cache,no-store'), | ||
) | ||
|
||
expect(result.private).toEqual(true) | ||
expect(result['no-cache']).toEqual(true) | ||
expect(result['no-store']).toEqual(true) | ||
}) | ||
test('gets smallest number directive', () => { | ||
const result = cacheControl.parse( | ||
getConservativeCacheControl( | ||
'max-age=10, s-maxage=300', | ||
'max-age=300, s-maxage=600', | ||
), | ||
) | ||
|
||
expect(result['max-age']).toEqual(10) | ||
expect(result['s-maxage']).toEqual(300) | ||
}) | ||
test('lets unset directives remain unset', () => { | ||
const result = cacheControl.parse( | ||
getConservativeCacheControl( | ||
'max-age=3600', | ||
'max-age=1800, s-maxage=600', | ||
'private, max-age=86400', | ||
), | ||
) | ||
|
||
expect(result['must-revalidate']).toBeUndefined() | ||
expect(result['stale-while-revalidate']).toBeUndefined() | ||
}) | ||
}) |
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,106 @@ | ||
import { type HeadersArgs } from '@remix-run/node' | ||
import { parse, stringify, type CacheControl } from 'cache-control-parser' | ||
|
||
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 stringify( | ||
cacheControlHeaders | ||
.filter(Boolean) | ||
.map((header) => parse(header)) | ||
.reduce<CacheControl>((acc, current) => { | ||
let directive: keyof CacheControl | ||
for (directive in current) { | ||
const currentValue = current[directive] | ||
|
||
// ts-expect-error because typescript doesn't know it's the same directive. | ||
switch (typeof currentValue) { | ||
case 'boolean': { | ||
if (currentValue) { | ||
// @ts-expect-error | ||
acc[directive] = true | ||
} | ||
|
||
break | ||
} | ||
case 'number': { | ||
const accValue = acc[directive] as number | undefined | ||
|
||
if (accValue === undefined) { | ||
// @ts-expect-error | ||
acc[directive] = currentValue | ||
} else { | ||
const result = Math.min(accValue, currentValue) | ||
// @ts-expect-error | ||
acc[directive] = result | ||
} | ||
|
||
break | ||
} | ||
} | ||
} | ||
|
||
return acc | ||
}, {}), | ||
) | ||
} |
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