I am trying to get use of searchParams in server component. but it is not working.
Answered
Donskoy posted this in #help-forum
DonskoyOP
export default async function BlogArticles({ params, searchParams }) {
console.log("================================", searchParams);
})95 Replies
@Donskoy params, searchPrarams are only available in pages not in any server components
you need to get searchParams in page and pass it to the children components
DonskoyOP
Anything can be done ?
or you can create a client component
DonskoyOP
Can you share an example if possible
and use hooks to get searchParams
DonskoyOP
I am working on SSG.
So use hooks I won't be using
@Donskoy Can you share an example if possible
export default async function Page({ searchParams }) {
...
<BlogArticles searchParams={searchParams} />
...
}
...
<BlogArticles searchParams={searchParams} />
...
}
yeah sort of
DonskoyOP
Checking
This is the URL - http://localhost:3000/blog-articles?page=2
Page.js
Page.js
import BlogArticles from "./BlogArticles";
export default async function Page({ params, searchParams }) {
return <BlogArticles searchParams={searchParams} />;
}@Donskoy so are you logging searchParams?
DonskoyOP
Yup.
I did it.
also
searchParams is a dynamic apihow are you gonna use it for SSG?
DonskoyOP
That's what I have doubt about.
I want to run pagination in SSG. How I can archive that
nope, pagination won't be implemented in SSG
it should be SSR
imagine users post their blogs every 5m
if they are statically generated, how are you gonna show new blogs then?
DonskoyOP
But actually this is what the requirement is about. They have limited blogs that they want to show up.
Can you share some link or something about SSR. How basically it works?
Can you share some link or something about SSR. How basically it works?
check out this page, that's my previous company website that I built
so the first page is a static one - just grabbed the most recent jobs for fast initial loading time
but when they try to search the blogs, it's doing SSR (pagination is also there)
@Donskoy But actually this is what the requirement is about. They have limited blogs that they want to show up.
Can you share some link or something about SSR. How basically it works?
btw can you tell me more about
"limited blogs" that they want to show up?is it really something about static generation at build time?
assuming it's about draft/published blogs or something
DonskoyOP
I mean we have db from that we are fetching blogs going to come up. There are no new blogs . So we are using SSG.
btw at some point you will have new blogs, no?
ofc you can re-deploy(build) your project every time you have new blogs
but again, SSG is NEVER for pagination with query params
unless you use dynamic routing
like
/page/1 /page/2if you have fixed number of blogs I think that's possible
DonskoyOP
Yeah NO NEW BLOGS
got it. Let me check with that
got it. Let me check with that
DonskoyOP
can you share some excample of it @James4u it would be great help?
so instead of query params like ?query=1 you can use dynamic routes
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
like when you build your project you will have all of your pages built under those dynamic routes
page/1page/2@Donskoy it's nearly the same as individual blog page
but instead you will have a list of pages
what part of it don't you understand?
DonskoyOP
yeah. let me check if it's working
DonskoyOP
can't we just render all the pages, and in next js, we handle page parameter in runtime?
@James4u
render all the pages?
did you mean individual pages?
when it comes to navigation it's sort of a group of pages
how will you render them?
and by page parameter, did you mean query param?
DonskoyOP
yeah query param
DonskoyOP
okay got it let me confirm with client in that case
they have some SEO things
dyanic routes will be better in terms of SEO
because you have static pages built
DonskoyOP
but thing is my whole website is in SSG. How I will be able to develop only this paginated page using dynamic route.
is it possible?
yeah ofc
DonskoyOP
moreover I have this in my nextjs.config
Answer
did it work? @Donskoy
DonskoyOP
Nope
hmm what's your trouble?
DonskoyOP
I am thinking to write basic js which will handle pagination at static level. initially I will fetch the all blogs and then I will use .js file to handle it and load it via useEffect at initial stage when application gets load
Pygmy Nuthatch
Make a next server route using route.ts
DonskoyOP
just to update you I am using export : "output" for static website. will it work in this case?
Pygmy Nuthatch
Well damn
@Donskoy It's not a good idea
fetch all blogs and manage them by js to "simulate" pagination? it doesn't make sense
I wonder what's the trouble with static generation of dynamic routes?
see, you have 100 blogs
your dynamic route will group blogs by 10 (it's up to you)
...
your dynamic route will group blogs by 10 (it's up to you)
blogs/page/1 => blog 0 - 9blogs/page/2 => blog 10 - 19...
if you don't like searchParam with SSR, this is the best option
DonskoyOP
I have to confim this with client actually
@chisto export default async function Page({ searchParams }) {
...
<BlogArticles searchParams={searchParams} />
...
}
This is the only right answer here in regards to search params in server components😄
You can access search params on the server side
@Jboncz he doesn't want to use searchParams
he wants SSG not SSR
DonskoyOP
@James4u , I have created pages using dynamic routing. Now I will confirm with client whether they want in that way or not
Thanks a lot for all your guidance
They should be okay
DonskoyOP
🤞
as you said they want static generation
DonskoyOP
yeah