-
Notifications
You must be signed in to change notification settings - Fork 30.9k
perf: cache ETag computation for static/pre-rendered pages #91608
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
Open
benfavre
wants to merge
1
commit into
vercel:canary
Choose a base branch
from
benfavre:perf/cache-etag-static-pages
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−2
Open
Changes from all commits
Commits
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,85 @@ | ||
| /* eslint-env jest */ | ||
| import { fnv1a52, generateETag } from 'next/dist/server/lib/etag' | ||
|
|
||
| describe('fnv1a52', () => { | ||
| test('returns a number', () => { | ||
| expect(typeof fnv1a52('hello')).toBe('number') | ||
| }) | ||
|
|
||
| test('produces consistent hashes', () => { | ||
| const hash1 = fnv1a52('test payload') | ||
| const hash2 = fnv1a52('test payload') | ||
| expect(hash1).toBe(hash2) | ||
| }) | ||
|
|
||
| test('produces different hashes for different inputs', () => { | ||
| const hash1 = fnv1a52('hello') | ||
| const hash2 = fnv1a52('world') | ||
| expect(hash1).not.toBe(hash2) | ||
| }) | ||
| }) | ||
|
|
||
| describe('generateETag', () => { | ||
| test('generates a strong ETag by default', () => { | ||
| const etag = generateETag('test') | ||
| expect(etag).toMatch(/^"[a-z0-9]+"$/) | ||
| expect(etag).not.toMatch(/^W\//) | ||
| }) | ||
|
|
||
| test('generates a weak ETag when requested', () => { | ||
| const etag = generateETag('test', true) | ||
| expect(etag).toMatch(/^W\/"[a-z0-9]+"$/) | ||
| }) | ||
|
|
||
| test('produces consistent ETags for the same payload', () => { | ||
| const etag1 = generateETag('hello world') | ||
| const etag2 = generateETag('hello world') | ||
| expect(etag1).toBe(etag2) | ||
| }) | ||
|
|
||
| test('produces different ETags for different payloads', () => { | ||
| const etag1 = generateETag('hello') | ||
| const etag2 = generateETag('world') | ||
| expect(etag1).not.toBe(etag2) | ||
| }) | ||
|
|
||
| test('strong and weak ETags for the same payload differ', () => { | ||
| const strong = generateETag('test payload') | ||
| const weak = generateETag('test payload', true) | ||
| expect(strong).not.toBe(weak) | ||
| expect(weak.startsWith('W/"')).toBe(true) | ||
| expect(strong.startsWith('"')).toBe(true) | ||
| }) | ||
|
|
||
| test('returns cached result on repeated calls (same reference)', () => { | ||
| const payload = 'cached-payload-test-' + Date.now() | ||
| // First call computes | ||
| const etag1 = generateETag(payload) | ||
| // Second call should hit cache and return identical result | ||
| const etag2 = generateETag(payload) | ||
| expect(etag1).toBe(etag2) | ||
| }) | ||
|
|
||
| test('handles empty string', () => { | ||
| const etag = generateETag('') | ||
| expect(etag).toMatch(/^"[a-z0-9]+"$/) | ||
| }) | ||
|
|
||
| test('handles large payloads', () => { | ||
| const large = 'x'.repeat(100_000) | ||
| const etag = generateETag(large) | ||
| expect(etag).toMatch(/^"[a-z0-9]+"$/) | ||
| // Repeated call for the same large payload should return identical result | ||
| expect(generateETag(large)).toBe(etag) | ||
| }) | ||
|
|
||
| test('does not cache payloads exceeding the size threshold', () => { | ||
| // Payloads over 512KB are not cached, but should still produce valid ETags | ||
| const huge = 'y'.repeat(600_000) | ||
| const etag1 = generateETag(huge) | ||
| const etag2 = generateETag(huge) | ||
| expect(etag1).toMatch(/^"[a-z0-9]+"$/) | ||
| // Both should produce the same ETag (just not cached) | ||
| expect(etag1).toBe(etag2) | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache key collision between strong and weak ETags when a strong ETag payload starts with "w\0", causing incorrect cached ETag values to be returned.