Skip to content
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

Prevent caching private responses server-side #834

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/header/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ export const defaultHeaderInterpreter: HeaderInterpreter = (headers) => {
const cacheControl: unknown = headers[Header.CacheControl];

if (cacheControl) {
const { noCache, noStore, maxAge, maxStale, immutable, staleWhileRevalidate } = parse(
String(cacheControl)
);
const {
noCache,
noStore,
maxAge,
maxStale,
immutable,
staleWhileRevalidate,
private: _private
} = parse(String(cacheControl));

// Header told that this response should not be cached.
if (noCache || noStore) {
if (noCache || noStore || (_private && typeof window === 'undefined')) {
return 'dont cache';
}

Expand Down
20 changes: 20 additions & 0 deletions test/header/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ describe('Header Interpreter', () => {
assert.deepEqual(result, { cache: 1000 * 60 * 60 * 24 * 365 });
});

it("Don't cache private, no-cache and no-store", () => {
const privateResult = defaultHeaderInterpreter({
[Header.CacheControl]: 'private'
});

assert.deepEqual(privateResult, 'dont cache');

const noCacheResult = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-cache'
});

assert.deepEqual(noCacheResult, 'dont cache');

const noStoreResult = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-store'
});

assert.deepEqual(noStoreResult, 'dont cache');
});

it('MaxAge=10 and Age=3 and StaleWhileRevalidate Headers', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: 'max-age=10, stale-while-revalidate=5',
Expand Down