Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use the middleware instead of injecting a cache #39

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

koba04
Copy link
Owner

@koba04 koba04 commented Dec 4, 2021

This approach has some problems, so I won't take this approach. But I created a PR to put my works.
SWR's middleware allows us to a hook for useSWR hooks, which seems to be a good place for SWRDevtools.
But this has the following problems.

  • the return values of useSWR are monitored by swr to optimize re-rendering, which is implemented by getters. So if we get the data for SWRDevtools, it is marked as "used" and causes extra re-rendering. refs feat: record SWR data with useDebugValue #37 (comment).
  • It's difficult to get key strings in middlewares. We can use unstable_serialize, but how should I handle useSWRInfinite?

@vercel
Copy link

vercel bot commented Dec 4, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/koba04/swr-devtools/GXQ6dicETieXm4eki9xQ713bJS7h
✅ Preview: https://swr-devtools-git-stop-injecting-cache-koba04.vercel.app

@shuding
Copy link
Collaborator

shuding commented Dec 7, 2021

Thanks for working on this! Here are my thoughts:

So if we get the data for SWRDevtools, it is marked as "used" and causes extra re-rendering

That is indeed a problem. We can probably monitor data updates via a listener or something else, but we can't use useDebugValue() without a re-render. Maybe this can be an option that users can switch on/off, and normally in development it's OK to have extra re-renders, React does the same too (facebook/react#15074).

but how should I handle useSWRInfinite?

useSWRInfinite is indeed a special case, do you think if the unstable_serialize exported from swr/infinite helps?

@koba04
Copy link
Owner Author

koba04 commented Dec 8, 2021

Thank you for your comments!

Maybe this can be an option that users can switch on/off, and normally in development it's OK to have extra re-renders, React does the same too (facebook/react#15074).

Yeah, that's right. I think it seems to be ok. But I'm a little worried about it because I often see that the behavior of React makes developers confused. I think isValidating causes many extra rendering, so it might be acceptable if SWRDevTools only tracks data and error.

useSWRInfinite is indeed a special case, do you think if the unstable_serialize exported from swr/infinite helps?

That might be useful, but is there any way to know whether useSWRNext is useSWRInfinite or not? Do I have to test this by confirming if the data has the size and setSize properties?

@shuding
Copy link
Collaborator

shuding commented Dec 11, 2021

That might be useful, but is there any way to know whether useSWRNext is useSWRInfinite or not?

I think the most "correct" way of doing so is by getting the middleware option and see if it has an infinite middleware (which is exported from swr/infinite).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants