CF worker append cacheTtl value to custom header?

I have CF worker with cache everything

  const cacheSeconds = 600
  const cacheShort = 180

  if (url.pathname.startsWith("/frequent/path/")) {
    // Edge Cache for 180 seconds
    return fetch(new Request(request, { cf: { cacheTtl: cacheShort, minify: { javascript: true, css: true, html: true } } }))
  } else {
    // Edge Cache for 600 seconds
    return fetch(new Request(request, { cf: { cacheTtl: cacheSeconds, minify: { javascript: true, css: true, html: true } } }))
  }

how would append a custom HTTP response header with the value of cacheSeconds or cacheShort value like CacheTime: 600 or CacheTime: 180

1 Like

Untested :slight_smile: but I’d imagine this to work (somewhat)

const cacheSeconds = 600
const cacheShort = 180

const cacheTime = url.pathname.startsWith("/frequent/path/") ? cacheShort : cacheSeconds;

let response = await fetch(new Request(request, { cf: { cacheTtl: cacheTime, minify: { javascript: true, css: true, html: true } } }));

response = new Response(response.body, response);
response.headers.set('CacheTime', cacheTime);

return response;
1 Like

works perfectly - thanks @sandro :+1:

age: 49
alt-svc: h3-24=":443"; ma=86400, h3-23=":443"; ma=86400
cache-control: private, no-cache, max-age=0
cachetime: 180
cf-cache-status: HIT
cf-ray: 54463fabae659edf-ORD
content-type: text/html; charset=utf-8
date: Fri, 13 Dec 2019 07:33:14 GMT
1 Like