Skip to content

Commit

Permalink
fix: cacheControlOverride not being overridden correctly (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
aui authored Apr 19, 2024
1 parent 080c4c1 commit d1aa8b2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-hats-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-widget/shared-cache": patch
---

Fix `cacheControlOverride` not being overridden correctly.
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,8 @@
}
}
},
"type": "module"
"type": "module",
"files": [
"dist"
]
}
4 changes: 4 additions & 0 deletions src/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('override headers', () => {
});

expect(res.headers.get('cache-control')).toBe('max-age=300, s-maxage=120');
expect(res.headers.get('x-cache-status')).toBe(MISS);
});

test('the `vary` header should be overridable', async () => {
Expand All @@ -55,6 +56,7 @@ describe('override headers', () => {
return new Response('lol', {
headers: {
vary: 'x-custom',
'cache-control': 'max-age=300',
},
});
},
Expand All @@ -66,6 +68,7 @@ describe('override headers', () => {
});

expect(res.headers.get('vary')).toBe('x-custom, accept-language');
expect(res.headers.get('x-cache-status')).toBe(MISS);
});

test('do not set HTTP headers when status code is greater than or equal to 500', async () => {
Expand All @@ -87,6 +90,7 @@ describe('override headers', () => {

expect(res.headers.get('cache-control')).toBe(null);
expect(res.headers.get('vary')).toBe(null);
expect(res.headers.get('x-cache-status')).toBe(DYNAMIC);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export function createSharedCacheFetch(
}

const fetchedResponse = await fetcher(request);
const cacheControl = fetchedResponse.headers.get('cache-control');
setCacheControlAndVary(fetchedResponse, sharedCache);
const cacheControl = fetchedResponse.headers.get('cache-control');

if (cacheControl) {
if (bypassCache(cacheControl)) {
Expand Down

0 comments on commit d1aa8b2

Please sign in to comment.