How-to
Infinite scroll
A list that loads its next part as the visitor scrolls: one link after the list, which zobacz follows by itself, and a real page behind every part.
Why one way
A long list (products, posts, a page that runs on into the next) should keep going as the visitor scrolls, and every part of it should still be a page that a search engine can read and a link can open. Every zobacz app does this the same way, so each list behaves the same for visitors, assistive technology and crawlers, and nobody writes their own scroll code.
The server decides what comes next. The browser only follows the link the server drew, so the list works without script, and a part that fails to load leaves a link the visitor can still follow.
How it works
- The server draws the first part of the list in an element with an id and data-z-keep, and after it a link to the next part: rel="next" and data-z-more set to that id.
- When the link comes within 1200 pixels of the viewport, or when it is clicked, zobacz fetches the link's page.
- It appends the children of that page's element with the same id to this list, then gives the link that page's own next link, or removes it when there is none.
- Components in the new part resume like any other: the runtime sees them arrive.
<div id="posts" data-z-keep>
{posts.map((p) => <PostCard key={p.id} post={p} />)}
</div>
{next ? <a href={`/posts?page=${next}`} rel="next" data-z-more="posts">More posts</a> : null}Rules
- Use the same id for the list on every part, so each part finds the next one's.
- Put data-z-keep on the list. A component that re-renders around it then leaves the appended parts alone.
- Make every part a real page: /posts?page=2 draws page 2 with its own next link. That page is what a visitor without script, a crawler and a shared link get.
- Give the link words that say what it loads, such as More posts or Next: Hero. It is visible, and it is what a keyboard user presses.
- Keep a part to what one request draws quickly: a page of products or one page's sections.
The runtime is on every page that has a component. Only the list's children move: the fetched page's scripts never run, and of its head only stylesheet links this page lacks are added. While a part loads, the list is aria-busy.
In a store: Continue with
A store page in the CMS has a Continue with field: another page of the store that loads below it as the visitor scrolls, and the page that one continues with after it. The Storefront Playground uses it so that scrolling down from its home page runs through every section and every variation.
Testing
appendMore (zobacz/src/runtime/more.mjs) does the moving and is tested against a fake DOM: the next part is appended in order, the link moves on or goes away, and a fetched page without the list changes nothing. Test your own list by requesting its pages: page 1 draws the link to page 2, and the last page draws none.