koba04 / swr-devtools

A DevTool for SWR
https://swr-devtools.vercel.app
MIT License
217 stars 3 forks source link

Injecting the middleware automatically #52

Closed shuding closed 2 years ago

shuding commented 2 years ago

To inject the DevTools middleware to the application, we currently have to wrap it with <SWRDevTools> when rendering. This is a great solution but not perfect due to the restrictions:

It would be great to inject the middleware automatically to all SWR apps, from the extension side. So I opened https://github.com/vercel/swr/issues/2003 to discuss this idea. In extension’s content, we create the middleware and add it to __SWR_DEVTOOLS_USE__:

global.__SWR_DEVTOOLS_USE__ = global.__SWR_DEVTOOLS_USE__ || []
global.__SWR_DEVTOOLS_USE__.push(devtoolsMiddleware)

And then SWR will always use the middleware if defined.

The only thing is to make sure the injection runs before application code to avoid mismatching of number of hooks. I think "run_at": "document_start" already does that but not 100% sure.

koba04 commented 2 years ago

I love the idea!!😍