You may have seen Doug’s post on how to add Swiftype to Octopress. I originally followed it to get things working too. But I wanted a slightly different experience and here are my notes for that.
I was on the mission to speed up this site. So I was really scrutinizing the page load times and found that Swiftype javascript was loading for each page. My Swiftype weekly report was showing that almost none of my users were actually using the typeahead search feature. So I decided I don’t need that portion on this website. Instead I’ll just have Swiftype installed on the search results page only.
Setting up Swiftype
Do the following on Swiftype.com
- Create an account at Swiftype.com
- If you haven’t already, create an engine for your site.
- Click the Install tab.
- Choose “Different Page” for the search results appearance
- Choose a URL for where the search results will display. I chose http://www.tunnelsup.com/search/index.html
- Choose
.search
for the search field input selector. - Choose
#st-results-container
for the results container. - Disable autocomplete.
- Save
Now go over to Overview tab and look for your “Engine Key”. We’ll need that later.
Create the Search results page
Do something like rake new_page["search"]
and I think that will make a search/index.markdown file.
Here’s the contents of my entire index.markdown page (except the yaml at the top).
|
I have no idea what’s happening in this script so I’m sorry I can’t explain it.
Create the search box in the navbar
In navigation.html
you can add a search box. Here’s what mine looks like:
|
The names here are important because Swiftype will reference the class of this form while our jquery coming up next is going to refrence the form value.
Now comes the part that feels hacky to me. If you have a better solution please let me know in the comments. Since jquery is already loaded by default we’ll take advantage of it slightly here.
In after_footer.html
add the following:
|
This will trigger when someone hits enter on the search box. When that takes place it will redirect the user to the search results page with a specially crafted URL. For some reason Swiftype needs this URL in order to process the search request. Then we restrict the form from being submitted by doing the preventDefault() function.
The results should be the same as what you see on my search results page on this website.
Comments