My Search Bar Autocompletes DJs, Then Plays Their Music Before You Click Anything

My Search Bar Autocompletes DJs, Then Plays Their Music Before You Click Anything

BackerLeader ●2 ●10 ●52
calendar_today ago • schedule4 min read

Originally published on bryanhamilton.info.

Most search bars are a formality. You type, you press enter, you wait, you scan ten blue links. On a site about DJs and tour dates, that always felt wrong to me. You're not searching for a document about David Guetta - you're searching for David Guetta. The name, the face, the sound. So when I rebuilt the search on The DJ Calendar, I stopped thinking about it as a search box and started thinking about it as an instrument. Here's what it does now, and how it's built.

The typeahead ships with the page

The autocomplete index is 99 artists - every DJ I track, with their id, display name, DJ Mag rank, and avatar - embedded as JSON directly in the page. No API call, no debounced fetch, no spinner. You type two characters and the dropdown is already filtered, because the entire index was in the browser before you started typing.

The filter is a plain substring match, capped at the top 8 hits, each rendered with its thumbnail image. One detail I'm quietly proud of: results come back in DJ Mag rank order, not alphabetical. Type "mar" and you get Martin Garrix and Marshmello ranked by their actual chart position, because the index is stored rank-ordered and I never sort it. Alphabetical would be the lazy default; relevance to why people visit the site is the better default.

The whole thing is guarded the boring-but-important way: a dataset.searchBound flag so the handler can't double-bind, click-outside to close, Escape to dismiss and blur.

Then it plays their music before you click anything

This is the part people remember. Hover any result in the dropdown and, after a beat, a mini player slides in on the right side of the screen: artwork, track name, artist name, a live progress bar - and a 30-second audio preview starts playing. You haven't clicked anything. You hovered a name and the site told you what they sound like.

The build behind it is where the real work lived:

A 250ms intent delay. The preview fires on mouseover, which fires constantly. So the lookup waits 250ms. Sweep your cursor across eight results looking for the one you want, and nothing plays. Rest on one, and it does. The difference between "magical" and "annoying" is a single setTimeout.

A generation token on every fetch. The preview URL comes from iTunes' Search API, which is async - so a slow response from the result you hovered first could land after you've moved on and start playing the wrong artist. Every hover increments a token; when a response arrives, if its token doesn't match the current one, it's discarded. Stale results die silently instead of haunting you.

A per-artist cache. The lookup result is cached by name, so hovering Calvin Harris, moving away, and hovering back replays instantly - no second network round trip.

Single-audio discipline. Hovering the search result and hovering the flagship DJ card on the homepage use two different preview systems, so they'd happily play over each other. The hover player now explicitly stops the card engine before starting its own audio, and exposes a global stop function. There is exactly one moment where sound comes out of this site, and every feature agrees on it.

Graceful failure everywhere. If the browser blocks autoplay, the panel still shows track info - you get the "who is this" value without the sound. If iTunes has no preview, the panel says so instead of hanging on "Loading...". And Escape or Backspace kills playback immediately, because Backspace is what your thumb is on when you're re-typing a search.

One extra that cost eight lines: the panel writes to the navigator.mediaSession API, so the track you're previewing shows up on your lock screen / media keys with real artwork. A preview player that reports itself as a first-class media session. It's a web page behaving like a native music app, and it cost less than a coffee.

The full search page does the boring half properly

All of that is the front door. The dedicated /search/ page handles the actual lookup - artist, city, or venue, with filter tabs - over an index built server-side from the tour-date data itself. Search "Ibiza" and you get the dates; search a club and you get who's playing there. Same SearchAction JSON-LD wired into the browser's site-search entry point, so the browser itself knows this site can search.

The stack, for the inevitable comment

PHP on the server, vanilla JavaScript on the client, no frameworks, no libraries, no bundler. The hover player is about 9KB of hand-written JS including its CSS. The typeahead index rides along in the HTML. There's no search service to pay for and nothing between the keystroke and the result.

None of the individual pieces are hard. The 250ms delay, the stale-token guard, the single-play rule - each one is five lines. But a search bar that's "just" autocomplete ships five lines of behavior; the other fifteen lines of intent-handling are what make it feel like the site is listening instead of processing.

Question for the comments: what's the smallest amount of code you've shipped that made users think the site was way more expensive to build than it was? Mine is currently this hover player, and it's 9KB.

Want more #webdev & #javascript deep dives?

Join 6,011+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 9 of 9 in Live-Verified

More Posts

Cloudflare Blocked My Server, So I Made My Visitors' Browsers Do the Fetching

bryanhamiltondevverified - Nov 15

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

Sovereign Intelligence: The Complete 25,000 Word Blueprint (Download)

Pocket Portfolio - Apr 1

What Security Teams Want From AI Agents Before They Let Them Act Alone

Tom Smithverified - Nov 18
chevron_left
3.3k Points • 64 Badges
Rochelle Park, New Jersey, US • bryanhamilton.info
27Posts
35Comments
24Connections
Full-stack web engineer · PHP & vanilla JS · 25+ years · Founder of The DJ Calendar · Open to work

Related Jobs

Commenters (This Week)

6 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!