Statically serve a dynamic route
Unanswered
tnfAngel posted this in #help-forum
tnfAngelOP
I dont need any on-demand server-side rendering on my dynamic Route, the only thing i use is the useParams() hook which only works in the client side. How can i disable the on-demand SSR?
75 Replies
@joulev Try export const dynamic = "force-static"
tnfAngelOP
amazing, i'll try it 👍
@tnfAngel hmm it didn't really work (still getting the ƒ icon on build)
Remove the "use client" part there
@joulev Remove the "use client" part there
tnfAngelOP
Amazing! that's exactly what i wanted. Thank you so much ❤️
@tnfAngel Amazing! that's exactly what i wanted. Thank you so much ❤️
tnfAngelOP
Hmm, it's not 100% static although next.js shows it like it is, I added this console.log and every time I visit a different route (same routes seems to be cached or fully static) it runs it on the server. I imagine that it cannot be made 100% static or maybe it is a bug 🤔 anyways, thank you 👍
Fire ant
Why not only use "use client"?
Where is the
use client?@tnfAngel Hmm, it's not 100% static although next.js shows it like it is, I added this console.log and every time I visit a different route (same routes seems to be cached or fully static) it runs it on the server. I imagine that it cannot be made 100% static or maybe it is a bug 🤔 anyways, thank you 👍
that's weird... but is it fast? say if you add a
delay(1s) into the function, does each request wait one second or does it load instantly? force-static should make it completely static, this looks like a framework bug1s delay to request with different path
same path is cached so no delay
@joulev Remove the "use client" part there
tnfAngelOP
"use client" was removed here, when I removed it, nextjs marked it as static
(for @Fire ant and @ᴉuɐpɹɐɐ)
(for @Fire ant and @ᴉuɐpɹɐɐ)
Just found this post. I have posted about a similar problem. I would appreciate it if you could check it. Thank you.
#Why my `/post` page is dynamic rendered?
#Why my `/post` page is dynamic rendered?
well... then i honestly dont know what to do
i feel this particular behaviour is not possible in the app router
if even
force-static can't make it static, i don't know what can@tnfAngel hmm it didn't really work (still getting the ƒ icon on build)
try adding
generateStaticParams and return []@ᴉuɐpɹɐɐ try adding `generateStaticParams` and return `[]`
that is still dynamically rendered the first time, it is a good workaround yes but it doesn't fully replicate the behaviour
i would put the client stuff in the layout (
/layout.tsx) and return null in /[dynamicroute]/page.tsxif you plan to statically render your website with client-side dynamic features, that is what i would do
i also am not sure, iirc force-static would override the dynamic route's default staticity, but i guess that would be ambiguous
@ᴉuɐpɹɐɐ try adding `generateStaticParams` and return `[]`
tnfAngelOP
same as
force-static, it marks it as static but still rendered on the server when i change the pathHonestly it seems like a next.js bug
i said to put the rendering on layout.tsx
tnfAngelOP
so generateStaticParams & return null?
return
[]and in your component, just return
nullit will always make request to the server at every navigation, but atleast it aint rendering anything
im not sure theres a way to do shallow routing with dynamic routes
tnfAngelOP
so that it really wont call the server at all
not sure if this will update
useParams() though@ᴉuɐpɹɐɐ it will always make request to the server at every navigation, but atleast it aint rendering anything
tnfAngelOP
well i actually need to render something, the #Unknown Channel</> is just a test, so i render it on the layout? won't the same thing happen?
@tnfAngel well i actually need to render something, the <></> is just a test, so i render it on the layout? won't the same thing happen?
you dont, all your rerenders are in the client though
that "something" is in <DocumentScreen /> which is located in the parent layout.tsx
tnfAngelOP
Yes, I know that, but when the layout is rendered in a new route (without actually using a pushState, but with manual access to the route, imagine that someone shares the url with a friend), will it also be rendered on the server?
fair point
tnfAngelOP
should an issue be created on Github or is on-demand SSR really 100% necessary for it to work?
i mean it could always be generated once on build time and then distributed to all routes instead of re-generating it on demand, the code should be the same for every route
do you still need SSR on some part of your app?
tnfAngelOP
not really
hmm try static export maybe?
how it's missing generateStaticParams
maybe it does not like the []
okay
now i'll just find a way to serve that html on every route
tnfAngelOP
tysm @ᴉuɐpɹɐɐ
wait what?
it worked?
tnfAngelOP
well i need the way to serve the html on every route
but that's not a next.js problem
the static export is really interesting
your
documentId is everly expanding right? i.e the params are not statically predefinedtnfAngelOP
I didn't try that and I can't really do it with serve because it doesn't allow wildcards afaik
oh yes
cool
i'll try it
whoa
can you share the link to that?
@tnfAngel okay
i feel this will always make useParams() return { documentId: "index" }
regardless of which URL you access from
@joulev i feel this will always make useParams() return { documentId: "index" }
tnfAngelOP
well js can solve that if this gives problems
@joulev i feel this will always make useParams() return { documentId: "index" }
tnfAngelOP
yep, i'll use js then 👍
@tnfAngel well js can solve that if this gives problems
in that case you can just make a static route and use [rewrites](https://nextjs.org/docs/app/api-reference/next-config-js/rewrites) to rewrite everything to it, no need to switch to static export which will be very limited
tnfAngelOP
I fixed it in my particular case with static export & serve (i dont really need the server features of next.js), but if anyone needs SSR, image optimization, etc, route rewrites should work well