© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•3y ago•
4 replies
bateswebtech

Trying to make a simple cache using a cloudflare worker with hono.

I was wondering if anyone could assist me with what I thought was going to be a simple usage of the cache api.

My issue is that I cannot seem to get the cache to store the body properly...

The cache key is getting stored and properly invalidated after 10 seconds but the body is always an empty object when the cache is matched.

app.get('/events', async (c) => {
  try {
    const cacheUrl = c.req.url
    const cache = caches.default

    // Check if our response is already in the cache
    let response = await cache.match(cacheUrl)
    console.log('body', JSON.stringify(response?.body))

    if (!response) {
      const { email } = c.req.query()
      const config = getConfig(c.env)
      const events = await findEvents(email, config)

      response = new Response(JSON.stringify({ data: events }))
      response.headers.append('Cache-Control', 's-maxage=10')

      c.executionCtx.waitUntil(cache.put(cacheUrl, response.clone()))

      return c.json({
        data: events
      })
    } else {
      console.log(`cache hit for ${cacheUrl}`)
    }

    return response
  } catch (err: Error | any) {
    return c.json(
      {
        error: {
          message: err.message
        }
      },
      500
    )
  }
})
app.get('/events', async (c) => {
  try {
    const cacheUrl = c.req.url
    const cache = caches.default

    // Check if our response is already in the cache
    let response = await cache.match(cacheUrl)
    console.log('body', JSON.stringify(response?.body))

    if (!response) {
      const { email } = c.req.query()
      const config = getConfig(c.env)
      const events = await findEvents(email, config)

      response = new Response(JSON.stringify({ data: events }))
      response.headers.append('Cache-Control', 's-maxage=10')

      c.executionCtx.waitUntil(cache.put(cacheUrl, response.clone()))

      return c.json({
        data: events
      })
    } else {
      console.log(`cache hit for ${cacheUrl}`)
    }

    return response
  } catch (err: Error | any) {
    return c.json(
      {
        error: {
          message: err.message
        }
      },
      500
    )
  }
})


The logs are showing up as this with an empty body when hitting the cache.

{
  "outcome": "ok",
  "scriptName": "some script name",
  "exceptions": [],
  "logs": [
    {
      "message": [
        "body",
        "{}"
      ],
      "level": "log",
      "timestamp": 1681117743889
    },
    {
      "message": [
        "cache hit for https://someurl/events?email=someemail@email.com"
      ],
      "level": "log",
      "timestamp": 1681117743889
    }
  ],
  "eventTimestamp": 1681117743883,
  "event": {
    "request": {
      "url": "some url",
      "method": "GET",
      "headers": {
        "content-type": "application/json"
      }
    },
    "response": {
      "status": 500
    }
  },
  "id": 27
}
{
  "outcome": "ok",
  "scriptName": "some script name",
  "exceptions": [],
  "logs": [
    {
      "message": [
        "body",
        "{}"
      ],
      "level": "log",
      "timestamp": 1681117743889
    },
    {
      "message": [
        "cache hit for https://someurl/events?email=someemail@email.com"
      ],
      "level": "log",
      "timestamp": 1681117743889
    }
  ],
  "eventTimestamp": 1681117743883,
  "event": {
    "request": {
      "url": "some url",
      "method": "GET",
      "headers": {
        "content-type": "application/json"
      }
    },
    "response": {
      "status": 500
    }
  },
  "id": 27
}
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Cloudflare Worker with Hono + D1 + D1-ORM
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
Hono and Cloudflare worker ratelimiting
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
15mo ago
Using cache in worker
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
Cloudflare worker Cache not work
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago