Search-as-you-type is powered by the Ajax /search/suggest.json endpoint. You pass the query and which resource types you want; it returns ranked matches:
fetch("/search/suggest.json?q=" + encodeURIComponent(query) + "&resources[type]=product")
.then(function (r) { return r.json(); })
.then(function (data) {
// data.resources.results.products is the array of matches
});
The response nests the matches under resources.results (with products, collections, pages, articles). Fetch on the input's input event so results update as the shopper types, and URL-encode the query.
AI tell: looping over a client-side array of every product and substring-matching the title. That ignores ranking and typo tolerance and won't scale. Let the endpoint do the search.
Production refinement (not required here): debounce the fetch. Wait ~200ms after the last keystroke before calling the API, so typing 'kenya' fires one request instead of five. We skip it to keep the focus on the API, but never ship search-as-you-type without it.