Search is the first thing many visitors reach for on a local directory. They know the name of the business, or they type "emergency plumber" and expect a list. On a static site there's no database to query, so search has to run in the browser from data you ship with the pages. That sounds heavy, and it can be. A careless index for a few thousand listings can easily outweigh everything else on the page.
It doesn't have to be. A directory's search needs are narrow: match names, categories and places, then link to the right page. With a small, purpose-built index and a few lines of JavaScript, you can make search feel instant without pulling in a full-text search library. Here's how I'd approach it.
Decide what search actually needs to find
Start by listing what visitors type. On most local directories it falls into a few patterns: a business name, a category word, a city or neighborhood, or a combination like "dentist springfield". Very few people search for street addresses or phone numbers.
That tells you which fields belong in the index. Name, category, city and the URL of the listing page cover nearly every query. Ratings and review counts can come along if you want to sort results, but descriptions, hours and full addresses can stay on the listing pages where they already live.
Cutting fields is the biggest single saving. An export from Outscraper, Apify or any scraper typically has dozens of columns. The index needs four or five.
Build the index at build time
Generate the index during the same build step that writes your HTML. Short keys and a flat array keep the file small, and normalizing text once at build time saves work in the browser.
// build-index.js: run after the CSV is parsed into rows
const fs = require("fs");
const norm = s => (s || "")
.toLowerCase()
.normalize("NFD").replace(/[̀-ͯ]/g, "")
.replace(/[^a-z0-9 ]+/g, " ").trim();
function buildIndex(rows) {
return rows.map(r => ({
n: r.name, // display name
u: "/listings/" + r.slug + "/", // page URL
c: r.category,
t: r.city,
k: norm([r.name, r.category, r.city].join(" ")) // search text
}));
}
function search(index, query) {
const words = norm(query).split(" ").filter(Boolean);
return index.filter(e => words.every(w => e.k.includes(w))).slice(0, 20);
}
module.exports = { buildIndex, search, norm };
// fs.writeFileSync("dist/search.json", JSON.stringify(buildIndex(rows)));
The same norm function runs in the browser on the query, so accents, punctuation and case never cause a miss. Requiring every word to match keeps results tight: "dentist springfield" only returns dentists in Springfield.
Keep the payload small
A few thousand entries in this shape usually compress well, since names and categories repeat a lot. Check the gzipped size of search.json in your build output and set yourself a budget.
If the directory covers a large region, split the index. One file per county works well, because most visitors search within the area they're already browsing. The county page loads its own index, and the homepage can load a lightweight index of counties and categories that routes people to the right place.
Also load the index lazily. There's no reason to download it on page load when most visitors never touch the search box. Fetch it when the input gets focus, and the first keystroke usually lands after the file has arrived.
Ranking that feels right
Plain filtering returns matches in whatever order the array holds. Two small tweaks go a long way. Put exact name matches first, so someone typing a business name sees it at the top. Then sort the rest by a field you trust, such as review count, or alphabetically if you'd rather not imply any ranking.
Avoid getting clever with fuzzy matching unless you see real misspellings in your logs. Fuzzy search on short business names produces strange results, and visitors lose trust quickly when a search for "Bell Plumbing" returns a bakery.
Make it work without JavaScript too
Search should be a convenience, not the only route to a listing. Every listing needs to be reachable through county, city and category pages with ordinary links, so crawlers and visitors without JavaScript can still find everything. The search box itself can sit inside a form that submits to a plain results page, or simply hide when scripts don't run.
Label the input properly, announce the number of results to screen readers with a live region, and make sure results can be reached with the keyboard. Then test it with real queries. This guide to testing directory search and filters before launch has a good list of queries worth trying, including the empty and misspelled ones.
More notes on the code side of static directories live in this collection on building static directory sites. If you'd rather not build it yourself, 1 Click Directory Builder generates a static directory from a Google Maps export with search and filters included.
Questions about static directory search
When should I switch to a search library?
When you need typo tolerance, relevance scoring across long text, or more than tens of thousands of entries per index file. Most local directories never reach that point.
Should search results link to listing pages or open a popup?
Link to listing pages. They're shareable, crawlable and already designed for reading details.
How do I know what people search for?
Log queries with your analytics tool as custom events, without storing anything personal. A month of queries tells you which fields and synonyms matter.
Top comments (0)