← Build · all briefsBrief 3 · after "Connect the market" · demo next class
Brief 3

Instant search

Replace the in-browser search with the server's: GET /api/stalls?q=… searches names and blurbs. It has to feel instant, show the right results for what's in the box, and live in the URL so a search can be shared and the Back button works. ★★☆

“Somebody said there's a stall with pineapple. Which one was it?”

You'll demo (next class): type "pineapple" quickly: one request after you stop, and Taco Bike. Copy the URL into a new tab: same search, same result. Type, then press Back: the previous search comes back.

The core (start in class if you've connected, the minimum for the demo)

Four things, on top of your connected market. They count when they work with chaos on (control page).

The full feature (finish at home)

What has to work for the demo next class, not how. The core above is part of it.

Requests

  • GET /api/stalls?q= built with URLSearchParams, combined with the tag filter (?tag=food&q=…).
  • Debounced: one request once typing pauses for ~250 ms.
  • Every new search aborts the one in flight (AbortController).

States

  • Results, "No stall matches …" and an error each render from state; the old results stay visible, dimmed, while a search is loading.
  • The query and the tag are in the page's URL (?q=pineapple&tag=food), and the page starts from them on load.
  • Back and Forward restore earlier searches.

Errors

  • AbortError is never shown to the user and never reaches the console.
  • A 503 shows a message and keeps the previous results.
  • Typing faster than the server never shows results for an older query.

New tool: history.replaceState / pushState and the popstate event: change the URL without reloading.

The endpoints you need

MethodPathAnswers
GET/api/stalls?q=&tag=&sort=200 · array, X-Total-Count · 400 for an unknown tag

The server answers short queries slowly and long ones fast (more matches to rank): q=t takes about 0.7 s, q=pineapple about 0.1 s. Out-of-order responses are guaranteed.

Edge cases (with chaos on, minutes 38–43)

Stretch, if you're done early

Stuck? One hint at a time

Network panel first: is the request sent, with what, and what came back? Then a hint.

Hint 1

Debounce and abort solve two different problems. Which one does each solve, and which one makes the result correct?

Hint 2

replaceState or pushState: should every keystroke be a Back step?

Hint 3

On load, the URL has a query. What runs first: reading it, or the first render?

Traps

Open when something behaves strangely

Before you start

This brief assumes Connect the market is done: api.js works, the stalls come from the API, loading and errors are handled. Then: Typing sends GET /api/stalls?q=…; with fast typing, earlier ones show (canceled) in the Network panel.

Demo · next class, 3 minutes

Your 3 minutes
  1. The happy path, with the Network panel open next to the page.
  2. Chaos on: the loading state, one failure, and how the page recovers.
  3. One request in code: the call, the status codes you handle, where the state changes.
  4. One bug you hit, and the panel or the message that showed it to you.