Next.js Discord

Discord Forum

Nextjs making static pages into dynamic

Answered
mohtashim posted this in #help-forum
Open in Discord
Hi I am facing this issue that nextjs is making my static pages into dynamic why is it that kindly help me
Answered by ᴉuɐpɹɐɐ
this is causing it to convert from static to dynamic
View full answer

76 Replies

Asiatic Lion
Thank you.
I can share code what part you would like to see ?
Asiatic Lion
It is about the page which is converting to dynamic instead of static
I commented this code which in main layout
and it's not converting them into dynamic
so how should I achieve both things
should I add this export const dynamic = 'force-static' ?
Asiatic Lion
that would make probably everything static.. i dont know much about tanstack hydration, maybe you should take a look into their docs https://tanstack.com/query/v4/docs/framework/react/guides/ssr
I added the dynamic='force-static' but prefetch is not working
Well I did the above coding using the docs
let me check if I find anything
Asiatic Lion
ah okay.. btw. where is your queryclientprovider?
It's in the top layout.js
In the docs Hydration is done for a specific component
But in my case i am doing in a layout
so I should somehow do it for the specific component i think
is this your problem?
wdym?
you said you have issue that nextjs is making static pages into dynamic.

so you want them static right?
yes and also the prefetch part
nice
so your only problem is these right?
the one with []
like [id] and [[...id]]
that's alirght but when I perform prefetch all the static pages also becomes dynamic
interesting
whats inside the usePrefetch function?
api call
show code
or snippet
or import statements
So If I perform the api call in server component instead of in layout
will this issue be solved ?
im not sure what is inside the "prefetchQuery" function that is making it dynamic
what is inside the getCompany function?
also protip: you dont have to pass queryClient everywhere ;)
@ᴉuɐpɹɐɐ also protip: you dont have to pass `queryClient` everywhere ;)
I am doing it because we have to create on instance of queryclient which then we hydrate it once
@mohtashim I am doing it because we have to create on instance of queryclient which then we hydrate it once
you can do it without passing it everywhere with the cache() function
@mohtashim Click to see attachment
what is inside getToken?
@mohtashim Click to see attachment
this is causing it to convert from static to dynamic
Answer
particularly the cookies() part.
it doesnt make sense for a webpage that is designed to render at build time, to read cookies. Whose cookies is it reading from? The use of that will convert the site from statically rendered to dynamically rendered.
you can read more here:
https://nextjs.org/docs/app/building-your-application/rendering/server-components#dynamic-rendering
wdym by that "Whose cookies is it reading from ?"
if you build the route at build time, and you call cookie(), whose cookie are you reading it from? there is no user accessing the page
@ᴉuɐpɹɐɐ if you build the route at build time, and you call cookie(), whose cookie are you reading it from? there is no user accessing the page
ah yes i get it so how do i make it like so it wont call cookie() at build time?
remove const nextCookies = cookies()
then how would I get accestoken and call the api ?
get accesstoken from where?
from client
how?
there is no "client" at build time
i am getting your point but how not to call it build time but when user request i do need it
no?
you cant
building the route statically means that it will build the routes regardless of the users, once and store the result in a build cache, like a cdn with all the html, js, css
when user access a statically rendered route, it will directly return htmll, js, and css and run any client-side javascript (like react) to do your app logic
if you want to "get accesstoken" "when user request" in the server, then make a dynamically rendered route.
So If I make server component then that component used in the path will also convert it into dynamic path ?
or it will be just a server component
if you make a server component that calls any dynamic features, and that is used in the path, then that path will be opted into dynamic server rendering. yes. that means you will have access to the request object
however,
you want to enforce static rendering at build time, then you can just make the api call in the client.
hit server action from the client, read cookie from server, then make api call
I see. I'll try that.
Thanks alot
But it won't solve our problem
We are just adding another layer of request
lets hear your problem
First client then server
in our current we do server thing first then client
what is the problem?
The problem is using the server action won't we have to wait more
like we are calling the api after the client is rendered
but we want the data before the client is rendered
@mohtashim but we want the data before the client is rendered
ya but why are you avoiding dynamic rendering?
what is your goal
There is a lag when navigating between pages
then that is another problem
I have answered the reason why your pages goes from static to dynamic
if you have issues with dynamic rendering please open up a new thread