Skip to content

Commit 13abd85

Browse files
committed
fix(api): correct malformed stale-while-revalidate cache header
1 parent e15a46a commit 13abd85

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

app/api/github/route.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { GET } from './route';
3+
4+
vi.mock('@/lib/github', () => ({
5+
getFullDashboardData: vi.fn(),
6+
}));
7+
8+
import { getFullDashboardData } from '@/lib/github';
9+
10+
function makeRequest(params: Record<string, string> = {}): Request {
11+
const url = new URL('http://localhost/api/github');
12+
13+
for (const [key, value] of Object.entries(params)) {
14+
url.searchParams.set(key, value);
15+
}
16+
17+
return new Request(url.toString());
18+
}
19+
20+
describe('GET /api/github', () => {
21+
beforeEach(() => {
22+
vi.clearAllMocks();
23+
vi.mocked(getFullDashboardData).mockResolvedValue({
24+
profile: { username: 'octocat' },
25+
repositories: [],
26+
languages: [],
27+
insights: [],
28+
commitClock: [],
29+
} as never);
30+
});
31+
32+
it('returns a standards-compliant Cache-Control header', async () => {
33+
const response = await GET(makeRequest({ username: 'octocat' }));
34+
35+
expect(response.status).toBe(200);
36+
expect(response.headers.get('Cache-Control')).toBe(
37+
's-maxage=3600, stale-while-revalidate=86400'
38+
);
39+
});
40+
41+
it('bypasses cache with refresh=true', async () => {
42+
const response = await GET(makeRequest({ username: 'octocat', refresh: 'true' }));
43+
44+
expect(response.headers.get('Cache-Control')).toBe('no-cache, no-store, must-revalidate');
45+
expect(getFullDashboardData).toHaveBeenCalledWith('octocat', { bypassCache: true });
46+
});
47+
});

app/api/github/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function GET(request: Request) {
3737
const data = await getFullDashboardData(username, { bypassCache: refresh });
3838
const cacheControl = refresh
3939
? 'no-cache, no-store, must-revalidate'
40-
: 's-maxage=3600, stale-while-revalidate';
40+
: 's-maxage=3600, stale-while-revalidate=86400';
4141

4242
return NextResponse.json(data, {
4343
status: 200,

0 commit comments

Comments
 (0)