Giter Site home page Giter Site logo

Support SWR v1 about swr-devtools HOT 7 CLOSED

koba04 avatar koba04 commented on May 25, 2024
Support SWR v1

from swr-devtools.

Comments (7)

koba04 avatar koba04 commented on May 25, 2024 1

It seems to be hard to support the custom cache feature in v1 unless developers write their own injecting spy functionality into all mutations.

from swr-devtools.

shuding avatar shuding commented on May 25, 2024 1

Hey, I just found this project and it's looking great! Would love to contribute as much as I can.

Do you think if it's possible to build it as a middleware?

<SWRConfig value={{ middlewares: [swrdevtools] }}>

Since SWRConfig supports extending, you can make the above a component (so you can inject the UI there too):

<SWRDevTools>
  <MainApp />
</SWRDevTools>

The good part is, you can access to the active cache inside a middleware (config.cache), and you can provide a custom cache implementation as well by overriding config.cache.

Also, since the import { cache } from 'swr' is gone in 1.0, a way to get the global cache is SWRConfig.default.cache.

from swr-devtools.

shuding avatar shuding commented on May 25, 2024 1

Update: I'm working on a new hook to expose the mutate method in vercel/swr#1361, it should cover this use case (as well as many other pain points).

from swr-devtools.

koba04 avatar koba04 commented on May 25, 2024 1

@shuding Thank you! That looks amazing and I think this covers my use case 👍
I started considering that swr-devtools only supports v1 in the near future.

from swr-devtools.

koba04 avatar koba04 commented on May 25, 2024

The Cache interface of swr v1 only has get, set, and delete methods, which means I cannot subscribe changes in the Cache and cannot list all cache data.
https://github.com/vercel/swr/blob/master/src/types.ts#L170-L174

For that reasons, I would have to wrap a cache to add capabilities like subscribe() and keys() in __SWR_DEVTOOLS__.launch(cache).

from swr-devtools.

koba04 avatar koba04 commented on May 25, 2024

I've started supporting v1 at 90a9775.
I've noticed that v1 doesn't expose keys() as an interface, so I cannot retrieve all cache data before opening the devtool panel.

from swr-devtools.

koba04 avatar koba04 commented on May 25, 2024

@shuding Thank you for being interested in this project!
Yeah, That's a great idea for v1 support. I didn't come up with using config.cache, and it would be the best place to handle a cache. That also gives us the capability of observing fetch requests.

One thing that is not clear is that how to support mutation from a devtool panel. (#8) To mutate a cache, SWR Devtools has to keep the reference of the mutate method to support this, but middlewares would not be able to get the reference. So I have to find a way to pass the mutate method to the middleware.

from swr-devtools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.