This week I learnt server-side pagination, and omo, it humbled me.

On paper it's simple: instead of fetching every product and using .slice() like in client-side pagination, you ask the server for only the page you need. In practice I hit 13 bugs, and this is the longest I've ever spent on one concept.

I was building with dummyjson.com and the plan grew in stages: pagination, then search, then debouncing, then putting everything in the URL. Here are the bugs that taught me the most.

The core idea

For page 4 with 10 products per page:

const skip = (currentPage - 1) * productsPerPage; // (4 - 1) * 10 = 30 
https://dummyjson.com/products?limit=10&skip=30 

The server also sends back total, so the number of pages is derived, not stored:

const totalPages = Math.ceil(total / productsPerPage); 

Bug 1: The URL rule I didn't know existed

When I added search, I found a URL online that looked like https://dummyjson.com/products?laptop. I used it and got a bad request. I changed it to ?search=laptop and it still didn't work the way I expected.

After a lot of back and forth, I learnt that every API has its own request structure, and DummyJSON's search lives on a different endpoint:

https://dummyjson.com/products/search?q=laptop 

The Network tab showed 200 OK, yet my products weren't changing. A 200 only means the server handled the request. It doesn't mean it did what I meant. Now I check both the Request URL and the Response body in DevTools, and I read the docs before copying a URL from somewhere.

(Also: user input goes through encodeURIComponent() before it goes into a URL.)

Bug 2: Search only worked after I deleted a letter

This one stressed me, and I'm proud of how I found it. I noticed that when I typed a product name quickly, nothing updated. But when I deleted one letter, the search suddenly ran.

My fetching effect looked like this:

useEffect(() => { // fetching logic }, [currentPage, searchTerm]); 

But the URL inside it was built from debouncedSearchTerm. So:

  1. I type "phone" and searchTerm updates.
  2. The effect runs immediately, using the old debouncedSearchTerm.
  3. 500ms later debouncedSearchTerm becomes "phone".
  4. The effect doesn't run again, because debouncedSearchTerm isn't in its dependencies.
  5. I delete a letter, searchTerm changes, and finally a request fires.

The fix:

}, [currentPage, debouncedSearchTerm]); 

Lesson: the values that decide what a side effect does must be in that effect's dependencies.

Bug 3: controller.abort vs controller.abort()

To avoid race conditions (an old request finishing after a newer one and showing wrong results), I used AbortController. My cleanup looked fine:

return () => { controller.abort; }; 

It cancelled nothing. I never called the function. That's a JavaScript problem, not a React one: a function reference is not a function call. I wasn't paying attention to detail, and it cost me time.

The part I loved: URL search parameters

Moving page and search into the URL with useSearchParams was the most practical thing I learnt this week.

I expected refresh to reset to page 1, like it did with useState. Instead the page survived refresh, and I can now share a link like localhost:5173/?page=3&search=phone and the other person lands on exactly what I was looking at.

My final design:

  • Local state: searchTerm, for instant typing feedback
  • 500ms debounce: wait until typing stops
  • URL: the committed search and page
  • Fetching effect: requests products using the URL values

Once the URL became the source of truth, debouncedSearchTerm no longer needed its own state. That was my own call, and I'm getting better at asking "does this value really need to exist on its own?" before adding more state.

Stage 7 was the most frustrating, and Bug 13 stressed me the most

Combining the URL, debouncing and pagination broke things in ways I didn't expect: updating one parameter wiped another, every keystroke reset the page, Back created junk history entries, and shared links reset themselves to page 1 after 500ms.

Bug 13 was the worst. I'm on ?search=laptop, press Back, and the URL returns to ?search=phone, but the input still says "laptop". My debounce effect sees the mismatch, thinks it's a new search, and overwrites the URL with "laptop" again.

The fix I settled on is to sync the input whenever the URL changes:

useEffect(() => { setSearchTerm(urlSearchTerm); }, [urlSearchTerm]); 

Honest note: I haven't tested every Back/Forward case with this fix yet. I'd already spent too long on one concept, so I moved on. I'm sharing it as "reasonable, not verified", and it's on my list to test.

What this week taught me

  • Trace your code's execution properly. Almost every bug here (stale dependencies, effects firing on mount, mismatched state) was found by walking through what runs, in what order, with which values. This can't be over-emphasized.
  • Finding the bug is half the work, and the fix needs real reasoning too. The first fix that comes to mind often creates the next bug.
  • React is getting past syntax. I'm entering the stage where you have to think and think. I even got tired at some point.
  • Wondering aloud: I kept thinking about developers who learnt this without AI and wondering how deeply they must have reasoned. I can't prove they were sharper, but it made me want to make sure that, with AI around, I'm still doing the tracing myself.

What I'd do next

Test Back/Forward properly, then think about how to get rid of the useEffect that syncs input from the URL.

If you've handled debounced search plus URL state in a cleaner way, I'd love to hear how.