// we forward the request to the actual nextjs worker since its not cached
let res = await nextWorker.fetch(req, env, ctx)
if (res.ok && res.body) {
// First, check for compression and decompress if necessary
if (res.headers.get("Content-Encoding")) {
res = new Response(res.body, res)
res.headers.delete("Content-Encoding")
}
// since nextjs streams, we are tee-ing the response to return one part and cache the other copy
const [toClient, toCache] = res.body.tee()
// We are constructing the response to the client
const out = new Response(toClient, {
status: res.status,
statusText: res.statusText,
headers: clientHeaders,
})
// We perform the background cache insert
ctx.waitUntil(
this.cacheResponse(env, cacheKey, toCache, res.status, cacheHeaders)
)
// We expect to return to the client immediately here
return out
// we forward the request to the actual nextjs worker since its not cached
let res = await nextWorker.fetch(req, env, ctx)
if (res.ok && res.body) {
// First, check for compression and decompress if necessary
if (res.headers.get("Content-Encoding")) {
res = new Response(res.body, res)
res.headers.delete("Content-Encoding")
}
// since nextjs streams, we are tee-ing the response to return one part and cache the other copy
const [toClient, toCache] = res.body.tee()
// We are constructing the response to the client
const out = new Response(toClient, {
status: res.status,
statusText: res.statusText,
headers: clientHeaders,
})
// We perform the background cache insert
ctx.waitUntil(
this.cacheResponse(env, cacheKey, toCache, res.status, cacheHeaders)
)
// We expect to return to the client immediately here
return out