Sub topic 3
No Results, Errors, and Recovery
This chapter assumes you know how a search field and filter controls send a query to a backend, that you can read TypeScript and HTML, and that you have the search and filter patterns from earlier chapters.
No results, errors, and recovery
A search can end in two failure states. It can succeed with zero matches, or the request can fail. Each needs a different message.
- Zero results: say what was searched, show the active filters, and offer a way to remove them.
- Request error: say the search did not run, keep the query text, and offer a retry button.
- Timeout: treat a slow request as an error after a fixed wait, and show cached or popular items if you have them.
Latency and debouncing
Debouncing is a technique that waits until the user pauses before sending a request. It cuts requests from one per keystroke to one per pause.
function debounce<A extends unknown[]>(fn: (...args: A) => void, waitMs: number) {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: A) => {
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(() => fn(...args), waitMs);
};
}
const onInput = debounce((value: string) => {
console.log("fetch suggestions for", value);
}, 200);
onInput("b");
onInput("bo");
onInput("boots"); // Only "fetch suggestions for boots" prints.Show a loading indicator only after about 300 ms, so fast responses do not flicker. Keep the previous results visible while new ones load, and mark them as stale.
Search and filters on small screens
Keep the search field at the top of the screen. Move filters into a drawer opened by one button, and show the number of active filters on that button. Apply changes when the user taps "Show N results" instead of reloading after each tap.
Making search and filters accessible
Wrap the search in a form with role="search", and give the input a visible label. Announce result counts in a live region.
<form role="search" action="/search" method="get">
<label for="q">Search products</label>
<input id="q" name="q" type="search" autocomplete="off">
<button type="submit">Search</button>
</form>
<p role="status">24 results for "boots"</p>Label each filter checkbox with its name and count. After a reload, move focus to the results heading.
> Keep the query and filter selections after an error. Losing typed text is a common reason people abandon a retry.
Measuring and improving search
Track the share of searches with zero results, the click-through rate on the first page of results, and the median time to first result. A high zero-result rate usually points to missing synonyms or overly strict filters.
Exercise
A user applies three filters and gets zero results. What should the screen show?
Show answer
The query and the active filters, a message that no items match, and buttons to remove filters one at a time or clear them all. The query text stays in the search box.