DEMOS / Turbo

Turbo Infinite Scroll - 2/2

Building on Part One, this version loads the next batch when a lazy Turbo Frame enters the scrollable catalog; compare it with the Live Component version, which uses a Live Component to load on scroll.

ux.symfony.com
🐻
1.99
🐨
2.99
🐼
3.99
🦥
4.99
🦦
5.99
🦨
6.99
🦘
7.99
🐾
8.99
🐓
9.99
🐣
10.99
🐤
11.99
🐥
12.99

So... how does it work?

Keep scrolling inside the demo. Each batch moves the next loading point farther down the grid.

Load when the frame comes into view

In Part One, you click a link to load another batch. Here, an empty Turbo Frame sits just after the grid and points to the next page through src.

With loading="lazy", Turbo waits until the frame comes into view inside the scrollable catalog before requesting that page.

70{% macro pagination(has_more, next_page) %}
71    {% if has_more %}
72        <twig:Turbo:Frame
73            id="product-grid-pagination"
74            src="{{ path('app_demo_turbo_infinite_scroll_2', {
75                page: next_page,
76                _format: 'turbo_stream',
77            }) }}"
78            loading="lazy"
79        />
80    {% else %}
81        <span class="code">The End</span>
82    {% endif %}
83{% endmacro %}

Append cards and replace the frame

The response appends the cards to #product-grid, then replaces #product-grid-pagination with a frame whose src points to the following page.

The new frame moves down as the grid grows and triggers the next request when it comes into view. On the last batch, The End replaces the frame, so there’s no next request.

58{% block stream_success %}
59    <twig:Turbo:Stream:Append target="#product-grid">
60        {% for item in items %}
61            <twig:ProductGridItem :item="item" />
62        {% endfor %}
63    </twig:Turbo:Stream:Append>
64    <twig:Turbo:Stream:Replace target="#product-grid-pagination">
65        {% from 'demos/turbo/infinite_scroll_2.html.twig' import pagination %}
66        {{ pagination(has_more, next_page) }}
67    </twig:Turbo:Stream:Replace>
68{% endblock %}

Return the stream response

The route defaults to HTML for a normal visit. The frame’s src explicitly requests the .turbo_stream format, so both behaviors can share one controller action.

The response uses text/vnd.turbo-stream.html. Turbo applies its actions to the grid and the pagination frame by selector.

71#[Route('/infinite-scroll-2.{_format}', name: 'app_demo_turbo_infinite_scroll_2', requirements: ['_format' => 'html|turbo_stream'], format: 'html')]
72public function infiniteScroll2(
73    Request $request,
74    #[MapQueryParameter(filter: \FILTER_VALIDATE_INT, options: ['min_range' => 1])] int $page = 1,
75): Response {
76    $items = $this->getItems($page);
77
78    $hasMore = \count($this->emojiCollection) > ($page * self::PER_PAGE);
79    $nextPage = $hasMore ? $page + 1 : null;
80
81    if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
82        $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
83
84        return $this->renderBlock('demos/turbo/infinite_scroll_2.html.twig', 'stream_success', [
85            'items' => $items,
86            'has_more' => $hasMore,
87            'next_page' => $nextPage,
88        ]);
89    }
90
91    return $this->render('demos/turbo/infinite_scroll_2.html.twig', [
92        'demo' => $this->turboDemoRepository->find('infinite-scroll-2'),
93        'relatedLiveDemo' => $this->liveDemoRepository->find('infinite-scroll-2'),
94        'items' => $items,
95        'has_more' => $hasMore,
96        'next_page' => $nextPage,
97    ]);
98}
Author ker0x
Published 2026-04-20