Replies: 5 comments 12 replies
-
Thanks for creating this topic! We’re using RESTful APIs, so all our SWR hooks are similar to this: function useUser(id: string, opts?: SWRConfig) {
return useSWR<User>(`/api/user/${id}`, fetcher, opts)
} Sometimes we also add some related logic into it, such as attaching a I think we can probably write a blog post to share more ideas about this! |
Beta Was this translation helpful? Give feedback.
-
Repost here for more visibility, asked by @dariye:
We use Next.js, and the application structure looks like this:
All the data related hooks are under /swr, and they're just lightweight wrappers of the Each component can directly import and use those hooks without worrying about duplicate requests (SWR handles it!), like this: function UserCard({ id }) {
const { user } = useUser(id)
...
} We've added polling for some specific resource hooks, and auto redirection when logged out for the use-user hook.
We do have a separate directory for the design system related components (container/button/...) that are more abstract and atomic. So all data requirements are tied to those under the /component directory, which have more business logic inside. For example a import Avatar from 'design-system/avatar'
import useUser from 'swr/use-user'
export default function Profile({ id }) {
const { user, loading } = useUser(id)
if (loading) return <Skeleton/>
return <>
<Avatar src={user.pic}/>
<h1>{user.name}</h1>
</>
} Simple and clean, doesn't have to pass data between components.
This is guaranteed by SWR. Since it deep compares the fetched data and only re-render when data is accessed and changes (https://swr.vercel.app/docs/advanced/performance), I don't think there are any "needless re-renders". Happy to know more about this. The layout will be consistent since the same resource will be ready at the same time for all components consuming it. But for the other cases like dependent requests, we have three ways of avoiding layout shift: making sure the skeletons/spinners have the same size as the content, don't progressively reveal the content but wait for all data to be ready, use If you're interested in more details I can cover more with a blog post. |
Beta Was this translation helpful? Give feedback.
-
Amazing write-up. Perhaps it could use a refresher given App directory is nearing a beta exit? Just saw #2218 and I'm also curious if this pattern is still somehow viable in app directory Next.js apps. Mainly, I'm interested in seeing how I can do RSC + optimistic updates (with or without SWR) |
Beta Was this translation helpful? Give feedback.
-
@shuding how can I use it with next 14? I mean is there any concept of initial data as there is in tanstack query? |
Beta Was this translation helpful? Give feedback.
-
Why Vercel doesn't update SWR package? Where are many issues that causing improper hydration & React bugs. Almost 4 months since last release and shady dev activity. |
Beta Was this translation helpful? Give feedback.
-
I have a bit of a broader question about how Vercel uses this library. I'm curious to hear a bit about the architecture of a modern company with such a large product.
For example, how do you use the key prop? Do you provide the endpoint URL, like
useSWR('/api/user')
as shown often in the docs? Or do you use user IDs directly in keys to identify keys with users?I'm curious about type-safety as well. If you call
useSWR('/api/user')
, do you have to cast the types on, likeuseSWR<User>('/api/user')
? Or do you have some sort of code-gen fetcher/types generator that does this for you? And if you are casting, is this just using a shared types package with the backend?Example
For context, at beatgig.com, we use swr. Our backend is made with Python and FastAPI (using the OpenAPI spec.) Every time our Python server deploys, it code-gens a TypeScript package with typed functions to all of it's endpoints.
The front-end installs the package from a NPM package, imports functions, and passes them to SWR directly.
That's a bit of a simplified version of what we do, but it gets the message across.
Overall, I'd love some advanced details on how Vercel does this stuff. I think this kind of content would be useful in general to the community. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions