-
Notifications
You must be signed in to change notification settings - Fork 592
CACHE-13371: more cache knobs under request.cf object #6300
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
yj7o5
wants to merge
2
commits into
cloudflare:main
Choose a base branch
from
yj7o5:yawar/CACHE-13371_add-cf-cache-control-field
base: main
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.
+358
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,184 @@ | ||
| // Copyright (c) 2025 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| // Tests for cf.cacheControl mutual exclusion, synthesis, and additional cache settings. | ||
| // These require the workerd_experimental compat flag. | ||
|
|
||
| import assert from 'node:assert'; | ||
|
|
||
| // Tests for cf.cacheControl mutual exclusion and synthesis. | ||
| // These tests run regardless of cache option flag since cacheControl is always available. | ||
| export const cacheControlMutualExclusion = { | ||
| async test(ctrl, env, ctx) { | ||
| // cacheControl + cacheTtl → TypeError at construction time | ||
| assert.throws( | ||
| () => | ||
| new Request('https://example.org', { | ||
| cf: { cacheControl: 'max-age=300', cacheTtl: 300 }, | ||
| }), | ||
| { | ||
| name: 'TypeError', | ||
| message: /cacheControl.*cacheTtl.*mutually exclusive/, | ||
| } | ||
| ); | ||
|
|
||
| // cacheControl alone should succeed | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheControl: 'public, max-age=3600' }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| } | ||
|
|
||
| // cacheTtl alone should succeed | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheTtl: 300 }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| } | ||
|
|
||
| // cacheControl + cacheTtlByStatus should succeed (not mutually exclusive) | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { | ||
| cacheControl: 'public, max-age=3600', | ||
| cacheTtlByStatus: { '200-299': 86400 }, | ||
| }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| } | ||
|
|
||
| // cacheControl with undefined cacheTtl should succeed (only non-undefined triggers conflict) | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheControl: 'max-age=300', cacheTtl: undefined }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| export const cacheControlWithCacheOption = { | ||
| async test(ctrl, env, ctx) { | ||
| if (!env.CACHE_ENABLED) return; | ||
|
|
||
| // cache option + cf.cacheControl → TypeError at construction time | ||
| assert.throws( | ||
| () => | ||
| new Request('https://example.org', { | ||
| cache: 'no-store', | ||
| cf: { cacheControl: 'no-cache' }, | ||
| }), | ||
| { | ||
| name: 'TypeError', | ||
| message: /cacheControl.*cannot be used together with the.*cache/, | ||
| } | ||
| ); | ||
|
|
||
| // cache: 'no-cache' + cf.cacheControl → also TypeError | ||
| // (need cache_no_cache flag for this, skip if not available) | ||
| }, | ||
| }; | ||
|
|
||
| export const cacheControlSynthesis = { | ||
| async test(ctrl, env, ctx) { | ||
| // When cacheTtl is set without cacheControl, cacheControl should be synthesized | ||
| // in the serialized cf blob. We verify by checking the cf property roundtrips correctly. | ||
|
|
||
| // cacheTtl: 300 → cacheControl should be synthesized as "max-age=300" | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheTtl: 300 }, | ||
| }); | ||
| // The cf object at construction time won't have cacheControl yet — | ||
| // synthesis happens at serialization (fetch) time in serializeCfBlobJson. | ||
| // We can verify the request constructs fine. | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheTtl, 300); | ||
| } | ||
|
|
||
| // cacheTtl: -1 → cacheControl should be synthesized as "no-store" | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheTtl: -1 }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheTtl, -1); | ||
| } | ||
|
|
||
| // cacheTtl: 0 → cacheControl should be synthesized as "max-age=0" | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheTtl: 0 }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheTtl, 0); | ||
| } | ||
|
|
||
| // Explicit cacheControl should NOT be overwritten | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { cacheControl: 'public, s-maxage=86400' }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheControl, 'public, s-maxage=86400'); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| export const additionalCacheSettings = { | ||
| async test(ctrl, env, ctx) { | ||
| // All additional cache settings should be accepted on the cf object | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { | ||
| cacheReserveEligible: true, | ||
| respectStrongEtag: true, | ||
| stripEtags: false, | ||
| stripLastModified: false, | ||
| cacheDeceptionArmor: true, | ||
| cacheReserveMinimumFileSize: 1024, | ||
| }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheReserveEligible, true); | ||
| assert.strictEqual(req.cf.respectStrongEtag, true); | ||
| assert.strictEqual(req.cf.stripEtags, false); | ||
| assert.strictEqual(req.cf.stripLastModified, false); | ||
| assert.strictEqual(req.cf.cacheDeceptionArmor, true); | ||
| assert.strictEqual(req.cf.cacheReserveMinimumFileSize, 1024); | ||
| } | ||
|
|
||
| // Additional cache settings should work alongside cacheControl | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { | ||
| cacheControl: 'public, max-age=3600', | ||
| cacheReserveEligible: true, | ||
| stripEtags: true, | ||
| }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheControl, 'public, max-age=3600'); | ||
| assert.strictEqual(req.cf.cacheReserveEligible, true); | ||
| assert.strictEqual(req.cf.stripEtags, true); | ||
| } | ||
|
|
||
| // Additional cache settings should work alongside cacheTtl | ||
| { | ||
| const req = new Request('https://example.org', { | ||
| cf: { | ||
| cacheTtl: 300, | ||
| respectStrongEtag: true, | ||
| cacheDeceptionArmor: true, | ||
| }, | ||
| }); | ||
| assert.ok(req.cf); | ||
| assert.strictEqual(req.cf.cacheTtl, 300); | ||
| assert.strictEqual(req.cf.respectStrongEtag, true); | ||
| assert.strictEqual(req.cf.cacheDeceptionArmor, true); | ||
| } | ||
| }, | ||
| }; |
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,28 @@ | ||
| using Workerd = import "/workerd/workerd.capnp"; | ||
|
|
||
| const unitTests :Workerd.Config = ( | ||
| services = [ | ||
| ( name = "cf-cache-control-test", | ||
| worker = ( | ||
| modules = [ | ||
| ( name = "worker", esModule = embed "cf-cache-control-test.js" ) | ||
| ], | ||
| bindings = [ | ||
| ( name = "CACHE_ENABLED", json = "false" ), | ||
| ], | ||
| compatibilityFlags = ["nodejs_compat", "cache_option_disabled", "experimental"], | ||
| ) | ||
| ), | ||
| ( name = "cf-cache-control-test-cache-enabled", | ||
| worker = ( | ||
| modules = [ | ||
| ( name = "worker-cache-enabled", esModule = embed "cf-cache-control-test.js" ) | ||
| ], | ||
| bindings = [ | ||
| ( name = "CACHE_ENABLED", json = "true" ), | ||
| ], | ||
| compatibilityFlags = ["nodejs_compat", "cache_option_enabled", "experimental"], | ||
| ) | ||
| ), | ||
| ], | ||
| ); |
Oops, something went wrong.
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.