Understanding static rendering
Answered
American posted this in #help-forum
AmericanOP
I'm quite confused. What I know from the docs and [Learn Next.js Chapter 8](https://nextjs.org/learn/dashboard-app/static-and-dynamic-rendering) is that routes that are supposed to be statically rendered, they get rendered at build time and this result is what always gets served. Now I thought something like this would always return <h1>Hi</h1> since during build time, useSelector would return the initial state
but that didn't happen. Once the state got mutated and the if condition isn't satisfied anymore the content of the page changed.
Another case though, in the learn nextjs project (right after chapter 9), there is this component that gets rendered in the dashboard page (attached as a screenshot). Any changes to the data after the project is deployed are not shown, even though if fetchCardData was executed at request time this wouldn't have been the case.
I'm very lost at this point I'm not even sure what static rendering does/should do anymore 💀 hopefully someone can point me in the right direction :)
export default function Page() {
const router = useRouter();
const user = useSelector((state: RootState) => state.user);
if (Object.keys(user).length === 0) // If "user" is still the initial state
return <h1>Hi</h1>;
return <h1>{user.name}</h1>;but that didn't happen. Once the state got mutated and the if condition isn't satisfied anymore the content of the page changed.
Another case though, in the learn nextjs project (right after chapter 9), there is this component that gets rendered in the dashboard page (attached as a screenshot). Any changes to the data after the project is deployed are not shown, even though if fetchCardData was executed at request time this wouldn't have been the case.
I'm very lost at this point I'm not even sure what static rendering does/should do anymore 💀 hopefully someone can point me in the right direction :)
103 Replies
American Chinchilla
Are you running that example in dev or in a prod preview?
AmericanOP
prod
the example code that uses redux and the learn nextjs project, both are deployed on vercel
@American what is useSelector?
AmericanOP
its from redux toolkit
so this is a client page?
AmericanOP
correct
i mean, it def is a client page you are using useRouter
ok so thats the thing
so now how ssg works, imagine you have a fetch call in there
and you dont use anything dynamic, like cookies or headers
now this means, that fetch call is static.. it isnt unique per user
makes sense till here?
AmericanOP
yeah
@Arinji now this means, that fetch call is static.. it isnt unique per user
AmericanOP
does that apply to the fetchCardData/sql function by any chance? (the second screenshot)
also it could be unique per user if the request uses any user info for example
@American also it could be unique per user if the request uses any user info for example
yes, so cookies and headers
since those are the only 2 ways you identify someone
@American yeah
ok so, static and dynamic is ONLY for server stuff
Answer
when you go into client, thats when react takes over
static will pregenerate your site, so it will give a big generated html to the browser
but then react takes over and then it does all the mutating it does
@Arinji since those are the only 2 ways you identify someone
AmericanOP
what if I'm keeping track of the user thru state management, like in the example code (not the screenshots)
@American what if I'm keeping track of the user thru state management, like in the example code (not the screenshots)
well, ideally you dont take all of this into the client
because in that case dont do nextjs
nextjs is very very server centric
@Arinji ok so, static and dynamic is ONLY for server stuff
AmericanOP
from what I understand, client components don't really imply client side rendering, they get partially (or mostly) rendered in the server as well
one sec i had a good thing for this
read that @American
AmericanOP
also if i export const dynamic = force-dynamic in the page that uses the CardWrapper, the data gets updated per request
AmericanOP
a wait it's not a client component
hmm
do you have a "use client" at the top?
AmericanOP
yeah I just noticed its a server component
@Arinji ok so, static and dynamic is ONLY for server stuff
AmericanOP
I was stuck on that hehe
@American yeah I just noticed its a server component
how are you using useRouter then?
@Arinji Click to see attachment
AmericanOP
so no static or dynamic rendering for client components
show your entire page.tsx pls
AmericanOP
the screenshots are different from the code example
@American so no static or dynamic rendering for client components
yes, it will only pregen the code, but the rest is handled by react
AmericanOP
the 2 screenshots are from the Learn Nextjs project
hence its recomendded to make the smallest part of your code, client components
AmericanOP
the code is just from a personal project of mine
so server pages -> server components -> client interaction
instead of client pages -> explosions
@Arinji so server pages -> server components -> client interaction
this is a simplification btw, everything is server components.. there isnt anything called server pages but dont worry abt it
@Arinji yes, it will only pregen the code, but the rest is handled by react
AmericanOP
pregens the js and html, server sends out html (and js? not sure) and the hydration happens at the client side
where everything gets connected basically
is that right?
but yeah now this makes sense for why the code that has userouter doesn't return just a single result
@American pregens the js and html, server sends out html (and js? not sure) and the hydration happens at the client side
yes, pregens the html css and js files, and then react goes in and maps the html nodes with its react nodes
and you get your entire virtual and real dom stuff
AmericanOP
rightt
hm
okay this makes sense
awesome
AmericanOP
now back to the learn nextjs project (that has the fetchCardData) 💀
now the page is statically rendered, due to no dynamic functions, fair
however I've seen something in a reddit post, that if u wrap a component in a Suspense, then the page will load normally and this component basically wont be blocking the whole page
and will get rendered once the data is ready
sure, makes sense
this doesn't seem to affect static rendering at all tho, is this the expected behaviour?
so the only way to make it render per request is for me to either use a dynamic function by any chance, or, since in this case I don't need one, I export force-dynamic
@American this doesn't seem to affect static rendering at all tho, is this the expected behaviour?
what would you consider expected?
and what is currently happening
AmericanOP
well, one second
this is the page, the CardWrapper renders those cards below Latest Invoices
u see the test123 card? right now in the database it's something else
if I redeploy I can see the changes
mhm
AmericanOP
so I'm assuming this is due to our lord and saviour static rendering
yes
AmericanOP
not assuming, I'm sure 💀
AmericanOP
yeah
sooo basically I have to keep into consideration if I'm gonna fetch data without really using "fetch"
I can't set the cache options, so export force dynamic
and adding a Suspense won't affect how the component is rendered, it will always be statically rendered anyway
is that right?
kinda annoying but oh well, at least I know now
AmericanOP
oh yeah saw that today as well
sick
ikr
anything else?
AmericanOP
nope, everything clear now
thanks a lot dude, been stuck on this all day and yesterday as well 💀
really appreciate the help <3
@American thanks a lot dude, been stuck on this all day and yesterday as well 💀
no worries, ask around iff you get confused :D
AmericanOP
will do, thanks again :D