Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
CloudFlare Worker Cache API Not Storing Fetch Results

I've just spent an entire day on this simple worker and i've gotten no where.

I've followed the Cache API examples on the CloudFlare site and also trawled through StackOverflow postings but im still nowhere.

This post (Trouble with Cloudflares Worker Cache API) looked like it would be what I need, but it didnt help.

Essentially all im trying to do is cache certain pages for a period of time using the cache API. I will eventually need to do specific caches per country code (which is why im using workers and not page rules) but for now all im trying to do is get the basic cache API working as a test.

Here is the code, it is basically the same code from the example on their site

handler.ts

export async function handleRequest(event: FetchEvent): Promise<Response> {
  const { request } = event
  const cacheUrl = new URL(request.url)
  const cacheKey = new Request(cacheUrl.toString(), request)
  const cache = caches.default

  let response = await cache.match(cacheKey)
  if (!response) {
    console.log('NO CACHE MATCH')
    // 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)
    response.headers.append('Cache-Control', 's-maxage=10')
    event.waitUntil(cache.put(cacheKey, response.clone()))
  } 
  return response
}

index.ts

import { handleRequest } from './handler'

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event))
})

I am aware that you cannot use the Cache API in the workers.dev domain so I have deployed the worker to a custom domain so it should have access to the API.

I have tried

  • Logging out the response from cache.put - nothing
  • Changing the cache key from a Response obj to a URL (string) - nothing
  • Straight await'ing the cache.put - nothing
  • Using a custom cache namespace (so await caches.open('pages')) - nothing

Basically, I just get cache misses and the request goes through to my webserver. I can see the console log, everything is running fine, it just never caches or never finds the cache.

Its incredibly hard to debug and there is next to no information returned from these methods. cache.put returns nothing, cache.match returns nothing. They might as well be mocked.

Im using wrangler to build / publish, and everything is working fine there.

Can anyone assist here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think your code looks pretty straightforward / makes sense. So, to me seems like the next possibility is some sort of data issue (e.g. with the response format). Some headers, like Cache-Control and specifically Set-Cookie will affect if it can be cached - https://developers.cloudflare.com/workers/runtime-apis/cache#headers

Can you verify your response doesn't contain something that would make it uncacheable? If you are able to share the full response, or just the headers, that would also help.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda