Caching a Vercel endpoint using cloudflare workers

We are trying to simply cache a nextjs endpoint hosted on vercel. We are using the standard code from the docs (https://developers.cloudflare.com/workers/examples/cache-api/):
interface Env {}
export default {
async fetch(request, env, ctx): Promise<Response> {
const cacheUrl = new URL(request.url);

// Construct the cache key from the cache URL
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;

// Check whether the value is already available in the cache
// if not, you will need to fetch it from origin, and store it in the cache
let response = await cache.match(cacheKey);

if (!response) {
console.log(
`Response for request url: ${request.url} not present in cache. Fetching and caching request.`,
);
// If not in cache, get it from origin
response = await fetch(request);

// Must use Response constructor to inherit all of response's fields
response = new Response(response.body, response);

// Cache API respects Cache-Control headers. Setting s-max-age to 10
// will limit the response to be in cache for 10 seconds max

// Any changes made to the response here will be reflected in the cached value
response.headers.append("Cache-Control", "s-maxage=10");

ctx.waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
},
} satisfies ExportedHandler<Env>;
interface Env {}
export default {
async fetch(request, env, ctx): Promise<Response> {
const cacheUrl = new URL(request.url);

// Construct the cache key from the cache URL
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;

// Check whether the value is already available in the cache
// if not, you will need to fetch it from origin, and store it in the cache
let response = await cache.match(cacheKey);

if (!response) {
console.log(
`Response for request url: ${request.url} not present in cache. Fetching and caching request.`,
);
// If not in cache, get it from origin
response = await fetch(request);

// Must use Response constructor to inherit all of response's fields
response = new Response(response.body, response);

// Cache API respects Cache-Control headers. Setting s-max-age to 10
// will limit the response to be in cache for 10 seconds max

// Any changes made to the response here will be reflected in the cached value
response.headers.append("Cache-Control", "s-maxage=10");

ctx.waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
},
} satisfies ExportedHandler<Env>;
When I then go to my local wrangler worker and hit the endpoint that forwards to my nextjs app at localhost:8000 I get this error immediately:
Error
An error has occurred
Network connection lost.

at async Object.fetch....
Error
An error has occurred
Network connection lost.

at async Object.fetch....
Does anyone know what might be the issue here?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?