How can I build pages without calling the API?
Answered
Swyftey posted this in #help-forum
SwyfteyOP
Hi.
I am using Next.Js version 13+ ('/app' directory) and would like to know if I can build my project without it using the API (server-side)? I'd only like the API to be used when the project is running or on 'request time' I think.
Thanks for any assistance.
I am using Next.Js version 13+ ('/app' directory) and would like to know if I can build my project without it using the API (server-side)? I'd only like the API to be used when the project is running or on 'request time' I think.
Thanks for any assistance.
10 Replies
@Swyftey Hi.
I am using Next.Js version 13+ ('/app' directory) and would like to know if I can build my project without it using the API (server-side)? I'd only like the API to be used when the project is running or on 'request time' I think.
Thanks for any assistance.
you can make your site fully dynamic and then the page only request your DB (backend/api) when the user requests your page
@B33fb0n3 you can make your site fully dynamic and then the page only request your DB (backend/api) when the user requests your page
SwyfteyOP
I see. Is there a chance you can show me a simple example of how to make it "fully dymamic"?
This is basically what I have right now, and it requires the API when I build:
This is basically what I have right now, and it requires the API when I build:
const fetchData = () => {}); // Fetch Data
const ProfilePage = async () => {}); // Call fetchData in here
export default ProfilePage;This would be in the "page.tsx" file.
app -> profile -> page.tsx
app -> profile -> page.tsx
@Swyftey I see. Is there a chance you can show me a simple example of how to make it "fully dymamic"?
This is basically what I have right now, and it requires the API when I build:
js
const fetchData = () => {}); // Fetch Data
const ProfilePage = async () => {}); // Call fetchData in here
export default ProfilePage;
export const dyamic = "force-dynamic";
export default async function ProfilePage({params}:{params:{id:string}}){
const profile = await db.query(`SELECT * FROM users where id=${params.id}`);
if(!profile) return notFound()
return <>Profile Page</>
}@@ts-ignore tsx
export const dyamic = "force-dynamic";
export default async function ProfilePage({params}:{params:{id:string}}){
const profile = await db.query(`SELECT * FROM users where id=${params.id}`);
if(!profile) return notFound()
return <>Profile Page</>
}
SwyfteyOP
So, for the "force-dynamic", all I need to do is create a variable?
"export const dynamic = "force-dynamic";"
"export const dynamic = "force-dynamic";"
SwyfteyOP
Alright.
Answer
SwyfteyOP
Thanks