← Blog

javascript · 2026-11-19 · 7 min

I Didn't Like the Loading Spinner, So I Changed How Infinite Scroll Works

A practical approach to making infinite scroll feel instant by keeping upcoming data buffered and ready before the user needs it.

I was experimenting with infinite scroll using plain HTML, CSS, JavaScript, and a random user generator API.

The basic idea was simple: load some users, display them, and when the user reaches the end, load more.

It worked.

But there was one thing I didn't like.

The loading spinner.

The user would reach the end of the current data, the application would make another API request, and the user would have to wait for the response before continuing.

Technically, everything was working exactly as expected.

But from a UX perspective, it felt like the application was asking the user to wait for something that it could have prepared earlier.

That led me to a simple idea:

What if the application always had some upcoming data ready before the user needed it?

That became the idea behind my approach: a small prefetch buffer between the API and the UI.

The Traditional Infinite Scroll

Let's say we're displaying users in batches of 20.

A typical infinite scroll implementation looks like this:

Load 20 users
     ↓
Display them
     ↓
User scrolls
     ↓
User reaches the bottom
     ↓
Fetch next 20 users
     ↓
Loading spinner ⏳
     ↓
API responds
     ↓
Display next 20 users

There's nothing inherently wrong with this.

In fact, it's probably the most straightforward way to implement infinite scroll.

The problem is when the network request starts.

The application waits until the user has already consumed everything currently available.

Only then does it begin fetching what comes next.

So the user reaches the end and the application effectively says:

"Wait, let me go get that for you."

Even a small delay becomes noticeable when it happens exactly at the point where the user expects more content.

Instead of Loading at the Edge, Keep a Buffer

Rather than thinking about infinite scroll as:

show → reach end → fetch → show

I started thinking about it as a buffered data pipeline:

API
 ↓
Prefetch buffer
 ↓
UI

The UI consumes data from the buffer while the application keeps filling the buffer with upcoming data.

For example:

                 API
                  │
                  ▼
        ┌──────────────────┐
        │  Prefetch Buffer │
        │                  │
        │  Chunk 2         │
        │  Chunk 3         │
        └────────┬─────────┘
                 │
                 ▼
                UI

The user doesn't need to know that this buffer exists.

They simply keep scrolling.

When they need the next chunk, it's already there.

A One-Chunk Buffer

The simplest version is to keep one chunk ahead of the user.

Suppose the user is currently viewing chunk 1:

┌───────────────┐
│   Chunk 1     │
│               │
│  User sees    │
│  this data    │
└───────────────┘
 
┌───────────────┐
│   Chunk 2     │
│               │
│  Prefetching  │
└───────────────┘

While the user is consuming chunk 1, chunk 2 is being fetched in the background.

Once it finishes:

┌───────────────┐
│   Chunk 1     │
│               │
│  User sees    │
└───────────────┘
 
┌───────────────┐
│   Chunk 2     │
│               │
│  Ready ⚡     │
└───────────────┘

Now when the user reaches the end of chunk 1:

Chunk 1
   ↓
Chunk 2 ⚡

There is no new network request to wait for at that moment.

The application simply consumes the already-prepared chunk.

And as soon as chunk 2 becomes the visible chunk, the application starts preparing chunk 3.

Chunk 1 → Chunk 2 → Chunk 3 → Chunk 4 → ...
           ↑             ↑
   currently visible   being prepared                 
             

The buffer keeps moving forward with the user.

You Can Buffer More Than One Chunk

A single chunk ahead is useful, but you don't have to stop there.

You can maintain a larger buffer when you want more protection against slower networks or faster scrolling.

For example, with a buffer size of 2:

User sees:
 
┌───────────────┐
│   Chunk 1     │
└───────────────┘
 
Already ready:
 
┌───────────────┐
│   Chunk 2     │
├───────────────┤
│   Chunk 3     │
└───────────────┘
 
Being prepared:
 
┌───────────────┐
│   Chunk 4     │
└───────────────┘

As the user moves forward:

Before:
 
Visible → Chunk 1
Buffer  → Chunk 2, Chunk 3
 
 
Then:
 
Visible → Chunk 2
Buffer  → Chunk 3, Chunk 4
 
 
Then:
 
Visible → Chunk 3
Buffer  → Chunk 4, Chunk 5

The application is constantly consuming from one side of the buffer and filling it from the other.

This gives the network more time to prepare upcoming data.

However, a larger buffer isn't automatically better.

If the user stops scrolling after chunk 1, every prefetched chunk after it was unnecessary work.

More buffering also means more bandwidth and memory usage.

So the buffer size should be a deliberate trade-off.

A simple implementation might expose it as a configuration:

createInfiniteData({
  fetcher: fetchUsers,
  buffer: 2
});

The important idea isn't that 2 is the perfect number.

It's that upcoming data can be kept ready instead of making the UI wait for the network.

Why Does This Feel Faster?

There's an important distinction here.

The API didn't become faster.

The network request still takes the same amount of time.

What changed is when the waiting happens.

Without prefetching:

User needs data
      ↓
Start request
      ↓
Wait
      ↓
Data arrives
      ↓
Show data

With a buffer:

Start request
      ↓
User continues scrolling
      ↓
Data arrives
      ↓
Keep it ready
      ↓
User needs data
      ↓
Show it immediately

We're essentially moving network latency into time where the user wasn't waiting for that data anyway.

That's a small but useful example of perceived performance.

The application isn't necessarily doing less work.

It's doing the work earlier.

