-
Notifications
You must be signed in to change notification settings - Fork 28
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
DO NOT MERGE YET - HTTP Caching API #1018
base: main
Are you sure you want to change the base?
Conversation
Starting by committing proposed TypeScript types for the API surface. The design and behavior are very similar to what's in the Rust SDK, described in the developer documentation: Customizing cache interaction with the backend |
de12dc4
to
4b17ebe
Compare
ExamplesInject headers before sendingSometimes it is useful to perform modifications to the incoming // Example: inject headers before sending.
const request = event.request;
const response = await fetch(request, {
backend: 'example',
cacheOverride: new CacheOverride('override', {
onBeforeSend(req) {
// Assume buildAuthorizationHeader() may be expensive, so
// we only want to call this when the request would actually reach the backend.
req.headers.set('Authorization', buildAuthorizationHeader());
},
}),
}); Customize caching based on content typeSometimes it is useful to modify caching policy based on the backend response. Specify The This example shows usages that utilize these members of
// Example: customizing caching based on content type.
const request = event.request;
const response = await fetch(request, {
backend: 'example',
cacheOverride: new CacheOverride('override', {
onAfterSend(resp) {
const contentType = resp.headers.get('Content-Type') ?? '';
switch(true) {
case contentType.startsWith('image/'):
resp.ttl = 67;
break;
case contentType === 'text/html':
resp.ttl = 321;
break;
case contentType === 'application/json':
// setUncacheable() with no param (default false) marks this object as uncacheable
// without disabling request collapsing
resp.setUncacheable();
break;
default:
resp.ttl = 2;
}
},
}),
}); Creating a hit-for-pass objectBy specifying // Example: creating a hit-for-pass object.
const request = event.request;
const response = await fetch(request, {
backend: 'example',
cacheOverride: new CacheOverride('override', {
onAfterSend(resp) {
if (resp.headers.has('my-private-header')) {
// setUncacheable() with true param marks this object as uncacheable
// and marks it as hit-for-pass, resulting in disabling request collapsing
resp.setUncacheable(true);
}
},
}),
}); Manipulating the response body that is stored to the cacheIn an after-send callback, optionally set the Employing The transformation is specified by setting a property ( This design enables the readthrough cache to internally manage the complexities of revalidation, allowing the developer to provide a single code path without needing to think about revalidation at all. // Example: expanding a template before caching.
const request = event.request;
const response = await fetch(request, {
backend: 'example',
cacheOverride: new CacheOverride('override', {
onAfterSend(resp) {
resp.headers.set('Content-Type', 'text/html');
resp.bodyTransform = new TransformStream({
bytes: null,
start() {
this.bytes = new Uint8Array(0);
},
transform(chunk) {
// The ideal transform would transform bytes as it goes over the wire,
// but for a transformation whose input is JSON, we need to buffer the
// bytes to memory, because we need the whole body before we can
// deserialize it.
const newBytes = new Uint8Array(this.bytes.length + chunk.length);
newBytes.set(this.bytes, 0);
newBytes.set(chunk, this.bytes.length);
this.bytes = newBytes;
},
flush(controller) {
const str = new TextDecoder().decode(this.bytes);
// jsonToHtml applies a template to generate HTML from JSON
const html = jsonToHtml(str);
controller.enqueue(new TextEncoder().encode(html));
},
});
},
}),
});
// The resulting cached object will have a body that is HTML
// despite that the backend returned a JSON response:
return response; Notes
|
676f982
to
7e4fd83
Compare
7e4fd83
to
d6763ce
Compare
6aff377
to
206a60e
Compare
This PR tracks the implementation of the HTTP caching API.
Implements #991