Skip to content

Commit

Permalink
feat: allow private caching with auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
codename-niels committed Aug 5, 2024
1 parent d4f2f6a commit 2bcea6d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
30 changes: 20 additions & 10 deletions npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function createCacheControlResponse(
redisUrl: string,
opt: Partial<HeaderOptions>,
request: Request,
response: Response | (() => MaybePromise<Response>) | null = null,
response: Response | (() => MaybePromise<Response>) | null = null
) {
const options = {
...DEFAULT_HEADER_OPTIONS,
Expand All @@ -74,10 +74,14 @@ export async function createCacheControlResponse(
const headers: Record<string, string> = {};

if (
!request.headers.has('Authorization') &&
options.noCacheSearchParams.every((param) => !new URL(request.url).searchParams.has(param))
options.noCacheSearchParams.every(
(param) => !new URL(request.url).searchParams.has(param)
)
) {
if (options.etagCacheKey && redis) {
const allowPublicCaching =
options.public && !request.headers.has('Authorization');

if (allowPublicCaching && options.etagCacheKey && redis) {
const etag = await redis.get(options.etagCacheKey);

if (etag) {
Expand All @@ -95,7 +99,7 @@ export async function createCacheControlResponse(

headers['Cache-Control'] = [
options.private && 'private',
options.public && 'public',
allowPublicCaching && 'public',
options.maxAge !== null && `max-age=${options.maxAge}`,
options.sMaxAge !== null && `s-maxage=${options.maxAge}`,
options.mustRevalidate && 'must-revalidate',
Expand All @@ -107,8 +111,8 @@ export async function createCacheControlResponse(
const resp = !response
? new Response()
: typeof response === 'function'
? await response()
: response;
? await response()
: response;

if (resp.status !== 200) return resp;

Expand All @@ -119,7 +123,10 @@ export async function createCacheControlResponse(
return resp;
}

export function cacheControlHandle(redisUrl: string, opt: Partial<HandleOptions>): Handle {
export function cacheControlHandle(
redisUrl: string,
opt: Partial<HandleOptions>
): Handle {
const options = {
...DEFAULT_HANDLE_OPTIONS,
...opt,
Expand All @@ -138,8 +145,11 @@ export function cacheControlHandle(redisUrl: string, opt: Partial<HandleOptions>
options.methods.includes(event.request.method) &&
options.routes.some((route) => new RegExp(route).test(event.url.pathname))
) {
return await createCacheControlResponse(redisUrl, options, event.request, () =>
resolve(event),
return await createCacheControlResponse(
redisUrl,
options,
event.request,
() => resolve(event)
);
}

Expand Down
4 changes: 2 additions & 2 deletions npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@born05/sveltekit-cache-control",
"version": "1.2.1",
"version": "1.2.2",
"description": "A simple way to add control caching in your SvelteKit project.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 2bcea6d

Please sign in to comment.