My New Favorite Pagination Method

  Рет қаралды 73,455

Josh tried coding

Josh tried coding

11 ай бұрын

Pagination is so important when it comes to displaying data in your app. This approach of server-side pagination in Next.js 13 Server Components is so cool for that. I implemented something similar on accident while building a dashboard and figured this works so well with pagination too.
Test this for yourself!: gist.github.com/joschan21/7ad...
-- my links
Discord: / discord
My GitHub: github.com/joschan21

Пікірлер: 123
@robertomaurizzi1073
@robertomaurizzi1073 11 ай бұрын
As an old backend engineer, every time I see a video or article about all these "new cool methods to solve frontend problems" that are actually decades-old plain simple server-side approaches I silently cry... 😢
@drewwellington2496
@drewwellington2496 11 ай бұрын
I have, without a doubt, been doing server-side pagination for longer than Josh has been alive & the thought of this being a "new" method makes me feel sorry for anyone who only knows a lot of these buzzword frameworks.
@rand0mtv660
@rand0mtv660 11 ай бұрын
I love frontend development, but I agree with you. It seems people are re-discovering things and showing them as they are something new. I mean nothing wrong about showcasing this, but it's not something revolutionary. What's great is that these frameworks are starting to embrace the browser and server more and more and are not trying to bypass them somehow, but rather align with them more.
@robertomaurizzi1073
@robertomaurizzi1073 11 ай бұрын
@@rand0mtv660 showing and talking about them isn't wrong: the problem is that this is a symptom that a lot of knowledge has been thrown away wholesale by the "JS world" and that can't be good. I'd expect things like this to be taught in books and courses, instead they're being "rediscovered" with great fanfare after literally wasting years using questionable approaches... my favorite is when they noticed it might be a good idea to pre-render things on the server and not use client-side JS (rehydrated or otherwise) at all unless where it's necessary... 🤦‍♂
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
Oh I see why it might come across that way. Be "new" I just mean I didn't know it before and find it cooler than client side pagination now. It's not that the method is new, but rather that it's my new favorite
@robertomaurizzi1073
@robertomaurizzi1073 11 ай бұрын
@@joshtriedcoding that's completely fine! 😀but it's sad that nobody (teachers/writers/trainers) thought it would be a good idea to show you this approach and many others (like @germandkdev answer about keyset pagination for a good example of why this approach could have problems) that have literally decades of story.
@buddy.abc123
@buddy.abc123 11 ай бұрын
Thanks for this Josh, great intro for beginners to learn about pagination. Like a few others have said the bookmarks won't necessarily give you the same results all the time. I think cursor pagination is better suited for that as well for performance in the long run
@michaelzucker772
@michaelzucker772 11 ай бұрын
Josh, this is a very well done video. Thank you for making it! Despite some others' comments, it's a perfect solution in the appropriate context. This video demonstrates the concept of server side pagination very well and achieves the goal of walking through those concepts in a clear and understandable way. Bravo!
@irfansaeedkhan7242
@irfansaeedkhan7242 2 ай бұрын
i wasted to much on pagination for server side and you solved my problem in 2 mins, may you are so awesome, practical and concise
@germandkdev
@germandkdev 11 ай бұрын
With Offset Pagination you're only getting the same data if the data doesn't change (nothing can be added/removed in the middle of the collection). Keyset Pagination (after_id) is a easy way to at least preventing skipping a elements at the start of a page because of a change, but still can be affected by changes, just not as strongly. Cursor-based Pagination can prevent changes completely if done correctly, but can also be a bit resource-heavy
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
Very interesting! Thanks for sharing
@emee__
@emee__ 11 ай бұрын
Thanks
@nomadshiba
@nomadshiba 11 ай бұрын
i also do that. you can never trust some offset number. user loads the items, then something gets deleted, user scrolls down to load more and we have duplicates that also might cause errors
@germandkdev
@germandkdev 11 ай бұрын
@@nomadshibaYeah the most annoying pages are some where the offset search results auto-reload every 30s or so, sometimes making results that were at the end of the page in the middle instead or just skipping some results
@paca3107
@paca3107 11 ай бұрын
Thank u for this tutorial. I just needed use pagination for my current project.
@codelam
@codelam 15 күн бұрын
Great video. i was suffering with convex to make it but with your video i made it finally
@ValipPowa
@ValipPowa 26 күн бұрын
bro broke the 4th wall of development and noticed the punis
@DivyJani-yc1fw
@DivyJani-yc1fw 2 ай бұрын
Bro Bro perfect work simple and straightforward thank you so much
@user-po2iy4hq7v
@user-po2iy4hq7v 11 ай бұрын
hi. i saw your video. thank you for helping! can i ask you something? home component has searchparams as a props. can i use this props(searchParams) other component like this video????
@comaecod
@comaecod 11 ай бұрын
2:54 Kinda looks like a.....😆.
@rockNbrain
@rockNbrain 11 ай бұрын
Nice job Josh !
@shafiqbelaroussi1250
@shafiqbelaroussi1250 10 ай бұрын
I have a similar setup where Im chaing the searchparams inside the onChange of an input. the page (server comp) is calling the function and fetching the data (I can see it logging) but the page was not "forced" to refresh/rerender like it's doing with you. what could I be missing here. EDIT: the URL is also changing on the browser with the new serachparms
@brandonjohnson7822
@brandonjohnson7822 11 ай бұрын
great video, however when i follow this and have two pagination controls on the page (one at the bottom and one at the top) they get out of sync due to NextJS caching features (i think). It might be worth it to mention/address this concern.
@hussainbhagat8855
@hussainbhagat8855 10 ай бұрын
But how do we do these things as well as show the pages as buttons to directly jump on whilst also calling the db depending on the number on the button?
@felipejzrd
@felipejzrd 11 ай бұрын
This is cool for small applications, but in the real world, when you are getting the entries from a database, you should never query all the data and slice it. Instead, you should do the pagination at the database level. Great work as always!
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
Hi dude, you're 100% right. That's what I meant with the skipped and selected data in the comment - after all that is the entire idea behind pagination. Appreciate you man!
@dvlden
@dvlden 11 ай бұрын
Sometimes, in the real-world apps, you will also get a bigger chunk of data (like 100 entries) per request, but still only display about 10 per page. Josh, using the slice here (for the simple demo), is actually informative. We would be slicing 10 per page, until we reach all of the entries that we received, then we'd fetch additional 100 entries.
@fersaysrelax
@fersaysrelax 11 ай бұрын
@@dvlden if this db query that returns 100 is happening server side, how do you keep those around till the next request? shouldn't the server be stateless so it can scale horizontally? or is nextjs doing something I'm not aware here?
@mahdiidrissi9712
@mahdiidrissi9712 11 ай бұрын
That is exactly what I was thinking about while watching the video.
@dvlden
@dvlden 11 ай бұрын
@@fersaysrelax Well, if you reload the page, state on the frontend application is gone. Backend will serve those same 100 results. This time it could be cached Redis response instead, so no DB query. Not talking about Next.js / React per se, been using this approach on frontend for a couple of projects.
@yeico80
@yeico80 11 ай бұрын
Thanks for the amazing content!!! 🎉
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
Thank you man. Means a lot.
@alexlykesas9733
@alexlykesas9733 2 ай бұрын
You didnt mention how to get the data from api. Should it happen serverside? Should only initially happen server-Side and all other controls client-side? whats the best way?
@TomasJansson
@TomasJansson 11 ай бұрын
Why not have the pagination controller be server side rendered as well? All the page numbers and next/previous are just regular links.
@FranciscoMarcosMilhomemAbreu
@FranciscoMarcosMilhomemAbreu 5 ай бұрын
What is your fifth left panel? I am curious.
@gaurav_sachdeva
@gaurav_sachdeva 3 ай бұрын
How would you implement infinite scroll type of pagination using this server-side method?
@XDNickXP
@XDNickXP 9 ай бұрын
Hey Josh, have you tried this with real data because this is exactly what I have done with the app router. All i am seeing is cached data it never fetches for new data even though I have no-cache on the fetch to an API.
@jorden123
@jorden123 11 ай бұрын
Hi, What about changing the page manually in the url?
@artu-hnrq
@artu-hnrq 11 ай бұрын
Hey Josh! I see you always use FC to build your components, as well I saw in Shadcn-ui a React.forwardRef is widely used. Could you do a video about the differences from these multiple ways React allow us to define components?
@vincentnthomas1
@vincentnthomas1 3 ай бұрын
Just basic ts man
@darshandhabale143
@darshandhabale143 6 ай бұрын
loading state for navigation between pages?
@grouby3850
@grouby3850 11 ай бұрын
Just now I wanted to search how to implement pagination to my site and this video popped up on my home page.
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
I got you fam
@oleksandrploskovytskyy1520
@oleksandrploskovytskyy1520 11 ай бұрын
Somehow searchParams page props are not available if you wrap routes in a group like (dashboard)/whatever/route. Did anyone figure out a solution for it?
@aBradAbroad
@aBradAbroad 11 ай бұрын
Another absolute banger
@ojal.dev.
@ojal.dev. 6 ай бұрын
How can I implement server-side pagination by integrating an API? Please help!
@nro337
@nro337 11 ай бұрын
Great topic!
@ojal_sharnagat
@ojal_sharnagat 6 ай бұрын
How to do server side pagination by just fetching the data from an external API (without using MongoDB )? Please help! I'm stuck in the middle of a project.
@fersaysrelax
@fersaysrelax 11 ай бұрын
I've been storing the pagination state in the query string since the time of angularJS. This is nothing new. What's cool is not having to handle pagination via API calls to the backend
@wirapramuja007
@wirapramuja007 11 ай бұрын
hello josh when tutorial fullstack come out?
@codeitraw5241
@codeitraw5241 Ай бұрын
what if people manually change the params?
@Israel_chidera
@Israel_chidera Ай бұрын
Thanks Josh
@gammon9281
@gammon9281 11 ай бұрын
How would you implement this if your paginated table is connected to a search form? Also pass everything as url param? I can imagine that the url will get out of hand pretty quickly, especially if there are complex query params like multi selects etc. How would you solve this?
@Notoriousjunior374
@Notoriousjunior374 11 ай бұрын
You should always a default query for the optional query parameters. Say you have filter, sort asc/desc columns (country, name, age for eg), if none of those parameters are present then proceed with your default parameters for them if not then use users provided query.
@drewwellington2496
@drewwellington2496 11 ай бұрын
New? I absolutely guarantee I've been using server-side pagination for longer than you have been alive 😃
@zivtamary
@zivtamary 11 ай бұрын
Yup lol
@evansrobbie7335
@evansrobbie7335 29 күн бұрын
How comes am only getting to know this rn? Thanks mate
@federico.r.figueredo
@federico.r.figueredo 10 ай бұрын
Dude, we 've been doing this server-side pagination since the old vanilla PHP days...
@lucas.p.f
@lucas.p.f 11 ай бұрын
You actually need zero javascript for this same problem if you just switch router navigation to tags. You can do everything there with just a server component. Mind you, when using as buttons, always provide role="button" to it for accessibility purposes.
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
Oh that is a very interesting thought
@lapulapucityrider3227
@lapulapucityrider3227 11 ай бұрын
You lose spa like behavior
@lucas.p.f
@lucas.p.f 11 ай бұрын
@@lapulapucityrider3227 you mean the smooth transition? Just use a then. Still everything would be inside the RSC. I just said tag because I come from SvelteKit, so I forgot that Next.js comes with its built-in component
@Sree_ShortsYT
@Sree_ShortsYT 11 ай бұрын
thnks for updating gist
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
oh oops right
@turing4991
@turing4991 10 ай бұрын
Where do you get the searchParams from...is it default on every nextjs page?? i'm really confused
@joshtriedcoding
@joshtriedcoding 10 ай бұрын
yeah its default at the page level
@tshepokhumako1403
@tshepokhumako1403 3 ай бұрын
Please do a video using plain sql with nextjs14
@healingwithlove8614
@healingwithlove8614 11 ай бұрын
I think similarly we can make search functionality serverside in nextjs
@pratheeshm400
@pratheeshm400 5 ай бұрын
what is tsx?
@user-xu3lj1ej8d
@user-xu3lj1ej8d 4 ай бұрын
Thanks, very helpfull!
@absolute_atom
@absolute_atom 2 ай бұрын
Next js 14 you cant put client components inside server component.
@leeyahav4754
@leeyahav4754 7 ай бұрын
thnx my friend . it was good
@aguud
@aguud 8 ай бұрын
YOU ARE AWESOME
@nicolasguillenc
@nicolasguillenc 11 ай бұрын
I like this approach for how easy it seems, however, I like the UX when you click the "Next page" button and you see a loader and the button is momentarily disabled, and maybe see a cool animation when new content is added. Also you can save the results in state so when you come back to the page it doesn't query that data again and it's just there instantly. Stop reading if you don't want to see me vent.... I've been frustrated because the whole ecosystem is a mess right now. There are so many ways of doing things and it does not even matter if I can get an interview. A lot of dependencies or tools have not released versions that are compatible with Next 13 and there are so many UX decisions to make...
@hugo-abdou
@hugo-abdou 4 ай бұрын
thats cool for SSR but what about SSG
@hastingskondwani1066
@hastingskondwani1066 11 ай бұрын
please do it with a real world database josh
@Shubham-yc6nz
@Shubham-yc6nz 11 ай бұрын
So the data will be fetched on demand from the database? Or you are pulling all data then pagination?
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
Only fetch on demand, that's the idea behind pagination. Having all the data right there in the array just makes the principle much easier to understand in a video
@Shubham-yc6nz
@Shubham-yc6nz 11 ай бұрын
@@joshtriedcoding oks thanks. Yeah Got it. 🙂
@buraxta_
@buraxta_ 5 ай бұрын
that's amazing!
@markwonder8168
@markwonder8168 11 ай бұрын
Will this also work with pages directory and getServerSideProps?
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
As long as you can get the searchParams, I think there would be nothing stopping you
@snivels
@snivels 10 ай бұрын
That looks like a giant... Johnson! Get on the horn to British Intelligence and let them know about this
@8koi245
@8koi245 11 ай бұрын
I swear my boss loves you lmao
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
haha cheers appreciate both of you
@SeanCassiere
@SeanCassiere 11 ай бұрын
Just so there is no confusion... This isn't server-side pagination, rather this the benefits of storing state in the URL. (Something which Tanner is taking to another level with Tanstack Router). Server-side pagination is the process of cutting down the number of items sent back to the client to reduce bandwidth and increase speed. Client-side routing conversely always gets back the ENTIRE list of items from the server and then handles the pagination part in the browser/device. A common disadvantage of server-side pagination is the fact that you need to request each page, however the smaller payload size can matter when you are dealing with 10k+ records. For client-side pagination, although the pagination part feels smooth since all the data is available locally (onces fetched), it requires the browser to fetch a bunch of data which it potentially doesn't need and have to store in RAM.
@mohakbajaj4235
@mohakbajaj4235 11 ай бұрын
Noice i just used it add pagination in a app i am building
@gabriellsscolaro
@gabriellsscolaro 11 ай бұрын
And store using cookies or something like that is a shit?
@user-ux6wv1sz4g
@user-ux6wv1sz4g 11 ай бұрын
thanks 💯
@SMITSHINGALA
@SMITSHINGALA 10 ай бұрын
can make prev and next button pagination with MVC formet better to understand
@SMITSHINGALA
@SMITSHINGALA 10 ай бұрын
in node js
@yagelProject
@yagelProject 11 ай бұрын
Cool Bro 🤘👍
@kosti2647
@kosti2647 11 ай бұрын
everything is fine until u start considering SEO, recently been implementing pagination for a blog in my job, talked with SEO specialist and he told me it's kinda bad to have these as query params, cuz google would try to index these, ofc u can set up meta canonical tag but it's not 100% reliable, he said that the best practice would be to even have these in slug, like /blog/page/1, but when there's more params it can get messy, so yeah, not sure still what's the best practice really when it comes to pagination. I could handle everything on the client with js, but then I'm losing the ability to link to specific page and so on, I could do this with query params, but then it's bad for SEO. Wdyt?
@Wakkyguy
@Wakkyguy 11 ай бұрын
I read the same thing. Can't think of a way that makes everyone happy.
@spicynoodle7419
@spicynoodle7419 11 ай бұрын
You can pass the query params as headers
@darshandhabale143
@darshandhabale143 6 ай бұрын
thats total bs firstly, you aren't going to rank your blog list pages, even if you want to, you have your meta tags there's nothing that makes server side pagination bad for SEO,
@Bhanukamax
@Bhanukamax 2 ай бұрын
Use a real data fetch from db or external data source which takes more than a few milliseconds and see how pagination feels so laggy or frozen.
@8koi245
@8koi245 11 ай бұрын
instead of using buttons yoy could use a Link and render a button if there's no next/prev page! 🎉
@tokyoknight6676
@tokyoknight6676 9 ай бұрын
I know this is just a demonstration but don't put every single record into a variable called data and do client side pagination controls using array.slice like that. If you have 10,000 objects in memory you're gonna have a bad time. You should plug the start and end into some sort of range statement in your query and just retrieve the number of records you want based on the limit you defined in per_page. If you're using Supabase they have a range() function on the query builder.
@aurelianspodarec2629
@aurelianspodarec2629 7 ай бұрын
I worked at a company like that. I told them about the issues and the senior dev got mad at me and fired. Emotional creature.
@tokyoknight6676
@tokyoknight6676 7 ай бұрын
Sorry to hear that@@aurelianspodarec2629
@wandreperes
@wandreperes 11 ай бұрын
static data, Narnia, Pandora...ok..lol... but, good job.
@anwar_thoughts2738
@anwar_thoughts2738 8 ай бұрын
this kinda looks like a uhmm 😂 , otheriwse nice tips like usual Josh thanx
@elab4d140
@elab4d140 11 ай бұрын
we want server pagination with react table
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
right, for tables this should be super nice too
@HorizonHuntxr
@HorizonHuntxr 11 ай бұрын
Bold of you to assume that I can read 😅
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
aw man maybe I should have repeated it
@yowremco
@yowremco 11 ай бұрын
Josh man it's really time to upgrade your setup and replace those monitor stands with desk mounted gasspring arms. Srs will do alot of aesthetics and space.
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
honestly I dont really care about my setup, just wanna build cool shit
@Yetisnowstop
@Yetisnowstop 11 ай бұрын
You literally didn’t do what you had in the thumbnail.
@joshtriedcoding
@joshtriedcoding 11 ай бұрын
the thumbnail contains an image of pagination, that's literally the topic for the entire 5:45 of the video. No idea what you mean
@Peter-bg1ku
@Peter-bg1ku 3 ай бұрын
Bro there's nothing new here. Any old framework does this out of the box
@apurba1212
@apurba1212 9 ай бұрын
Well this kinda looks like a... amm anyways😆
@marlonjerezisla6496
@marlonjerezisla6496 11 ай бұрын
didn't knew you couldn't use url params to store your pagination state in the FE 🤦‍♂. Stop saying SS components are best.... they are not 95% of the time.
@abdellatif.x8127
@abdellatif.x8127 3 ай бұрын
dude, are u serious ?, this is not a server side pagination !!!!!!! it's a dummy pagination, imagine you have in your database millions of records ??? how u can handle this
@xya6648
@xya6648 11 ай бұрын
How rude of you to assume I can read 🤣
@nested9301
@nested9301 11 ай бұрын
NO THANKS
@filipkovac767
@filipkovac767 11 ай бұрын
I don't mean to be negative, but given the content, this should have been youtube short at max. You can squeeze a lot more info in 5+min video
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 191 М.
How Did I Not Know This TypeScript Trick Earlier??!
9:11
Josh tried coding
Рет қаралды 205 М.
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 12 МЛН
JavaScript Pagination in ~10 Minutes (Super EASY!!)
13:06
James Q Quick
Рет қаралды 26 М.
React Query Is (Still) Essential - My Favorite React Library
11:04
Theo - t3․gg
Рет қаралды 144 М.
Server-side Pagination with NextJS 13 Server Actions
19:27
Taylor Lindores-Reeves
Рет қаралды 10 М.
This Package Saved My SaaS
5:46
Josh tried coding
Рет қаралды 111 М.
They made React great again?
4:11
Fireship
Рет қаралды 1 МЛН
Fetching Data Doesn't Get Better Than This
6:58
Josh tried coding
Рет қаралды 90 М.
Pagination in MySQL - offset vs. cursor
13:20
PlanetScale
Рет қаралды 55 М.
React Pagination in 7 minutes [ EASY ] | Pagination Tutorial
7:40
Code Bless You
Рет қаралды 67 М.
You won't BELIEVE what I just did with TanStack's React Tables!
35:58
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 6 МЛН
Tag her 🤭💞 #miniphone #smartphone #iphone #samsung #fyp
0:11
Pockify™
Рет қаралды 32 МЛН
OZON РАЗБИЛИ 3 КОМПЬЮТЕРА
0:57
Кинг Комп Shorts
Рет қаралды 1,5 МЛН
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33
Ремонтяш
Рет қаралды 348 М.
Спутниковый телефон #обзор #товары
0:35
Product show
Рет қаралды 2,2 МЛН