Autocomplete using Edge Workers

This EdgeWorker serves responses for popular search terms at the Edge. Autocomplete requests are typically long tail and frequently changing. Without an EdgeWorker it is difficult to get up to date content from cache. Storing and serving the most popular search terms from the Edge will speed up responses significantly.

Outcome : Using the EdgeWorker CLI the most popular search terms can be updated on a regular basis. This EdgeWorker needs to be activated for the path matching your autocomplete service. It takes the GET parameter term= and does a lookup for the term in searchterms.js. If a match is found we return the serialized JSON response, when there is no match the request is forwarded to origin.

Below is the code snippet in main.js


/*
(c) Copyright 2020 Akamai Technologies, Inc. Licensed under Apache 2 license.
Version: 0.1
Purpose: Reply instantly to most popular search terms from the Edge, unpopular terms go forward to origin.
Repo: https://github.com/akamai/edgeworkers-examples/tree/master/fast-autocomplete
*/

import URLSearchParams from 'url-search-params';
import { default as searchterms } from './searchterms.js';

export function onClientRequest (request) {
  const params = new URLSearchParams(request.query);
  const jsonContentType = { 'Content-Type': ['application/json;charset=utf-8'] };
  const searchResult = searchterms[params.get('term').toLowerCase()];
  if (searchResult) {
    request.respondWith(200, jsonContentType, JSON.stringify(searchResult));
  }
}