-
-
Notifications
You must be signed in to change notification settings - Fork 10
perf(problems): cache anonymous /problems responses at the CDN edge #3667
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
+179
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce00537
perf(problems): cache anonymous /problems responses at the CDN edge
KATO-Hiro 3b4310b
test(problems): add logged-in CDN cache exclusion test and rule for r…
KATO-Hiro 2086b34
docs: remove Phase 3 sub-plan after implementation complete
KATO-Hiro be64273
test(problems): tighten cache-control assertions to full header value
KATO-Hiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,44 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| import { loginAsUser } from './helpers/auth'; | ||
|
|
||
| test.describe('anonymous /problems response', () => { | ||
| test('is cache-eligible (cache-control set, no set-cookie)', async ({ page }) => { | ||
| const response = await page.goto('/problems'); | ||
|
|
||
| if (!response) { | ||
| throw new Error('No response received from /problems'); | ||
| } | ||
|
|
||
| const headers = response.headers(); | ||
|
|
||
| // Local dev server (pnpm preview) is not behind Vercel edge, so s-maxage is visible. | ||
| expect(headers['cache-control']).toContain('public'); | ||
| expect(headers['cache-control']).toContain('max-age=0'); | ||
| expect(headers['cache-control']).toContain('s-maxage=300'); | ||
| expect(headers['cache-control']).toContain('stale-while-revalidate=600'); | ||
|
|
||
| // set-cookie makes a response ineligible for CDN caching. | ||
| // Lucia must not attach a session cookie to anonymous requests. | ||
| // If this assertion fails after a Lucia upgrade, verify it does not | ||
| // set cookies on unauthenticated requests. | ||
| expect(headers['set-cookie']).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe('logged-in /problems response', () => { | ||
| test('is not shared-cached (no CDN cache-control directive)', async ({ page }) => { | ||
| await loginAsUser(page); | ||
| const response = await page.goto('/problems'); | ||
|
|
||
| if (!response) { | ||
| throw new Error('No response received from /problems'); | ||
| } | ||
|
|
||
| const headers = response.headers(); | ||
|
|
||
| // Personalized responses must never be shared-cached. | ||
| expect(headers['cache-control'] ?? '').not.toContain('public'); | ||
| expect(headers['cache-control'] ?? '').not.toContain('s-maxage'); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,107 @@ | ||
| import { describe, test, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| import { Roles } from '$lib/types/user'; | ||
|
|
||
| vi.mock('$features/votes/services/vote_statistics', () => ({ | ||
| getVoteGradeStatistics: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('$lib/services/task_results', () => ({ | ||
| getTaskResults: vi.fn(), | ||
| getTasksWithTagIds: vi.fn(), | ||
| })); | ||
|
|
||
| import * as voteStatsModule from '$features/votes/services/vote_statistics'; | ||
| import * as taskCrud from '$lib/services/task_results'; | ||
| import { load } from './+page.server'; | ||
|
|
||
| const mockGetVoteGradeStatistics = vi.mocked(voteStatsModule.getVoteGradeStatistics); | ||
| const mockGetTaskResults = vi.mocked(taskCrud.getTaskResults); | ||
| const mockGetTasksWithTagIds = vi.mocked(taskCrud.getTasksWithTagIds); | ||
|
|
||
| type MockSession = { user: { userId: string; username: string; role: Roles } } | null; | ||
|
|
||
| const createMockEvent = ({ | ||
| session = null, | ||
| tagIds = null, | ||
| }: { | ||
| session?: MockSession; | ||
| tagIds?: string | null; | ||
| } = {}) => { | ||
| const setHeaders = vi.fn(); | ||
| const locals = { | ||
| auth: { validate: vi.fn().mockResolvedValue(session) }, | ||
| user: session | ||
| ? { | ||
| id: session.user.userId, | ||
| name: session.user.username, | ||
| role: session.user.role, | ||
| atcoder_name: '', | ||
| is_validated: false, | ||
| } | ||
| : undefined, | ||
| }; | ||
| const url = { searchParams: { get: vi.fn().mockReturnValue(tagIds) } }; | ||
|
|
||
| return { locals, url, setHeaders } as unknown as Parameters<typeof load>[0] & { | ||
| setHeaders: ReturnType<typeof vi.fn>; | ||
| }; | ||
| }; | ||
|
|
||
| const LOGGED_IN_SESSION: MockSession = { | ||
| user: { userId: 'user-abc123', username: 'testuser', role: Roles.USER }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockGetTaskResults.mockResolvedValue([]); | ||
| mockGetTasksWithTagIds.mockResolvedValue([]); | ||
| mockGetVoteGradeStatistics.mockResolvedValue(new Map()); | ||
| }); | ||
|
|
||
| describe('load() cache-control behaviour', () => { | ||
| describe('sets cache-control', () => { | ||
| test('anonymous users get a public shared-cache header when vote stats succeed', async () => { | ||
| const event = createMockEvent({ session: null }); | ||
|
|
||
| await load(event); | ||
|
|
||
| expect(event.setHeaders).toHaveBeenCalledOnce(); | ||
| const headerArg = event.setHeaders.mock.calls[0][0] as Record<string, string>; | ||
| expect(headerArg['Cache-Control']).toBe( | ||
| 'public, max-age=0, s-maxage=300, stale-while-revalidate=600', | ||
| ); | ||
| }); | ||
|
|
||
| test('anonymous users with tagIds also get a public shared-cache header', async () => { | ||
| const event = createMockEvent({ session: null, tagIds: 'abc,dp' }); | ||
|
|
||
| await load(event); | ||
|
|
||
| expect(event.setHeaders).toHaveBeenCalledOnce(); | ||
| const headerArg = event.setHeaders.mock.calls[0][0] as Record<string, string>; | ||
| expect(headerArg['Cache-Control']).toBe( | ||
| 'public, max-age=0, s-maxage=300, stale-while-revalidate=600', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('does not set cache-control', () => { | ||
| test('logged-in users — personalized response must never be shared-cached', async () => { | ||
| const event = createMockEvent({ session: LOGGED_IN_SESSION }); | ||
|
|
||
| await load(event); | ||
|
|
||
| expect(event.setHeaders).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('degraded response when vote stats fail — avoids pinning a broken page at the CDN', async () => { | ||
| mockGetVoteGradeStatistics.mockRejectedValue(new Error('DB timeout')); | ||
| const event = createMockEvent({ session: null }); | ||
|
|
||
| await load(event); | ||
|
|
||
| expect(event.setHeaders).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.