#How do I do the equivalent of getStaticProps in app router?
11 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
You've to fetch data in server component, pass to client component
// app/posts/[id]/page.tsx
// Function to fetch data
async function getPostData(id: string) {
const res = await fetch(`https://api.example.com/posts/${id}`, {
cache: 'force-cache', // Ensures data is fetched at build time
});
if (!res.ok) {
throw new Error('Failed to fetch data');
}
return res.json();
}
export default async function PostPage({ params }: { params: { id: string } }) {
const data = await getPostData(params.id);
return (
// Optionally pass to client component
<div>
<h1>{data.title}</h1>
<p>{data.content}</p>
</div>
);
}
// Static parameters generation for static paths
export async function generateStaticParams() {
// Fetch and return the static paths
return [
{ id: '1' },
{ id: '2' },
{ id: '3' },
];
}
@mild comet resolved?
whether its static or not generally depends on whether you import the headers or cookies functions on the page and what the cache on your fetch call is. Next will try to make things static as much as possible but switches to dynamic if the page requires request-specific data (from headers or cookies) or needs some data from a fetch that isnt cached
If it works, mark solution
To mark the message as solution:
- Hover over the message you want to mark as the solution.
- Right click the message Click the three dots that appear on the right side.
- You'll see a menu which should have the option of
Apps. Hover Over it. - Click on the
Mark as Answeroption.
Note: If you don't see theMark as Answeroption orAppsoption, restart/update your discord app!