Infinite Scroll
- Server-Side
- Client-Side
- Loading Buffer
- URL Synchronization
- Resetting
- Loading Direction
- Reverse Mode
- Manual Mode
- Slots
- Custom Element
- Element Targeting
- Scroll Containers
- Programmatic Access
- Inertia::scroll() Method
Inertia's infinite scroll feature loads additional pages of content as users scroll, replacing traditional pagination controls. This is great for applications like chat interfaces, social feeds, photo grids, and product listings.
Server-Side
To configure your paginated data for infinite scrolling, use Inertia::scroll()inertia::scroll_with() (or inertia::scroll() for an already-fetched paginator) when returning your response. This automatically configures the proper merge behavior and normalizes pagination metadata for the front-end component.
1Route::get('/users', function () {2 return Inertia::render('Users/Index', [3 'users' => Inertia::scroll(fn () => User::paginate())4 ]);5});1Route::get('/users', function () {2 return Inertia::render('Users/Index', [3 'users' => Inertia::scroll(fn () => User::paginate())4 ]);5});
1use laravel::inertia::{self, Page, props};23use crate::models::User;45pub async fn index() -> Page {6 inertia::render("Users/Index", props! {7 users => inertia::scroll_with(async || {8 User::query().order_by(User::id).paginate(15).await9 }),10 })11}1use laravel::inertia::{self, Page, props};23use crate::models::User;45pub async fn index() -> Page {6 inertia::render("Users/Index", props! {7 users => inertia::scroll_with(async || {8 User::query().order_by(User::id).paginate(15).await9 }),10 })11}
Scroll props work with Laravel's standard, simple, and cursor paginators (paginate, simplePaginate, and cursorPaginatepaginate, simple_paginate, and cursor_paginate), as well as pagination data wrapped in Eloquent API resources. For more details, check out the scroll method documentation.
Client-Side
On the client side, Inertia provides the <InfiniteScroll> component to automatically load additional pages of content. The component accepts a data prop that specifies the key of the prop containing your paginated data. The <InfiniteScroll> component should wrap the content that depends on the paginated data.
1<script setup>2import { InfiniteScroll } from "@inertiajs/vue3";34defineProps(["users"]);5</script>67<template>8 <InfiniteScroll data="users">9 <div v-for="user in users.data" :key="user.id">10 {{ user.name }}11 </div>12 </InfiniteScroll>13</template>1<script setup>2import { InfiniteScroll } from "@inertiajs/vue3";34defineProps(["users"]);5</script>67<template>8 <InfiniteScroll data="users">9 <div v-for="user in users.data" :key="user.id">10 {{ user.name }}11 </div>12 </InfiniteScroll>13</template>
1import { InfiniteScroll } from "@inertiajs/react";23export default function Users({ users }) {4 return (5 <InfiniteScroll data="users">6 {users.data.map((user) => (7 <div key={user.id}>{user.name}</div>8 ))}9 </InfiniteScroll>10 );11}1import { InfiniteScroll } from "@inertiajs/react";23export default function Users({ users }) {4 return (5 <InfiniteScroll data="users">6 {users.data.map((user) => (7 <div key={user.id}>{user.name}</div>8 ))}9 </InfiniteScroll>10 );11}
1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()4</script>56<InfiniteScroll data="users">7 {#each users.data as user (user.id)}8 <div>{user.name}</div>9 {/each}10</InfiniteScroll>1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()4</script>56<InfiniteScroll data="users">7 {#each users.data as user (user.id)}8 <div>{user.name}</div>9 {/each}10</InfiniteScroll>
The component uses intersection observers to detect when users scroll near the end of the content and automatically triggers requests to load the next page. New data is merged with existing content rather than replacing it.
Loading Buffer
You can control how early content begins loading by setting a buffer distance. The buffer specifies how many pixels before the end of the content loading should begin.
1<InfiniteScroll data="users" :buffer="500">2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="users" :buffer="500">2 <!-- ... -->3</InfiniteScroll>
1<InfiniteScroll data="users" buffer={500}>2 {/* ... */}3</InfiniteScroll>1<InfiniteScroll data="users" buffer={500}>2 {/* ... */}3</InfiniteScroll>
1<InfiniteScroll data="users" buffer={500}>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="users" buffer={500}>2 <!-- ... -->3</InfiniteScroll>
In the example above, content will start loading 500 pixels before reaching the end of the current content. A larger buffer loads content earlier but potentially loads content that users may never see.
URL Synchronization
The infinite scroll component updates the browser URL's query string (?page=...) as users scroll through content. The URL reflects which page has the most visible items on screen, updating in both directions as users scroll up or down. This allows users to bookmark or share links to specific pages. You can disable this behavior to maintain the original page URL.
1<InfiniteScroll data="users" preserve-url>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="users" preserve-url>2 <!-- ... -->3</InfiniteScroll>
1<InfiniteScroll data="users" preserveUrl>2 {/* ... */}3</InfiniteScroll>1<InfiniteScroll data="users" preserveUrl>2 {/* ... */}3</InfiniteScroll>
1<InfiniteScroll data="users" preserve-url>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="users" preserve-url>2 <!-- ... -->3</InfiniteScroll>
This is useful when infinite scroll is used for secondary content that shouldn't affect the main page URL, such as comments on a blog post or related products on a product page.
Resetting
When filters or other parameters change, you may need to reset the infinite scroll data to start from the beginning. Without resetting, new results will merge with existing content instead of replacing it.
You can reset data using the reset visit option.
1<script setup>2import { router } from "@inertiajs/vue3";34const show = (role) => {5 router.visit(route("users"), {6 data: { filter: { role } },7 only: ["users"],8 reset: ["users"],9 });10};11</script>1213<template>14 <button @click="show('admin')">Show admins</button>15 <button @click="show('customer')">Show customers</button>1617 <InfiniteScroll data="users">18 <div v-for="user in users.data" :key="user.id">19 {{ user.name }}20 </div>21 </InfiniteScroll>22</template>1<script setup>2import { router } from "@inertiajs/vue3";34const show = (role) => {5 router.visit(route("users"), {6 data: { filter: { role } },7 only: ["users"],8 reset: ["users"],9 });10};11</script>1213<template>14 <button @click="show('admin')">Show admins</button>15 <button @click="show('customer')">Show customers</button>1617 <InfiniteScroll data="users">18 <div v-for="user in users.data" :key="user.id">19 {{ user.name }}20 </div>21 </InfiniteScroll>22</template>
1import { InfiniteScroll, router } from '@inertiajs/react'23export default function Users({ users }) {4 const show = (role) => {5 router.visit(route('users'), {6 data: { filter: { role } },7 only: ['users'],8 reset: ['users'],9 })10 }1112 return (1314 <button onClick={() => show('admin')}>Show admins</button>15 <button onClick={() => show('customer')}>Show customers</button>1617 <InfiniteScroll data="users">18 {users.data.map(user => (19 <div key={user.id}>20 {user.name}21 </div>22 ))}23 </InfiniteScroll>2425 )26}1import { InfiniteScroll, router } from '@inertiajs/react'23export default function Users({ users }) {4 const show = (role) => {5 router.visit(route('users'), {6 data: { filter: { role } },7 only: ['users'],8 reset: ['users'],9 })10 }1112 return (1314 <button onClick={() => show('admin')}>Show admins</button>15 <button onClick={() => show('customer')}>Show customers</button>1617 <InfiniteScroll data="users">18 {users.data.map(user => (19 <div key={user.id}>20 {user.name}21 </div>22 ))}23 </InfiniteScroll>2425 )26}
1<script>2 import { InfiniteScroll, router } from '@inertiajs/svelte'3 let { users } = $props()45 const show = (role) => {6 router.visit(route('users'), {7 data: { filter: { role } },8 only: ['users'],9 reset: ['users'],10 })11 }12</script>1314<button onclick={() => show('admin')}>Show admins</button>15<button onclick={() => show('customer')}>Show customers</button>1617<InfiniteScroll data="users">18 {#each users.data as user (user.id)}19 <div>{user.name}</div>20 {/each}21</InfiniteScroll>1<script>2 import { InfiniteScroll, router } from '@inertiajs/svelte'3 let { users } = $props()45 const show = (role) => {6 router.visit(route('users'), {7 data: { filter: { role } },8 only: ['users'],9 reset: ['users'],10 })11 }12</script>1314<button onclick={() => show('admin')}>Show admins</button>15<button onclick={() => show('customer')}>Show customers</button>1617<InfiniteScroll data="users">18 {#each users.data as user (user.id)}19 <div>{user.name}</div>20 {/each}21</InfiniteScroll>
For more information about the reset option, see the Resetting props documentation.
Loading Direction
The infinite scroll component loads content in both directions when you scroll near the start or end. You can control this behavior using the only-next and only-previous props.
1<!-- Only load the next page -->2<InfiniteScroll data="users" only-next>3 <!-- ... -->4</InfiniteScroll>56<!-- Only load the previous page -->7<InfiniteScroll data="messages" only-previous>8 <!-- ... -->9</InfiniteScroll>1011<!-- Load in both directions (default) -->12<InfiniteScroll data="posts">13 <!-- ... -->14</InfiniteScroll>1<!-- Only load the next page -->2<InfiniteScroll data="users" only-next>3 <!-- ... -->4</InfiniteScroll>56<!-- Only load the previous page -->7<InfiniteScroll data="messages" only-previous>8 <!-- ... -->9</InfiniteScroll>1011<!-- Load in both directions (default) -->12<InfiniteScroll data="posts">13 <!-- ... -->14</InfiniteScroll>
1{2 /* Only load the next page */3}4<InfiniteScroll data="users" onlyNext>5 {/* ... */}6</InfiniteScroll>;78{9 /* Only load the previous page */10}11<InfiniteScroll data="messages" onlyPrevious>12 {/* ... */}13</InfiniteScroll>;1415{16 /* Load in both directions (default) */17}18<InfiniteScroll data="posts">{/* ... */}</InfiniteScroll>;1{2 /* Only load the next page */3}4<InfiniteScroll data="users" onlyNext>5 {/* ... */}6</InfiniteScroll>;78{9 /* Only load the previous page */10}11<InfiniteScroll data="messages" onlyPrevious>12 {/* ... */}13</InfiniteScroll>;1415{16 /* Load in both directions (default) */17}18<InfiniteScroll data="posts">{/* ... */}</InfiniteScroll>;
1<!-- Only load the next page -->2<InfiniteScroll data="users" only-next>3 <!-- ... -->4</InfiniteScroll>56<!-- Only load the previous page -->7<InfiniteScroll data="messages" only-previous>8 <!-- ... -->9</InfiniteScroll>1011<!-- Load in both directions (default) -->12<InfiniteScroll data="posts">13 <!-- ... -->14</InfiniteScroll>1<!-- Only load the next page -->2<InfiniteScroll data="users" only-next>3 <!-- ... -->4</InfiniteScroll>56<!-- Only load the previous page -->7<InfiniteScroll data="messages" only-previous>8 <!-- ... -->9</InfiniteScroll>1011<!-- Load in both directions (default) -->12<InfiniteScroll data="posts">13 <!-- ... -->14</InfiniteScroll>
The default option is particularly useful when users start on a middle page and need to scroll in both directions to access all content.
Reverse Mode
For chat applications, timelines, or interfaces where content is sorted descendingly (newest items at the bottom), you can enable reverse mode. This configures the component to load older content when scrolling upward.
1<InfiniteScroll data="messages" reverse>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="messages" reverse>2 <!-- ... -->3</InfiniteScroll>
1<InfiniteScroll data="messages" reverse>2 {/* ... */}3</InfiniteScroll>1<InfiniteScroll data="messages" reverse>2 {/* ... */}3</InfiniteScroll>
1<InfiniteScroll data="messages" reverse>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="messages" reverse>2 <!-- ... -->3</InfiniteScroll>
In reverse mode, the component flips the loading directions so that scrolling up loads the next page (older content) and scrolling down loads the previous page (newer content). The component handles the loading positioning, but you are responsible for reversing your content to display in the correct order.
Reverse mode also enables automatic scrolling to the bottom on initial load, which you can disable with :auto-scroll="false".
1<InfiniteScroll data="messages" reverse :auto-scroll="false">2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="messages" reverse :auto-scroll="false">2 <!-- ... -->3</InfiniteScroll>
1<InfiniteScroll data="messages" reverse autoScroll={false}>2 {/* ... */}3</InfiniteScroll>1<InfiniteScroll data="messages" reverse autoScroll={false}>2 {/* ... */}3</InfiniteScroll>
1<InfiniteScroll data="messages" reverse auto-scroll={false}>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="messages" reverse auto-scroll={false}>2 <!-- ... -->3</InfiniteScroll>
Manual Mode
Manual mode disables automatic loading when scrolling and allows you to control when content loads through the next and previous slots. For more details about available slot properties and customization options, see the Slots documentation.
1<template>2 <InfiniteScroll data="users" manual>3 <template #previous="{ loading, fetch, hasMore }">4 <button v-if="hasMore" @click="fetch" :disabled="loading">5 {{ loading ? "Loading..." : "Load previous" }}6 </button>7 </template>89 <!-- Your content -->1011 <template #next="{ loading, fetch, hasMore }">12 <button v-if="hasMore" @click="fetch" :disabled="loading">13 {{ loading ? "Loading..." : "Load more" }}14 </button>15 </template>16 </InfiniteScroll>17</template>1<template>2 <InfiniteScroll data="users" manual>3 <template #previous="{ loading, fetch, hasMore }">4 <button v-if="hasMore" @click="fetch" :disabled="loading">5 {{ loading ? "Loading..." : "Load previous" }}6 </button>7 </template>89 <!-- Your content -->1011 <template #next="{ loading, fetch, hasMore }">12 <button v-if="hasMore" @click="fetch" :disabled="loading">13 {{ loading ? "Loading..." : "Load more" }}14 </button>15 </template>16 </InfiniteScroll>17</template>
1import { InfiniteScroll } from "@inertiajs/react";23export default ({ users }) => (4 <InfiniteScroll5 data="users"6 manual7 previous={({ loading, fetch, hasMore }) =>8 hasMore && (9 <button onClick={fetch} disabled={loading}>10 {loading ? "Loading..." : "Load previous"}11 </button>12 )13 }14 next={({ loading, fetch, hasMore }) =>15 hasMore && (16 <button onClick={fetch} disabled={loading}>17 {loading ? "Loading..." : "Load more"}18 </button>19 )20 }21 >22 {users.data.map((user) => (23 <div key={user.id}>{user.name}</div>24 ))}25 </InfiniteScroll>26);1import { InfiniteScroll } from "@inertiajs/react";23export default ({ users }) => (4 <InfiniteScroll5 data="users"6 manual7 previous={({ loading, fetch, hasMore }) =>8 hasMore && (9 <button onClick={fetch} disabled={loading}>10 {loading ? "Loading..." : "Load previous"}11 </button>12 )13 }14 next={({ loading, fetch, hasMore }) =>15 hasMore && (16 <button onClick={fetch} disabled={loading}>17 {loading ? "Loading..." : "Load more"}18 </button>19 )20 }21 >22 {users.data.map((user) => (23 <div key={user.id}>{user.name}</div>24 ))}25 </InfiniteScroll>26);
1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()4</script>56<InfiniteScroll data="users" manual>7 {#snippet previous({ hasMore, fetch, loading })}8 {#if hasMore}9 <button onclick={fetch} disabled={loading}>10 {loading ? 'Loading...' : 'Load previous'}11 </button>12 {/if}13 {/snippet}1415 {#each users.data as user (user.id)}16 <div>{user.name}</div>17 {/each}1819 {#snippet next({ hasMore, fetch, loading })}20 {#if hasMore}21 <button onclick={fetch} disabled={loading}>22 {loading ? 'Loading...' : 'Load more'}23 </button>24 {/if}25 {/snippet}26</InfiniteScroll>1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()4</script>56<InfiniteScroll data="users" manual>7 {#snippet previous({ hasMore, fetch, loading })}8 {#if hasMore}9 <button onclick={fetch} disabled={loading}>10 {loading ? 'Loading...' : 'Load previous'}11 </button>12 {/if}13 {/snippet}1415 {#each users.data as user (user.id)}16 <div>{user.name}</div>17 {/each}1819 {#snippet next({ hasMore, fetch, loading })}20 {#if hasMore}21 <button onclick={fetch} disabled={loading}>22 {loading ? 'Loading...' : 'Load more'}23 </button>24 {/if}25 {/snippet}26</InfiniteScroll>
You can also configure the component to automatically switch to manual mode after a certain number of pages using the manualAfter prop.
1<InfiniteScroll data="users" :manual-after="3">2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="users" :manual-after="3">2 <!-- ... -->3</InfiniteScroll>
1<InfiniteScroll data="users" manualAfter={3}>2 {/* ... */}3</InfiniteScroll>1<InfiniteScroll data="users" manualAfter={3}>2 {/* ... */}3</InfiniteScroll>
1<InfiniteScroll data="users" manual-after={3}>2 <!-- ... -->3</InfiniteScroll>1<InfiniteScroll data="users" manual-after={3}>2 <!-- ... -->3</InfiniteScroll>
Slots
The infinite scroll component provides several slots to customize the loading experience. These slots allow you to display custom loading indicators and create manual load controls. Each slot receives properties that provide loading state information and functions to trigger content loading.
Default Slot
The main content area where you render your data items. This slot receives loading state information.
1<InfiniteScroll2 data="users"3 #default="{ loading, loadingPrevious, loadingNext }"4>5 <!-- Your content with access to loading states -->6</InfiniteScroll>1<InfiniteScroll2 data="users"3 #default="{ loading, loadingPrevious, loadingNext }"4>5 <!-- Your content with access to loading states -->6</InfiniteScroll>
1<InfiniteScroll data="users">2 {({ loading, loadingPrevious, loadingNext }) => (3 <div>{/* Your content with access to loading states */}</div>4 )}5</InfiniteScroll>1<InfiniteScroll data="users">2 {({ loading, loadingPrevious, loadingNext }) => (3 <div>{/* Your content with access to loading states */}</div>4 )}5</InfiniteScroll>
1<InfiniteScroll data="users">2 {#snippet children({ loading, loadingPrevious, loadingNext })}3 <!-- Your content with access to loading states -->4 {/snippet}5</InfiniteScroll>1<InfiniteScroll data="users">2 {#snippet children({ loading, loadingPrevious, loadingNext })}3 <!-- Your content with access to loading states -->4 {/snippet}5</InfiniteScroll>
Loading Slot
The loading slot is used as a fallback when loading content and no custom before or after slots are provided. This creates a default loading indicator.
1<template>2 <InfiniteScroll data="users">3 <!-- Your content -->45 <template #loading> Loading more users... </template>6 </InfiniteScroll>7</template>1<template>2 <InfiniteScroll data="users">3 <!-- Your content -->45 <template #loading> Loading more users... </template>6 </InfiniteScroll>7</template>
1<InfiniteScroll data="users" loading={() => "Loading more users..."}>2 {/* Your content */}3</InfiniteScroll>1<InfiniteScroll data="users" loading={() => "Loading more users..."}>2 {/* Your content */}3</InfiniteScroll>
1<InfiniteScroll data="users">2 <!-- Your content -->34 {#snippet loading()}5 Loading more users...6 {/snippet}7</InfiniteScroll>1<InfiniteScroll data="users">2 <!-- Your content -->34 {#snippet loading()}5 Loading more users...6 {/snippet}7</InfiniteScroll>
Previous and Next Slots
The previous and next slots are rendered above and below the main content, typically used for manual load controls. These slots receive several properties including loading states, fetch functions, and mode indicators.
1<template>2 <InfiniteScroll data="users" :manual-after="3">3 <template #previous="{ loading, fetch, hasMore, manualMode }">4 <button v-if="manualMode && hasMore" @click="fetch" :disabled="loading">5 {{ loading ? "Loading..." : "Load previous" }}6 </button>7 </template>89 <!-- Your content -->1011 <template #next="{ loading, fetch, hasMore, manualMode }">12 <button v-if="manualMode && hasMore" @click="fetch" :disabled="loading">13 {{ loading ? "Loading..." : "Load more" }}14 </button>15 </template>16 </InfiniteScroll>17</template>1<template>2 <InfiniteScroll data="users" :manual-after="3">3 <template #previous="{ loading, fetch, hasMore, manualMode }">4 <button v-if="manualMode && hasMore" @click="fetch" :disabled="loading">5 {{ loading ? "Loading..." : "Load previous" }}6 </button>7 </template>89 <!-- Your content -->1011 <template #next="{ loading, fetch, hasMore, manualMode }">12 <button v-if="manualMode && hasMore" @click="fetch" :disabled="loading">13 {{ loading ? "Loading..." : "Load more" }}14 </button>15 </template>16 </InfiniteScroll>17</template>
The loading, previous, and next slots receive the following properties:
| Property | Description |
|---|---|
loading |
Whether the slot is currently loading content |
loadingPrevious |
Whether previous content is loading |
loadingNext |
Whether next content is loading |
fetch |
Function to trigger loading for the slot |
hasMore |
Whether more content is available for the slot |
hasPrevious |
Whether more previous content is available |
hasNext |
Whether more next content is available |
manualMode |
Whether manual mode is active |
autoMode |
Whether automatic loading is active |
Custom Element
The InfiniteScroll component renders as a <div> element. You may customize this to use any HTML element using the as prop.
1<template>2 <InfiniteScroll data="products" as="ul">3 <li v-for="product in products.data" :key="product.id">4 {{ product.name }}5 </li>6 </InfiniteScroll>7</template>1<template>2 <InfiniteScroll data="products" as="ul">3 <li v-for="product in products.data" :key="product.id">4 {{ product.name }}5 </li>6 </InfiniteScroll>7</template>
1<InfiniteScroll data="products" as="ul">2 {products.data.map((product) => (3 <li key={product.id}>{product.name}</li>4 ))}5</InfiniteScroll>1<InfiniteScroll data="products" as="ul">2 {products.data.map((product) => (3 <li key={product.id}>{product.name}</li>4 ))}5</InfiniteScroll>
1<InfiniteScroll data="products" as="ul">2 {#each products.data as product (product.id)}3 <li>{product.name}</li>4 {/each}5</InfiniteScroll>1<InfiniteScroll data="products" as="ul">2 {#each products.data as product (product.id)}3 <li>{product.name}</li>4 {/each}5</InfiniteScroll>
Element Targeting
The infinite scroll component automatically tracks content and assigns page numbers to elements for URL synchronization. When your data items are not direct children of the component's root element, you need to specify which element contains the actual data items using the itemsElement prop.
1<template>2 <InfiniteScroll data="users" items-element="#table-body">3 <table>4 <thead>5 <tr>6 <th>Name</th>7 </tr>8 </thead>9 <tbody id="table-body">10 <tr v-for="user in users.data" :key="user.id">11 <td>{{ user.name }}</td>12 </tr>13 </tbody>14 </table>15 </InfiniteScroll>16</template>1<template>2 <InfiniteScroll data="users" items-element="#table-body">3 <table>4 <thead>5 <tr>6 <th>Name</th>7 </tr>8 </thead>9 <tbody id="table-body">10 <tr v-for="user in users.data" :key="user.id">11 <td>{{ user.name }}</td>12 </tr>13 </tbody>14 </table>15 </InfiniteScroll>16</template>
1<InfiniteScroll data="users" itemsElement="#table-body">2 <table>3 <thead>4 <tr>5 <th>Name</th>6 </tr>7 </thead>8 <tbody id="table-body">9 {users.data.map((user) => (10 <tr key="{user.id}">11 <td>{user.name}</td>12 </tr>13 ))}14 </tbody>15 </table>16</InfiniteScroll>1<InfiniteScroll data="users" itemsElement="#table-body">2 <table>3 <thead>4 <tr>5 <th>Name</th>6 </tr>7 </thead>8 <tbody id="table-body">9 {users.data.map((user) => (10 <tr key="{user.id}">11 <td>{user.name}</td>12 </tr>13 ))}14 </tbody>15 </table>16</InfiniteScroll>
1<InfiniteScroll data="users" items-element="#table-body">2 <table>3 <thead>4 <tr><th>Name</th></tr>5 </thead>6 <tbody id="table-body">7 {#each users.data as user (user.id)}8 <tr>9 <td>{user.name}</td>10 </tr>11 {/each}12 </tbody>13 </table>14</InfiniteScroll>1<InfiniteScroll data="users" items-element="#table-body">2 <table>3 <thead>4 <tr><th>Name</th></tr>5 </thead>6 <tbody id="table-body">7 {#each users.data as user (user.id)}8 <tr>9 <td>{user.name}</td>10 </tr>11 {/each}12 </tbody>13 </table>14</InfiniteScroll>
In this example, the component monitors the #table-body element and automatically tags each <tr> with a page number as new content loads. This enables proper URL updates based on which page's content is most visible in the viewport.
You can also specify custom trigger elements for loading more content using CSS selectors. This prevents the default trigger elements from being rendered and uses intersection observers on your custom elements instead.
1<template>2 <InfiniteScroll3 data="users"4 items-element="#table-body"5 start-element="#table-header"6 end-element="#table-footer"7 >8 <table>9 <thead id="table-header">10 <tr>11 <th>Name</th>12 </tr>13 </thead>14 <tbody id="table-body">15 <tr v-for="user in users.data" :key="user.id">16 <td>{{ user.name }}</td>17 </tr>18 </tbody>19 <tfoot id="table-footer">20 <tr>21 <td>Footer</td>22 </tr>23 </tfoot>24 </table>25 </InfiniteScroll>26</template>1<template>2 <InfiniteScroll3 data="users"4 items-element="#table-body"5 start-element="#table-header"6 end-element="#table-footer"7 >8 <table>9 <thead id="table-header">10 <tr>11 <th>Name</th>12 </tr>13 </thead>14 <tbody id="table-body">15 <tr v-for="user in users.data" :key="user.id">16 <td>{{ user.name }}</td>17 </tr>18 </tbody>19 <tfoot id="table-footer">20 <tr>21 <td>Footer</td>22 </tr>23 </tfoot>24 </table>25 </InfiniteScroll>26</template>
1<InfiniteScroll2 data="users"3 itemsElement="#table-body"4 startElement="#table-header"5 endElement="#table-footer"6>7 <table>8 <thead id="table-header">9 <tr>10 <th>Name</th>11 </tr>12 </thead>13 <tbody id="table-body">14 {users.data.map((user) => (15 <tr key={user.id}>16 <td>{user.name}</td>17 </tr>18 ))}19 </tbody>20 <tfoot id="table-footer">21 <tr>22 <td>Footer</td>23 </tr>24 </tfoot>25 </table>26</InfiniteScroll>1<InfiniteScroll2 data="users"3 itemsElement="#table-body"4 startElement="#table-header"5 endElement="#table-footer"6>7 <table>8 <thead id="table-header">9 <tr>10 <th>Name</th>11 </tr>12 </thead>13 <tbody id="table-body">14 {users.data.map((user) => (15 <tr key={user.id}>16 <td>{user.name}</td>17 </tr>18 ))}19 </tbody>20 <tfoot id="table-footer">21 <tr>22 <td>Footer</td>23 </tr>24 </tfoot>25 </table>26</InfiniteScroll>
1<InfiniteScroll2 data="users"3 items-element="#table-body"4 start-element="#table-header"5 end-element="#table-footer"6>7 <table>8 <thead id="table-header">9 <tr><th>Name</th></tr>10 </thead>11 <tbody id="table-body">12 {#each users.data as user (user.id)}13 <tr>14 <td>{user.name}</td>15 </tr>16 {/each}17 </tbody>18 <tfoot id="table-footer">19 <tr><td>Footer</td></tr>20 </tfoot>21 </table>22</InfiniteScroll>1<InfiniteScroll2 data="users"3 items-element="#table-body"4 start-element="#table-header"5 end-element="#table-footer"6>7 <table>8 <thead id="table-header">9 <tr><th>Name</th></tr>10 </thead>11 <tbody id="table-body">12 {#each users.data as user (user.id)}13 <tr>14 <td>{user.name}</td>15 </tr>16 {/each}17 </tbody>18 <tfoot id="table-footer">19 <tr><td>Footer</td></tr>20 </tfoot>21 </table>22</InfiniteScroll>
Alternatively, you can use template refs instead of CSS selectors. This avoids adding HTML attributes and provides direct element references.
1<script setup>2import { ref } from "vue";3const tableHeader = ref();4const tableFooter = ref();5const tableBody = ref();6</script>78<template>9 <InfiniteScroll10 data="users"11 :items-element="() => tableBody"12 :start-element="() => tableHeader"13 :end-element="() => tableFooter"14 >15 <table>16 <thead ref="tableHeader">17 <tr>18 <th>Name</th>19 </tr>20 </thead>21 <tbody ref="tableBody">22 <tr v-for="user in users.data" :key="user.id">23 <td>{{ user.name }}</td>24 </tr>25 </tbody>26 <tfoot ref="tableFooter">27 <tr>28 <td>Footer</td>29 </tr>30 </tfoot>31 </table>32 </InfiniteScroll>33</template>1<script setup>2import { ref } from "vue";3const tableHeader = ref();4const tableFooter = ref();5const tableBody = ref();6</script>78<template>9 <InfiniteScroll10 data="users"11 :items-element="() => tableBody"12 :start-element="() => tableHeader"13 :end-element="() => tableFooter"14 >15 <table>16 <thead ref="tableHeader">17 <tr>18 <th>Name</th>19 </tr>20 </thead>21 <tbody ref="tableBody">22 <tr v-for="user in users.data" :key="user.id">23 <td>{{ user.name }}</td>24 </tr>25 </tbody>26 <tfoot ref="tableFooter">27 <tr>28 <td>Footer</td>29 </tr>30 </tfoot>31 </table>32 </InfiniteScroll>33</template>
1import { useRef } from "react";23export default ({ users }) => {4 const tableHeader = useRef();5 const tableFooter = useRef();6 const tableBody = useRef();78 return (9 <InfiniteScroll10 data="users"11 itemsElement={tableBody}12 startElement={tableHeader}13 endElement={tableFooter}14 >15 <table>16 <thead ref={tableHeader}>17 <tr>18 <th>Name</th>19 </tr>20 </thead>21 <tbody ref={tableBody}>22 {users.data.map((user) => (23 <tr key={user.id}>24 <td>{user.name}</td>25 </tr>26 ))}27 </tbody>28 <tfoot ref={tableFooter}>29 <tr>30 <td>Footer</td>31 </tr>32 </tfoot>33 </table>34 </InfiniteScroll>35 );36};1import { useRef } from "react";23export default ({ users }) => {4 const tableHeader = useRef();5 const tableFooter = useRef();6 const tableBody = useRef();78 return (9 <InfiniteScroll10 data="users"11 itemsElement={tableBody}12 startElement={tableHeader}13 endElement={tableFooter}14 >15 <table>16 <thead ref={tableHeader}>17 <tr>18 <th>Name</th>19 </tr>20 </thead>21 <tbody ref={tableBody}>22 {users.data.map((user) => (23 <tr key={user.id}>24 <td>{user.name}</td>25 </tr>26 ))}27 </tbody>28 <tfoot ref={tableFooter}>29 <tr>30 <td>Footer</td>31 </tr>32 </tfoot>33 </table>34 </InfiniteScroll>35 );36};
1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()45 let tableHeader6 let tableFooter7 let tableBody8</script>910<InfiniteScroll11 data="users"12 items-element={() => tableBody}13 start-element={() => tableHeader}14 end-element={() => tableFooter}15>16 <table>17 <thead bind:this={tableHeader}>18 <tr><th>Name</th></tr>19 </thead>20 <tbody bind:this={tableBody}>21 {#each users.data as user (user.id)}22 <tr>23 <td>{user.name}</td>24 </tr>25 {/each}26 </tbody>27 <tfoot bind:this={tableFooter}>28 <tr><td>Footer</td></tr>29 </tfoot>30 </table>31</InfiniteScroll>1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()45 let tableHeader6 let tableFooter7 let tableBody8</script>910<InfiniteScroll11 data="users"12 items-element={() => tableBody}13 start-element={() => tableHeader}14 end-element={() => tableFooter}15>16 <table>17 <thead bind:this={tableHeader}>18 <tr><th>Name</th></tr>19 </thead>20 <tbody bind:this={tableBody}>21 {#each users.data as user (user.id)}22 <tr>23 <td>{user.name}</td>24 </tr>25 {/each}26 </tbody>27 <tfoot bind:this={tableFooter}>28 <tr><td>Footer</td></tr>29 </tfoot>30 </table>31</InfiniteScroll>
Scroll Containers
The infinite scroll component works within any scrollable container, not just the main document. The component automatically adapts to use the custom scroll container for trigger detection and calculations instead of the main document scroll.
1<template>2 <div style="height: 400px; overflow-y: auto;">3 <InfiniteScroll data="users">4 <div v-for="user in users.data" :key="user.id">5 {{ user.name }}6 </div>7 </InfiniteScroll>8 </div>9</template>1<template>2 <div style="height: 400px; overflow-y: auto;">3 <InfiniteScroll data="users">4 <div v-for="user in users.data" :key="user.id">5 {{ user.name }}6 </div>7 </InfiniteScroll>8 </div>9</template>
1<div style={{ height: "400px", overflowY: "auto" }}>2 <InfiniteScroll data="users">3 {users.data.map((user) => (4 <div key={user.id}>{user.name}</div>5 ))}6 </InfiniteScroll>7</div>1<div style={{ height: "400px", overflowY: "auto" }}>2 <InfiniteScroll data="users">3 {users.data.map((user) => (4 <div key={user.id}>{user.name}</div>5 ))}6 </InfiniteScroll>7</div>
1<div style="height: 400px; overflow-y: auto;">2 <InfiniteScroll data="users">3 {#each users.data as user (user.id)}4 <div>{user.name}</div>5 {/each}6 </InfiniteScroll>7</div>1<div style="height: 400px; overflow-y: auto;">2 <InfiniteScroll data="users">3 {#each users.data as user (user.id)}4 <div>{user.name}</div>5 {/each}6 </InfiniteScroll>7</div>
Multiple Scroll Containers
Sometimes you may need to render multiple infinite scroll components on a single page. However, if both components use the default page query parameter for URL synchronization, they will conflict with each other. To resolve this, instruct each paginator to use a custom page name.
1Route::get('/dashboard', function () {2 return Inertia::render('Dashboard', [3 'users' => Inertia::scroll(4 fn() => User::paginate(pageName: 'users')5 ),6 'orders' => Inertia::scroll(7 fn() => Order::paginate(pageName: 'orders')8 ),9 ]);10});1Route::get('/dashboard', function () {2 return Inertia::render('Dashboard', [3 'users' => Inertia::scroll(4 fn() => User::paginate(pageName: 'users')5 ),6 'orders' => Inertia::scroll(7 fn() => Order::paginate(pageName: 'orders')8 ),9 ]);10});
1use laravel::inertia::{self, Page, props};23use crate::models::{Order, User};45pub async fn dashboard() -> Page {6 inertia::render("Dashboard", props! {7 users => inertia::scroll_with(async || {8 User::query().order_by(User::id).paginate(15).page_name("users").await9 }),10 orders => inertia::scroll_with(async || {11 Order::query().order_by(Order::id).paginate(15).page_name("orders").await12 }),13 })14}1use laravel::inertia::{self, Page, props};23use crate::models::{Order, User};45pub async fn dashboard() -> Page {6 inertia::render("Dashboard", props! {7 users => inertia::scroll_with(async || {8 User::query().order_by(User::id).paginate(15).page_name("users").await9 }),10 orders => inertia::scroll_with(async || {11 Order::query().order_by(Order::id).paginate(15).page_name("orders").await12 }),13 })14}
Inertia automatically detects the custom page name from each paginator, allowing both scroll containers to maintain independent pagination state. This results in URLs like ?users=2&orders=3 instead of conflicting ?page= parameters.
For more information about pagination page names, see Laravel's documentation.
Programmatic Access
When you need to trigger loading actions programmatically, you may use a template ref.
1<script setup>2import { ref } from "vue";3const infiniteScrollRef = ref(null);45const fetchNext = () => {6 infiniteScrollRef.value?.fetchNext();7};8</script>910<template>11 <button @click="fetchNext">Load More</button>1213 <InfiniteScroll ref="infiniteScrollRef" data="users" manual>14 <!-- Your content -->15 </InfiniteScroll>16</template>1<script setup>2import { ref } from "vue";3const infiniteScrollRef = ref(null);45const fetchNext = () => {6 infiniteScrollRef.value?.fetchNext();7};8</script>910<template>11 <button @click="fetchNext">Load More</button>1213 <InfiniteScroll ref="infiniteScrollRef" data="users" manual>14 <!-- Your content -->15 </InfiniteScroll>16</template>
1import { InfiniteScroll } from '@inertiajs/react'2import { useRef } from 'react'34export default ({ users }) => {5 const infiniteScrollRef = useRef(null)67 const fetchNext = () => {8 infiniteScrollRef.current?.fetchNext()9 }1011 return (1213 <button onClick={fetchNext}>Load More</button>1415 <InfiniteScroll ref={infiniteScrollRef} data="users" manual>16 {users.data.map(user => (17 <div key={user.id}>{user.name}</div>18 ))}19 </InfiniteScroll>2021 )22}1import { InfiniteScroll } from '@inertiajs/react'2import { useRef } from 'react'34export default ({ users }) => {5 const infiniteScrollRef = useRef(null)67 const fetchNext = () => {8 infiniteScrollRef.current?.fetchNext()9 }1011 return (1213 <button onClick={fetchNext}>Load More</button>1415 <InfiniteScroll ref={infiniteScrollRef} data="users" manual>16 {users.data.map(user => (17 <div key={user.id}>{user.name}</div>18 ))}19 </InfiniteScroll>2021 )22}
1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()45 let infiniteScrollRef67 const fetchNext = () => {8 infiniteScrollRef?.fetchNext()9 }10</script>1112<button onclick={fetchNext}>Load More</button>1314<InfiniteScroll bind:this={infiniteScrollRef} data="users" manual>15 {#each users.data as user (user.id)}16 <div>{user.name}</div>17 {/each}18</InfiniteScroll>1<script>2 import { InfiniteScroll } from '@inertiajs/svelte'3 let { users } = $props()45 let infiniteScrollRef67 const fetchNext = () => {8 infiniteScrollRef?.fetchNext()9 }10</script>1112<button onclick={fetchNext}>Load More</button>1314<InfiniteScroll bind:this={infiniteScrollRef} data="users" manual>15 {#each users.data as user (user.id)}16 <div>{user.name}</div>17 {/each}18</InfiniteScroll>
The component exposes the following methods:
fetchNext()- Manually fetch the next pagefetchPrevious()- Manually fetch the previous pagehasNext()- Whether there is a next pagehasPrevious()- Whether there is a previous page
Inertia::scroll() Method
Inertia::scroll() providesinertia::scroll() and inertia::scroll_with() provide server-side configuration for infinite scrolling, automatically configuring the proper merge behavior so that new data is appended or prepended to existing content instead of replacing it and normalizing pagination metadata for the front-end component.
1// Works with all Laravel pagination methods...2Inertia::scroll(User::paginate(20));3Inertia::scroll(User::simplePaginate(20));4Inertia::scroll(User::cursorPaginate(20));56// Works with API resources...7Inertia::scroll(UserResource::collection(User::paginate(20)));1// Works with all Laravel pagination methods...2Inertia::scroll(User::paginate(20));3Inertia::scroll(User::simplePaginate(20));4Inertia::scroll(User::cursorPaginate(20));56// Works with API resources...7Inertia::scroll(UserResource::collection(User::paginate(20)));
1// Lazily evaluate the paginator (recommended when the page has other props)...2inertia::scroll_with(async || User::query().order_by(User::id).paginate(20).await);3inertia::scroll_with(async || User::query().order_by(User::id).simple_paginate(20).await);4inertia::scroll_with(async || User::query().order_by(User::id).cursor_paginate(20).await);56// Or wrap an already-fetched paginator...7let users = User::query().order_by(User::id).paginate(20).await?;8inertia::scroll(users);1// Lazily evaluate the paginator (recommended when the page has other props)...2inertia::scroll_with(async || User::query().order_by(User::id).paginate(20).await);3inertia::scroll_with(async || User::query().order_by(User::id).simple_paginate(20).await);4inertia::scroll_with(async || User::query().order_by(User::id).cursor_paginate(20).await);56// Or wrap an already-fetched paginator...7let users = User::query().order_by(User::id).paginate(20).await?;8inertia::scroll(users);
If you don't use Laravel's built-in paginators or use a different wrapper key, you may customize the wrapper key (data by default) and provide custom pagination metadata.
1// Customize the data wrapper key (defaults to 'data')...2Inertia::scroll($customPaginatedData, wrapper: 'items');34// Provide custom metadata resolution...5Inertia::scroll($data, metadata: $metadataProvider);1// Customize the data wrapper key (defaults to 'data')...2Inertia::scroll($customPaginatedData, wrapper: 'items');34// Provide custom metadata resolution...5Inertia::scroll($data, metadata: $metadataProvider);
1// Customize the data wrapper key (defaults to "data")...2inertia::scroll(custom_paginated_data).wrapper("items");1// Customize the data wrapper key (defaults to "data")...2inertia::scroll(custom_paginated_data).wrapper("items");
To integrate a custom pagination type, implement ProvidesScrollMetadata. You may pass an instance or callback to the metadata argument.Implement the ProvidesScrollMetadata trait on your paginated type so inertia::scroll() and inertia::scroll_with() can read its metadata directly.
1use League\Fractal\Resource\Collection;23class FractalScrollMetadata implements ProvidesScrollMetadata4{5 public function __construct(protected Collection $resource) {}6 public function getPageName(): string {}7 public function getPreviousPage(): int|string|null {}8 public function getNextPage(): int|string|null {}9 public function getCurrentPage(): int|string|null {}10}1use League\Fractal\Resource\Collection;23class FractalScrollMetadata implements ProvidesScrollMetadata4{5 public function __construct(protected Collection $resource) {}6 public function getPageName(): string {}7 public function getPreviousPage(): int|string|null {}8 public function getNextPage(): int|string|null {}9 public function getCurrentPage(): int|string|null {}10}
1use laravel::inertia::{ProvidesScrollMetadata, ScrollMetadata};2use serde::Serialize;34#[derive(Serialize)]5pub struct CustomPage<T> {6 pub items: Vec<T>,7 pub current_page: u64,8 pub last_page: u64,9}1011impl<T> ProvidesScrollMetadata for CustomPage<T> {12 fn scroll_metadata(&self) -> ScrollMetadata {13 ScrollMetadata::pages("page", self.current_page, self.last_page)14 }15}1use laravel::inertia::{ProvidesScrollMetadata, ScrollMetadata};2use serde::Serialize;34#[derive(Serialize)]5pub struct CustomPage<T> {6 pub items: Vec<T>,7 pub current_page: u64,8 pub last_page: u64,9}1011impl<T> ProvidesScrollMetadata for CustomPage<T> {12 fn scroll_metadata(&self) -> ScrollMetadata {13 ScrollMetadata::pages("page", self.current_page, self.last_page)14 }15}
You may then pass your custom paginated data to scroll():
1// Using an instance directly2Inertia::scroll($data, metadata: new FractalScrollMetadata($data));34// Using a callback5Inertia::scroll(6 fn() => $this->transformData($data),7 metadata: fn($data) => new FractalScrollMetadata($data)8);1// Using an instance directly2Inertia::scroll($data, metadata: new FractalScrollMetadata($data));34// Using a callback5Inertia::scroll(6 fn() => $this->transformData($data),7 metadata: fn($data) => new FractalScrollMetadata($data)8);
1inertia::render("Users/Index", props! {2 users => inertia::scroll(custom_page).wrapper("items"),3})1inertia::render("Users/Index", props! {2 users => inertia::scroll(custom_page).wrapper("items"),3})
To avoid repeating this setup in multiple controllers, you may define a macro:
1// In your AppServiceProvider's boot method2Inertia::macro('fractalScroll', function (Collection $data) {3 return Inertia::scroll(4 $data,5 metadata: fn(Collection $data) => new FractalScrollMetadata($data)6 );7});89// Then use it in your controllers10return Inertia::render('Users/Index', [11 'users' => Inertia::fractalScroll($fractalCollection)12]);1// In your AppServiceProvider's boot method2Inertia::macro('fractalScroll', function (Collection $data) {3 return Inertia::scroll(4 $data,5 metadata: fn(Collection $data) => new FractalScrollMetadata($data)6 );7});89// Then use it in your controllers10return Inertia::render('Users/Index', [11 'users' => Inertia::fractalScroll($fractalCollection)12]);