nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Malformed data written to cache storage

Open zsilbi opened this issue 1 year ago • 1 comments

Environment

Nitro 2.8.1 Nuxt 3.10.2

Ubuntu 20.04.5 LTS Wrangler 3.28

Ubuntu 22.04.1 LTS (WSL) Wrangler 3.28

storage: {
  cache: {
    driver: "cloudflare-kv-binding"
  }
}

Reproduction

Currently I have experienced this issue on two seperate computers but I don't have a consistent reproduction that produces the same error at the same endpoint on both machines.

Describe the bug

Responses of some endpoints that use cachedEventHandler are saved to the cache as an invalid JSON string.

If I use NITRO_PRESET=cloudflare it saves the first 4096 byte of the CacheEntry. If I use NITRO_PRESET=cloudflare_module it saves exactly 8192 bytes.

It results in an error on the next request when Nitro tries to write the expires property on the string.

Other endpoints that cache responses larger then 4/8kb work well in same project.

Setting swr to true/false does not make any difference.

I also tried Nitro 2.5.0 and 2.3.0 (CF preset only) but no difference.

I if remove everything that is async in the event handler, and just return the same content as the Promise would everything works.

Additional context

Issue in Nuxt: https://github.com/nuxt/nuxt/issues/12508#issuecomment-1607369533 This has some comments/likes that this can be also a problem in non-CF deployments.

I think regardless where the origin of this problem is, there should be a check that the CacheEntry retrieved from the strorage is indeed a valid cache entry, and console.error if not to prevent the app from crashing. https://github.com/unjs/nitro/pull/2161

Logs

✘ [ERROR] [nuxt] [request error] [unhandled] [500] Cannot create property 'expires' on string '{"expires":1708691163535,"value":{"code":200,"headers":{"etag":"W/\"Qgk2yYvVDI\"","last-modified":"Thu, 22 Feb 2024 12:26:04 GMT","cache-control":"max-age=86400"},"body": " ...

zsilbi avatar Feb 22 '24 12:02 zsilbi