DEV Community 👩‍💻👨‍💻

Farai Gandiya
Farai Gandiya

Posted on • Originally published at on

How To Add Search To Your Static Site

This post was originally posted on The Algorithmic Cookbook under How To Add Search To Your Static Site. This post is also on YouTube

In this post, I’ll go over how to add search to a static site by adding a form which points to a search engine.

What’s The Search Form Should Have

For the search form to work, it needs:

  • The form with role attribute search, a method attribute (usully GET) and an action attribute with a URL to your search provider,
  • the (labelled) text input field with the name attribute set to what the search engine uses as the search field whose value include site:yourdomain.example , and
  • a submit button along with
  • some optimizations like multiple search engines and hiding the site:yourdomain.example in the search field.

I’ll assume that search engines don’t use anti-CSRF tokens which you’ll need to find a way to submit with the rest of the form.

I’ve already added a search form in my site’s header if you wanna try it out and it looks like this.

input field and button which says Search in a Google

Examining the Search Engine

To start off, go to a search engine and see how it works. Here I’ll be using Google Search. Inspecting the home page, the search box is an input field with name q. Moving up to the form element, we can see an action of /search (which resolves to and a method of GET. Trimming the unnecessary stuff, Google’s search form looks like this (… is truncated code).

<form ... action="search" ... method="GET" name="f" ...role="search">
  <input ... name="q" ... aria-label="search">
  <input ... value="Google Search" ... name="btnK" type="submit">

Enter fullscreen mode Exit fullscreen mode

If I then search for, Google gives me the search results on my website, the Algorithmic Cookbook.

Google search results for

Those are shown by this URL.

Enter fullscreen mode Exit fullscreen mode

It looks horrendus with all the other parameters (which were hidden), but all you need is the q parameter with the (URI encoded) search field. So this works just as well.

Enter fullscreen mode Exit fullscreen mode

Using what we’ve discovered, we can make our own form.

<form method="GET" class="search-form" action="" role="search" name="search-form">
    <label for="search-form_query" class="sr-only search-form_label">Search Term</label>
    <input class="search-form_query" type="text" name="q" id="search-form_query" value=" ">
    <button class="search-form_button" type="submit">Search on Google</button>

Enter fullscreen mode Exit fullscreen mode

Here we’ve taken what we’ve seen from the Google search and moved it into a form.

The form’s action points to what we saw on Google’s search form with method GET. There’s also a role=search to expose the form as a search landmark for assistive technologies.

There’s a label for the search input. The sr-only is to visually hide the label so that assistive technologies can still understand what the input is for. There are other accessible labeling techniques in case you really don’t want a label element.

The input itself has name q, type text, the id for the label and a pre-filled value of␣ ( represents whitespace U+020). The pre-filled label is necessary to search your site, otherwise it will search the web instead of your specific site.

What I’ve done above is the most supported, barebones way of searching through a search engine. No JavaScript needed. I’m also confident that it’ll work across various search engines with their respective form actions and search query parameters.

Multiple Search Engines

If search engines use the same search button form parameter, you could add a button with the formaction to another search engine. For instance, Google and DuckDuckGo use q as a search parameter. I could support DuckDuckGo by adding this button:

<button action="submit" formaction="">Search DuckDuckGo</button>

Enter fullscreen mode Exit fullscreen mode

In DuckDuckGo’s case, you can also search via POST and you can add formmethod=POST to do just that.

Hiding the site:yoursite.example from the form input

The reason I used Google is because it makes this easier. We can add a second input with a name q with a hidden attribute and assign site:yoursite.example as the value.

<input type="text" name="q" label="" hidden>

Enter fullscreen mode Exit fullscreen mode

This is search engine specific, but browsers will supply the two q parameters and the search engine may merge them together.

If you can’t do this, you can then use JavaScript to remove it on load and replace it when the user wants to submit the form submitting the form.

const searchForm = document.forms['search-form'];

window.addEventListener('load', () => {
  searchForm.q.value = '';

searchForm.addEventListener('submit', function(e){
  searchForm.q.value += 'site:yoursite.example';

Enter fullscreen mode Exit fullscreen mode

If you don’t want to hard code the site:yoursite.example, you can do 'site:' + window.location.hostname instead.

Comparison of Search Engines

I’ve put this table together to compare how various search engines are set up. I could probably get away with just listing 4 or 5 of these, but discovering these search engines was fun.

Search Engine Form URL Methods Search Term Name Merges Multiple Inputs? Cross Compatable With
Google GET q Yes DuckDuckGo, Ecosia, Bing, Yooz, Quant, Parsijoo
Bing GET q No Google, DuckDuckGo, Ecosia, Yooz, Quant, Parsijoo
Yahoo GET, POST p No
DuckDuckGo GET, POST q No Google, Ecosia, Bing, Yooz, Quant, Parsijoo
Yandex GET text No
Baidu GET wd No
Ecosia GET q No Google, DuckDuckGo, Bing, Yooz, Quant, Parsijoo
Startpage GET, POST query No SwissCows
Swisscows GET query Maybe (it mashes them, but didn’t have results) `query
Yooz GET q No Google, DuckDuckGo, Ecosia, Bing, Quant, Parsijoo
Quant GET q No Google, DuckDuckGo, Ecosia, Bing, Yooz, Parsijoo
Parsijoo GET q No Google, DuckDuckGo, Ecosia, Bing, Yooz, Quant


And that’s how you can add search to your static site through a form pointing to a search engine. This isn’t the only way to add search to a static site and I hope to explore other ways in the future.

As a teaser, I’ve made a purely front-end search solution on my old website with lunr.js, but it’s a horrible front end strategy as it:

  • adds ~450KB of JavaScript on the webpage,
  • doesn’t prebuild the search index and
  • does all the work on the main thread.

Given what I know now, it’s time I improved this approach. Heck, I might even be able to move this logic onto a backend as is and leverage serverless functions to do the actual searching. Possibilities are endless1.

  1. Possibilities are probably not endless, unless you calculate every permutation of code that can do exactly this, then they are I guess? Then again, you’re limited to a few general approaches. But aren’t the specifics distinct algorithms as subsets of the general approaches? Should have just left this as a cliche ending. ↩︎

Thanks for reading! If you’ve enjoyed this post, you can support me by

Top comments (0)

Let's Get Wacky

Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

Join the Hackathon <-