TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How MDN's Autocomplete Search Works

227 pointsby oedmarapalmost 4 years ago

16 comments

winridalmost 4 years ago
We took a similar approach for our documentation search. [0]<p>You can see the &quot;inverted index&quot; is rendered inline in the page, since everything is generated at build time.<p>When you type something that matches a key in the index, we fetch that index key and add it to the results. [1] [2]<p>Obviously we could do a lot better in terms of relevancy, but it&#x27;s simple and fast.<p>[0] <a href="https:&#x2F;&#x2F;docs.fastcomments.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.fastcomments.com&#x2F;</a><p>[1] <a href="https:&#x2F;&#x2F;docs.fastcomments.com&#x2F;index-ublJLBnXgz88.json" rel="nofollow">https:&#x2F;&#x2F;docs.fastcomments.com&#x2F;index-ublJLBnXgz88.json</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;FastComments&#x2F;fastcomments-docs&#x2F;blob&#x2F;main&#x2F;src&#x2F;index.js#L92" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FastComments&#x2F;fastcomments-docs&#x2F;blob&#x2F;main&#x2F;...</a>
评论 #28053562 未加载
sa3danyalmost 4 years ago
I remember doing something similar a few years ago, I needed autocomplete for a shipping ports field, the data was too big though so I ended up using a csv file in an aws lamda function that filters based on the selected country and returns a much smaller subset. It lazy loaded after the user selected the country. To keep response times low I had to do a binary search on the raw csv bytes. It felt like I was reinventing databases but I liked the idea of it being self contained in a function.
评论 #28056570 未加载
mgalmost 4 years ago
My favorite autocomplete library is an ancient version of bootstrap-typeahead.js by Twitter. A single file with less than 400 lines of Javascript. They don&#x27;t make these anymore :)<p>I use it everywhere where I need autcompletion. For example on the Music-Map:<p><a href="https:&#x2F;&#x2F;www.music-map.com" rel="nofollow">https:&#x2F;&#x2F;www.music-map.com</a><p>I made a fork of the code which is available here:<p><a href="https:&#x2F;&#x2F;www.gibney.org&#x2F;0g-typeahead" rel="nofollow">https:&#x2F;&#x2F;www.gibney.org&#x2F;0g-typeahead</a>
评论 #28053692 未加载
评论 #28052327 未加载
评论 #28058460 未加载
bityardalmost 4 years ago
They hi-jacked the browser&#x27;s `&#x2F;` key to focus the field, which is something I hate. As a user, I want `&#x2F;` to bring up Firefox&#x27;s quick search bar, especially when reading documentation.<p>They should have just had the search field focused automatically but that would have done away with their &quot;clever&quot; hack to lazy-load the DB containing every page name.<p>Also, I&#x27;m confused, I thought <a href="https:&#x2F;&#x2F;mdn.dev&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mdn.dev&#x2F;</a> was the new thing because Mozilla was stepping back from MDN. Is it a fork? They both carry Mozilla logos, so what&#x27;s going on there?
评论 #28052012 未加载
评论 #28052506 未加载
评论 #28053060 未加载
评论 #28053677 未加载
评论 #28056224 未加载
评论 #28052023 未加载
评论 #28053558 未加载
评论 #28057622 未加载
评论 #28058553 未加载
评论 #28051909 未加载
muxatoralmost 4 years ago
Now I am curious if, in the real MDN production site, serach-index.json loading is triggered by the execution of &#x2F;static&#x2F;js&#x2F;autocomplete.js, when their download should really be started in parallel by the shim.<p>Many websites leave a lot of performance on the table because of such behaviors.<p>My hypothesis is that, since this is easier for the developer, and works good enough, not many people really care. But these things add up, and the web becomes slower and slower.
评论 #28053568 未加载
ShrigmaMalealmost 4 years ago
I like mosra&#x27;s search, implemented in m.css for magnum. He wrote a blog post on it here: <a href="https:&#x2F;&#x2F;blog.magnum.graphics&#x2F;meta&#x2F;improved-doxygen-documentation-and-search&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.magnum.graphics&#x2F;meta&#x2F;improved-doxygen-documenta...</a> and you can try it on the magnum docs site: <a href="https:&#x2F;&#x2F;doc.magnum.graphics&#x2F;magnum&#x2F;#search" rel="nofollow">https:&#x2F;&#x2F;doc.magnum.graphics&#x2F;magnum&#x2F;#search</a><p>Fast and can be served from a static site.
brailsafealmost 4 years ago
Are they using react for just this one thing on this page!? Honestly it wasn&#x27;t very clear to me, but they seemed to indicate that. I wonder if that&#x27;s just because it&#x27;s the pattern at MDN, but I feel like shipping react along with the JSON has got to be huge.
评论 #28056147 未加载
eric4smithalmost 4 years ago
As an aside here... MDN docs are pretty awesome. I&#x27;ve been learning pure vanilla Javscript on this site more than anywhere else.<p>I used to automatically include jQuery in every project as a habit&#x2F;reflex.<p>Now, because of MDN, I never do that anymore, unless It makes total sense. Kudos guys!
encryptluks2almost 4 years ago
I think adding search to the HTML standard makes more sense overall. The thing I hate about search like this is that they don&#x27;t work with JS turned off (e.g. terminal browser). Why not just add a JSON search component to HTML itself?
评论 #28057451 未加载
namanyaygalmost 4 years ago
In the code snippet they show the `startAutocomplete()` function checks for the &quot;started&quot; variable being true; but never actually sets it to true.
评论 #28053704 未加载
ushakovalmost 4 years ago
i&#x27;m wondering how much kb it loads before ready to search?<p>update: 144KB for JSON file<p>a little bit worrying, given their scale and potential bandwidth requirements
评论 #28052110 未加载
评论 #28052281 未加载
thinkloopalmost 4 years ago
I thought this was going to be about advanced usage of &lt;datalist&gt;: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTML&#x2F;Element&#x2F;datalist" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTML&#x2F;Element&#x2F;da...</a>
评论 #28053467 未加载
评论 #28053219 未加载
fleroviumalmost 4 years ago
I can&#x27;t wait until FlexSearch reaches 1.0.0. Reading the source code is like reading great literature.
评论 #28053555 未加载
earthboundkidalmost 4 years ago
I miss the old search that let me narrow things down by category.
评论 #28053719 未加载
randompwdalmost 4 years ago
so, &quot;144KB over the network&quot; - how much does that equate to in memory?<p>And that&#x27;s per page where the search has been activated, correct? With no sharing of that dataset between each page?
vdmalmost 4 years ago
<a href="https:&#x2F;&#x2F;react-spectrum.adobe.com&#x2F;blog&#x2F;building-a-combobox.html" rel="nofollow">https:&#x2F;&#x2F;react-spectrum.adobe.com&#x2F;blog&#x2F;building-a-combobox.ht...</a>