Repository navigation
How can i call api in serversite like in nextjs? #7097
Replies: 2 comments 3 replies
|
In a client-side React page, you can achieve data fetching similar to Next.js by using a combination of the useState and useEffect hooks. For more advanced use cases, dedicated libraries like SWR or React Query are highly recommended. |
|
Hi @ShikharKhadka, I see you're using the App Router (src/app) and want to keep your baseUrl private while still using Refine's hooks like useList. Since Refine DataProviders run in the browser, any URL passed to them is visible in the Network tab. The standard way to solve this in Next.js is to create a Route Handler that acts as a secure proxy. Step 1: Create a Route Handler (The Proxy)Create a file at src/app/api/proxy/[...py]/route.ts. This server-side code will hide your real API URL and keys. import { NextRequest, NextResponse } from "next/next";
export async function ALL(
request: NextRequest,
{ params }: { params: { py: string[] } }
) {
const path = params.py.join("/");
const searchParams = request.nextUrl.searchParams.toString();
// Your private API URL from .env
const targetUrl = `${process.env.API_BASE_URL}/${path}${searchParams ? `?${searchParams}` : ""}`;
const response = await fetch(targetUrl, {
method: request.method,
headers: {
"Authorization": `Bearer ${process.env.API_TOKEN}`,
"Content-Type": "application/json",
},
body: request.method !== "GET" ? await request.text() : undefined,
});
const data = await response.json();
return NextResponse.json(data, { status: response.status });
}Step 2: Update your Refine DataProviderIn your Refine config, point the dataProvider to your internal proxy route instead of the external API. // src/app/layout.tsx (or where you config Refine)
import dataProvider from "@refinedev/simple-rest";
const API_URL = "/api/proxy"; // Browser only sees this!
// ... inside your Refine component
<Refine
dataProvider={dataProvider(API_URL)}
/* ... other props */
>Why this solves your issue:
Hope this helps you secure your Refine application!" |
Uh oh!
There was an error while loading. Please reload this page.
I have a page and inside page i want to call api same in next js which i was doing using fetch or axios and pass data to client side


. How can i achieve same as in next js but using hooks.
All reactions