Paginate without losing your place
Show the results three at a time, with “Previous” and “Next” links that keep the search and the order, so every page of results has its own address.
Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder with the dev server running.
Read
Start at L10. Search and order live in the address. With more records, one long list gets hard to read, so the list is split into pages of results.
fetch.pages.page_size sets how many records a page holds, and fetch.pages.page reads which page to show from the address, as ?page=2. The search and the order apply first: the count and the pages come from the same matching records, so no record is missing or shown twice. The page also gets #pages_page#, a description of where you are: number, pages, total, and prev or next when that page exists.
Some addresses cannot be answered. ?page=abc, ?page=0 or ?page=-1 are not page numbers: “400 Bad Request”. A page past the last one, such as ?page=9 when there are three, gets the “not found” page with a real 404. When nothing matches, page 1 still exists and shows the empty message.
Change
1. Add more records. Replace data/pages.json. The first three records are unchanged; five more make three pages of three. Two share the title “Weekly notes” on purpose.
[
{
"id": 1,
"title": "Why I started this website",
"slug": "why-i-started",
"summary": "A short note on what this site is for.",
"body": "I wanted one small place on the web that is mine. This page explains why."
},
{
"id": 2,
"title": "What I am learning",
"slug": "what-i-am-learning",
"summary": "Notes from building pages by hand.",
"body": "Every lesson adds one idea. This one is data: content that lives in a file instead of a page."
},
{
"id": 3,
"title": "Tools & ideas for later",
"slug": "tools-and-ideas",
"summary": "A short list of things to try next.",
"body": "Cards, a table and one page per entry, all from the same file."
},
{
"id": 4,
"title": "Weekly notes",
"slug": "weekly-notes-1",
"summary": "What went well this week.",
"body": "A first round of notes: small wins, one surprise and a plan for next week."
},
{
"id": 5,
"title": "A reading list",
"slug": "reading-list",
"summary": "Books and articles worth a second look.",
"body": "A short list I keep coming back to, with one line on why each is here."
},
{
"id": 6,
"title": "Weekly notes",
"slug": "weekly-notes-2",
"summary": "What I would change next time.",
"body": "The second round of notes. Same title as the first on purpose: the order still stays fixed."
},
{
"id": 7,
"title": "Colours I like",
"slug": "colours",
"summary": "The palette behind this site.",
"body": "Why the site uses calm greens and a warm background, and where each colour came from."
},
{
"id": 8,
"title": "Questions people ask",
"slug": "questions",
"summary": "Short answers to common questions.",
"body": "Who writes this site, how often it changes and how to get in touch."
}
]
2. Split the list into pages. Replace site/pages/index.html. Two new fetch.pages lines choose the page, the count now reads #pages_page.total#, and a small navigation follows the cards. Each link repeats the search and the order with |urlencode, so a page link never loses them. The form has no page field: a new search starts again at page 1.
<what>
title = "Pages | My website"
description = "Everything I have written, gathered from one data file."
fetch.pages = "local:pages"
fetch.pages.search = "#query.q#"
fetch.pages.search_fields = "title, summary"
fetch.pages.sort = "#query.sort#"
fetch.pages.sort_options = "id, title, title:desc"
fetch.pages.page = "#query.page#"
fetch.pages.page_size = "3"
</what>
<h1>Pages</h1>
<form class="listing-controls" method="get" action="/pages" role="search">
<div class="field">
<label for="q">Search titles and summaries</label>
<input id="q" name="q" type="search" value="#query.q#">
</div>
<div class="field">
<label for="sort">Order</label>
<select id="sort" name="sort">
<if query.sort == "title">
<option value="id">ID order</option>
<option value="title" selected>Title A to Z</option>
<option value="title:desc">Title Z to A</option>
<elseif query.sort == "title:desc"/>
<option value="id">ID order</option>
<option value="title">Title A to Z</option>
<option value="title:desc" selected>Title Z to A</option>
<else/>
<option value="id" selected>ID order</option>
<option value="title">Title A to Z</option>
<option value="title:desc">Title Z to A</option>
</if>
</select>
</div>
<button type="submit">Show pages</button>
</form>
<p class="result-count">Matching pages: #pages_page.total#</p>
<if pages>
<div class="card-grid">
<loop data="#pages#" as="page">
<what-content-card heading="#page.title#" summary="#page.summary#" href="/pages/#page.slug|urlencode#"/>
</loop>
</div>
<nav class="pager" aria-label="More pages">
<if pages_page.has_prev><a rel="prev" href="/pages?q=#query.q|urlencode#&sort=#query.sort|urlencode#&page=#pages_page.prev#">Previous page</a><else/><span></span></if>
<span aria-current="page">Page #pages_page.number# of #pages_page.pages#</span>
<if pages_page.has_next><a rel="next" href="/pages?q=#query.q|urlencode#&sort=#query.sort|urlencode#&page=#pages_page.next#">Next page</a><else/><span></span></if>
</nav>
<else/>
<if query.q>
<p class="empty-state">No pages match “#query.q#”. <a href="/pages">Show all pages</a></p>
<else/>
<p class="empty-state">No pages yet. Add a record to data/pages.json.</p>
</if>
</if>
3. Style the navigation. Replace static/styles.css; two rules are added at the end.
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
margin: 0;
color: #26372c;
background: #faf8f2;
font-family: system-ui, sans-serif;
line-height: 1.65;
}
.site-header, .site-main, .site-footer {
width: min(100% - 2rem, 48rem);
margin-inline: auto;
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-block: 1.5rem;
border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
outline: 3px solid #825414;
outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
position: absolute;
top: -10rem;
left: 1rem;
padding: 0.6rem 1rem;
background: #fff;
z-index: 10;
}
.skip-link:focus { top: 1rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }
.listing-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1rem; margin-bottom: 1rem; }
.listing-controls .field { display: grid; gap: 0.25rem; }
.listing-controls input, .listing-controls select, .listing-controls button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.listing-controls input { min-width: min(100%, 16rem); }
.listing-controls button { background: #315f40; border-color: #315f40; color: #fff; cursor: pointer; }
.listing-controls :focus-visible { outline: 3px solid #825414; outline-offset: 2px; }
.result-count { color: #4b594e; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.pager [aria-current] { font-weight: 650; }
No restart is needed: changes to data/pages.json are read on the next visit.
Check
- Open /pages. It shows three cards, “Matching pages: 8” and “Page 1 of 3”.
- Follow “Next page” twice. Page 3 shows the last two records and no “Next page” link. Every record appeared exactly once.
- Choose “Title A to Z”, then follow “Next page”. The address keeps
sort=title, and the two “Weekly notes” cards stay in the same order every time. - Search for
notes: three matches (both “Weekly notes” and one summary), one page. Press Back: you return to page 2 of the title order, exactly as it was. - Open
/pages?page=9(404, not found) and/pages?page=abc(400, bad request).
Exercise
Change fetch.pages.page_size to 2. Now there are four pages: walk through them and check that all eight records still appear once, that ?page=4 is the last page and that ?page=5 is not found. Set it back to 3 when you are done.
If something goes wrong
- Every visit shows an error page.
pageandpage_sizego together, the page size is a whole number from 1 to 100, and neither can be combined withlimitoroffset. - “Next page” forgets the search. Check that the link repeats
qandsortwith|urlencode, joined by&. - A bookmarked page is suddenly “not found”. Records were removed and that page no longer exists. Page 1 always does.
- Start again from a known state. Download the L11 checkpoint into a new, empty folder; never unpack it over your project.
Next: Make content durable.