what

Make listings searchable and shareable

Add a search box and a choice of order to /pages, and keep both in the address, so a reload, a bookmark or a link sent to a friend shows the same results.

Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder with the dev server running.

Read

Start at L09. /pages shows every record. With a handful that is fine; with more, visitors need to find one.

A form with method="get" puts what the visitor typed into the address: searching for “notes” opens /pages?q=notes. The part after ? is the query string, and inside a page each value is #query.name#. Because the whole state is in the address, reloading, going back or sharing the link brings the same results back. No session or cookie is involved.

The page decides what a visitor may change. search_fields lists the fields a search looks in, and sort_options lists the orders a visitor may choose; the first one is the default. A search matches text inside those fields, ignoring the case of English letters, and characters such as % or _ are just characters. An order that is not in the list is refused with “400 Bad Request” instead of quietly showing something else. Records with the same title keep a fixed order, so the same address always gives the same list.

Change

1. Add the controls. Replace site/pages/index.html. The new fetch.pages lines read the search and the order from the address. The form shows the current search in its box and the current order in its menu, so the visitor sees what they asked for. A search that finds nothing says so and offers the full list again.

site/pages/index.html
<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"
</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|count#</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>
<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>

2. Style the form. Replace static/styles.css. The new rules at the end lay the controls out in one row on wide screens and stack them on phones.

static/styles.css
/* 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; }

No restart is needed.

Check

  1. Open /pages. It shows the three pages in ID order, the order of their id numbers, and “Matching pages: 3”.
  2. Search for learning. The address becomes /pages?q=learning&sort=id and one card is left. Reload: the same card and the same search box.
  3. Choose “Title Z to A” and press “Show pages”. Use the browser’s Back button: the previous results come back.
  4. Search for 50%: nothing matches, and the page says so with a link to all pages. Clear the box and submit: all three pages are back.
  5. Open http://127.0.0.1:8085/pages?sort=price. The order price is not allowed, so the answer is “400 Bad Request”.

Exercise

Let the search look in the page text too. Add body to fetch.pages.search_fields, then search for a word that appears only in a body, such as lesson. Check that the address still reproduces the result after a reload. The change is one field name: the visitor still cannot choose where to search.

If something goes wrong

  • Every visit shows an error page. Check the fetch.pages lines. A search needs search_fields, and an order read from the address needs sort_options; field names are letters, digits and _.
  • The menu jumps back to “ID order”. Each <option> value must match an entry in sort_options exactly, such as title:desc.
  • The static export has no /pages any more. That is expected. A listing that reads the address needs the running server; the build reports it as skipped instead of writing a page that would ignore every search.
  • Start again from a known state. Download the L10 checkpoint into a new, empty folder; never unpack it over your project.

Next: Paginate without losing your place.