Server side SWR #4178
Unanswered
vitalijalbu
asked this question in
Q&A
Server side SWR
#4178
Replies: 2 comments
-
|
@vitalijalbu SWR is primarily a Client-Side Fetching library.
// Server Component
const data = await getData();
return (
<SWRConfig value={{ fallback: { ''/api/user'': data } }}>
<ClientProfile />
</SWRConfig>
) |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
SWR itself still runs in a Client Component. In the App Router, I usually do the initial fetch in a Server Component, then pass that result into SWR as A minimal example: // app/users/page.tsx
import { SWRConfig } from "swr";
import UsersClient from "./UsersClient";
async function getUsers() {
const res = await fetch("https://jsonplaceholder.typicode.com/users", {
// choose whatever caching behavior you need
cache: "no-store",
});
if (!res.ok) {
throw new Error("Failed to fetch users");
}
return res.json();
}
export default async function UsersPage() {
const users = await getUsers();
return (
<SWRConfig
value={{
fallback: {
"/api/users": users,
},
}}
>
<UsersClient />
</SWRConfig>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys, anyone can share a real example of SWR usage in SSR(app dir)
Thanks
Beta Was this translation helpful? Give feedback.
All reactions