Boost search autocomplete performance
Suggesting relevant search terms while a visitor types in a search box is a common practice on many websites and search engines. It provides a relevant and fast response that is essential for an excellent user experience.
To do this, you’ll create an EdgeWorkers bundle that contains a JSON key-value store with the responses for the most popular terms.
Follow these steps to implement a serverless function, written in JavaScript, that accelerates autocomplete at the Edge.
-
Create a key value store with labels and values in a file called
searchterms.js.
Format the file as shown. You can download a complete example from GitHubThis could be a local file, or you can use the EdgeKV CLI to store the data on the edge network. The local list of terms in this example code uses JSON formats seen in jQuery UI or Awesomplete.
-
Create a
main.jsfile, the JavaScript source that contains event handler functions.This EdgeWorkers code takes the
GETparameterterm=and looks up the term in the key value store filesearchterms.js. If a match is found, a 200 status with the serialized JSON response is returned, and when there is no match, the request is forwarded to origin. -
Create a
bundle.jsonfile that contains metadata for the EdgeWorker function. -
Create a code bundle to compress the
.main.jsandbundle.jsonfiles into a.tgzfile.
tar -czvf filename.tgz main.js bundle.json -
Activate the version, see Manage EdgeWorkers.
-
Use the EdgeWorkers CLI to update the results for the most popular search terms via a scheduled task.