Prefetching Isn't Loading Everything

One thing I like about this approach is that it still avoids loading the entire dataset.

Imagine an API with 10,000 users.

We don't want to request all 10,000 users when the page opens.

Instead, we might have:

20 visible
20 buffered

Or with a larger buffer:

20 visible
20 buffered
20 buffered

As the user moves forward, old data can become visible while new data is fetched to keep the buffer filled.

So we're getting a balance:

  • We don't load the entire dataset at once.
  • We don't make the user manually request every batch.
  • Upcoming data is usually ready before it's needed.
  • The amount of prefetched data remains controlled.

The JavaScript Doesn't Need to Be Complicated

The important part isn't a specific framework or library.

The concept can be implemented with plain JavaScript.

At the simplest level, the application needs to distinguish between the data the UI is currently consuming and the data that has already been prepared for later.

Conceptually:

let currentChunk = [];
let nextChunk = null;

When the current chunk is displayed, start preparing the next one:

nextChunk = fetchNextChunk();

When the user needs it:

currentChunk = await nextChunk;

And immediately begin preparing the following chunk:

nextChunk = fetchNextChunk();

A production implementation obviously needs more than this.

You need to handle:

  • Request failures
  • Duplicate requests
  • Loading states
  • End-of-data detection
  • Page-based APIs
  • Cursor-based APIs
  • Cancellation
  • Caching
  • Buffer management

But the underlying idea is simple.

Start Before the User Reaches the End

There's another important detail.

You don't necessarily have to wait until the user reaches the exact bottom of the list before starting the next request.

You can start filling the buffer before that point.

For example, using IntersectionObserver, you can place a sentinel near the end of the currently rendered content:

User content
User content
User content
User content
       ↓
   [ sentinel ]
       ↓
More content

When the sentinel approaches or enters the viewport, the application can start preparing more data.

So instead of:

Bottom reached → fetch

you get:

Approaching bottom
       ↓
Start fetching
       ↓
User keeps scrolling
       ↓
Data becomes ready
       ↓
User reaches the end
       ↓
Data is already available

That extra head start can make a significant difference on slower connections.

What If the User Scrolls Very Fast?

Prefetching isn't magic.

A user can still consume data faster than the network can provide it.

For example:

User:
Chunk 1 → Chunk 2 → Chunk 3 → Chunk 4
                    ↑
              catches up here
 
Network:
Chunk 2 ────────────────────┐
                            ↓
                         loading

If the user scrolls extremely quickly, the buffer can eventually become empty.

That's when a loading state may still be necessary.

And that's completely fine.

The goal isn't to promise that a loading spinner will never appear.

The goal is to make the normal experience smooth enough that the user rarely encounters it.

Good performance isn't always about eliminating loading states.

Sometimes it's about making them less frequent and less disruptive.

There Are Trade-offs

Prefetching isn't free.

Extra network requests

The user might never reach the data you've prefetched.

That means some network requests may ultimately be unnecessary.

More memory

A larger buffer means more data remains in memory.

This is especially important when each chunk contains large objects, images, or other heavy data.

API considerations

Different APIs paginate differently.

Some use page numbers:

?page=2
?page=3

Others use cursors:

?cursor=abc123

A reusable implementation needs to account for both.

Errors still happen

A background request can fail.

The application needs to know that the buffer couldn't be filled and decide whether to retry, show an error, or fall back to fetching when the user actually needs the data.

This Made Me Think About a Reusable Library

While building this experiment, I started thinking about whether this pattern could be extracted into a reusable npm package.

Instead of every application implementing its own infinite-scroll data logic, the application could use a data layer responsible for maintaining the buffer.

Something like:

Application
     ↓
Infinite data layer
     ↓
Prefetch buffer
     ↓
API

The application would consume the data, while the package handled things like:

  • Fetching upcoming chunks
  • Maintaining the buffer
  • Prefetching
  • Caching
  • Detecting when more data is needed
  • Loading states
  • Errors and retries
  • Page-based pagination
  • Cursor-based pagination

The UI would still belong to the application.

The package would handle the data-loading machinery underneath.

I'm not building that package right now.

For now, this is just an idea that came from solving a small UX problem in a simple JavaScript experiment.

But I like that progression:

A small problem → an implementation → a reusable pattern → potentially a reusable tool.

Infinite Scroll vs Pagination

I don't think infinite scroll is automatically better than pagination.

They solve different problems.

Pagination is useful when users need to:

  • Jump between pages
  • Know where they are in a dataset
  • Return to a specific result
  • Work with tables and structured data

Infinite scroll works particularly well for:

  • Feeds
  • Social content
  • Images
  • User directories
  • Product discovery
  • Exploratory browsing

For these experiences, repeatedly clicking Next can feel unnecessarily interruptive.

Infinite scroll lets users continue exploring without explicitly requesting every batch.

And when the upcoming data is already buffered, the experience can feel almost continuous.

The Bigger Lesson

The most interesting thing I learned from this wasn't actually how to implement infinite scroll.

It was this:

You don't always need to make an operation faster. Sometimes you just need to start it earlier.

A network request might take 500ms.

You probably can't make that particular request instant.

But if you start it two seconds before the user needs the result, those 500ms can effectively disappear from their experience.

That's what this experiment taught me.

The user shouldn't have to care that the application is fetching the next chunk.

They should just keep scrolling.

Behind the scenes, the application can quietly keep a small buffer of upcoming data ready for them.

And when they reach it:

It's already there.

0%