How can i use getStaticProps in next js 13 app directory?
Answered
Seppala Siberian Sleddog posted this in #help-forum
Seppala Siberian SleddogOP
I want to use getStaticProps in the new App router but it seems it does not work anymore.
51 Replies
Because it is not necessary. With server component, you can fetch data inside it
and the page will be static generated at build time by default
@Ray Because it is not necessary. With server component, you can fetch data inside it
Seppala Siberian SleddogOP
So the component runs on the server and I can write server code directly?
Original message was deleted
yes
@aardani yes
Seppala Siberian SleddogOP
And how can i getServerSideProps
just write the code directly in the component
Answer
in the page.jsx or layout.jsx
Seppala Siberian SleddogOP
I mean for dynamic not static
yes exactly
Next.js will automatically detect if its dynamic or static.
you can force it to be dynamic with a specific settings
you can force it to be dynamic with a specific settings
Seppala Siberian SleddogOP
and how will it distinguish if this is a dynamic or static?
okay
by the presence of dynamic functions, or by exporting a setting for that rotue
cookies() or headers(), or using searchParams will make it dynamic, or
export const dynamic = 'force-dynamic' will make it dynamicelse, its static by default
Seppala Siberian SleddogOP
Got it thank you!
theres more nuance to it so
if you need help with specific use case, feel free to provide more information or create a separate thread
best if you check out the documentation on how to migrate
Seppala Siberian SleddogOP
I was using the pages directory for 1 year, and i only started working on the app 2 months ago so i was a bit confused.
you can keep using the pages directory, it will stay relevant for years to come
its just that Next.js believes in latest react tech thats why they're heavily promoting it
But im sure because of that, tehres many guide to migrate from pages to dir on the internet
Seppala Siberian SleddogOP
Can i use them both, in the same project?
Seppala Siberian SleddogOP
that's right
you can incrementally move from pages dir to app dir one route at a time, but its not advisable to keep them both in production
Seppala Siberian SleddogOP
So what is the component is a client component can i still fetch data from the server?
what if the component is a client component?
you have to split that client component into 2 parts
the server fetching part and the client-interactivity part
and you have to move down the client boundary as deep as possible in the component tree
only making the interaction the client component
you can still however, make a top level context client component and interleave server component and client component
Seppala Siberian SleddogOP
But I'm using redux so I need the provider at the top level and it uses a client component
exactly why i said the last message
so
<Client>
<Server>
<Server>
<Client/>
is still ok
<Client>
<Server>
<Server>
<Client/>
is still ok
Seppala Siberian SleddogOP
I can create a server component inside a client component?
you can
you can render server component at the server if you pass them as props
as props
children -> props
not import inside the client ok? that'd be different.
Please read this article on how the magic is done, it has been explained multiple times here in this forum sorry xD
unless you have further question regarding the section
Seppala Siberian SleddogOP
no thank you !, i will read it through