unstorage icon indicating copy to clipboard operation
unstorage copied to clipboard

feat: cloudflare cache runtime api

Open RihanArfan opened this issue 9 months ago • 2 comments

resolves #602

TODO: Needs testing

RihanArfan avatar Mar 19 '25 17:03 RihanArfan

Really smart approach. Honestly i'm not 💯 sure it would be best case of using unstorage (Nitro v3 rather can directly leverage caches API on top of web response handling)

But let's keep both on the track and tested.

pi0 avatar Mar 19 '25 17:03 pi0

If Nitro v3 can do cached event handlers and functions directly via the cache API then I think may not be necessary then 😄

Happy for you to close if Nitro is handling it better.

RihanArfan avatar Mar 19 '25 17:03 RihanArfan

@RihanArfan any update on this? (have you tested this solution on prod?)

pi0 avatar Apr 02 '25 09:04 pi0

This isn't Cloudflare-specific. It's using the CacheStorage api, which is also supported by other runtimes too

ascorbic avatar Apr 09 '25 08:04 ascorbic

@ascorbic Cloudflare's implementation of the Cache API is slightly different from the web standard - https://developers.cloudflare.com/workers/runtime-apis/cache/#accessing-cache

Most notably, there's no way to list all keys - https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/keys

@pi0 Unfortunately haven't been able to yet sorry

RihanArfan avatar Apr 10 '25 00:04 RihanArfan

@RihanArfan is there a way to implement this in a cross-platform way though, that supports runtimes that implement the web standard too

ascorbic avatar Apr 10 '25 09:04 ascorbic

@RihanArfan is there a way to implement this in a cross-platform way though, that supports runtimes that implement the web standard too

Netlify would also be interested in this.

eduardoboucas avatar Apr 10 '25 16:04 eduardoboucas

~~@RihanArfan do you remember what is last state of this? could we move it forward?~~ (updated PR with refactors + tests but seems wrangler proxy is not implementing caches API at least out of the box)

@eduardoboucas while making this one generic could take time (or not be possible), if you have time to draft a driver for netlify, i can merge and release.

pi0 avatar Aug 22 '25 19:08 pi0

Codecov Report

:x: Patch coverage is 29.16667% with 51 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 61.83%. Comparing base (70310f9) to head (673b97e). :warning: Report is 41 commits behind head on main.

Files with missing lines Patch % Lines
src/drivers/cloudflare-cache-binding.ts 27.14% 51 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #603      +/-   ##
==========================================
+ Coverage   59.99%   61.83%   +1.84%     
==========================================
  Files          42       44       +2     
  Lines        3657     3962     +305     
  Branches      590      661      +71     
==========================================
+ Hits         2194     2450     +256     
- Misses       1460     1509      +49     
  Partials        3        3              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Aug 22 '25 22:08 codecov[bot]

Disabled tests with a manual test for now (works in prod sandbox with using unstorage:// as cache key)

@dario-piotrowicz @petebacondarwin could you please kindly review this PR and confirm if i am not mistaking and wrangler dev proxy does not implement caches API?

pi0 avatar Aug 22 '25 22:08 pi0

Disabled tests with a manual test for now (works in prod sandbox with using unstorage:// as cache key)

@dario-piotrowicz @petebacondarwin could you please kindly review this PR and confirm if i am not mistaking and wrangler dev proxy does not implement caches API?

I am not very familiar with this codebase unfortunately, but by quickly skimming through the PR it all seems correct to me 🙂

Regarding the cache not being implemented you're right, we also have an issue for this: https://github.com/cloudflare/workers-sdk/issues/4879 (which refers to getBidingsProxy the old name for getPlatformProxy, I'll update the issue with the correct name) unfortunately this piece of work has never been prioritized (also it being not trivial doesn't help). The fact that we're trying to get more and more code to run natively in our runtime also makes it a bit improbable in my opinion that we can prioritize getPlatformProxy-only features (unless there's a significant demand/motivation for it) 🥲

(PS: Running code inside miniflare should support the caching API... although I just manually checked that and that doesn't seem to work... I'll need to investigate a bit 🤔)

(PS: sorry for the late reply, I only now saw this 🙇)

dario-piotrowicz avatar Aug 27 '25 23:08 dario-piotrowicz