-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search: Update from algoliasearch v3 to v4
* Use the new algoliasearch-lite mode available as of v4, which implements only the search() API and that's the only API we need. Overall this reduces the JS payload from ~70KB to ~11KB. * Avoid script errors on older browsers by limiting inline script execution with `type="module"`.
- Loading branch information
Showing
2 changed files
with
20 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,9 @@ | |
|
||
Remember to use the 'defer' attribute. | ||
{% endcomment %} | ||
<script defer src="https://cdn.jsdelivr.net/npm/algoliasearch@3.26.0/dist/algoliasearch.min.js" integrity="sha256-dgq8vEaJVxWMmcF/BEk9W6Wm+o2+LFlw1VSl2sx4JCI=" crossorigin="anonymous"></script> | ||
<script defer src="https://cdn.jsdelivr.net/npm/algoliasearch@4.3.1/dist/algoliasearch-lite.umd.js" integrity="sha256-YuyfhK9VEyfW6ejFiPbfOX2SELbFrgFU/8tU3MHuh60=" crossorigin="anonymous"></script> | ||
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/autocomplete.min.js" integrity="sha256-YVWQosorZnr6fALvOW9VALYuInld27RkSPkElGBdCaU=" crossorigin="anonymous"></script> | ||
<script> | ||
<script type="module"> | ||
window.addEventListener('DOMContentLoaded', function setupSearch() { | ||
const client = algoliasearch('{{ site.algolia.application_id }}', '{{ site.algolia.search_only_api_key }}'); | ||
const indexMain = client.initIndex('{{ site.algolia.index_name }}'); | ||
|
@@ -37,9 +37,22 @@ | |
return suggestion.url; | ||
} | ||
} | ||
function newHitsSource(index, params) { | ||
return function doSearch(query, cb) { | ||
index | ||
.search(query, params) | ||
.then(function(res) { | ||
cb(res.hits, res); | ||
}) | ||
.catch(function(err) { | ||
console.error(err); | ||
cb([]); | ||
}); | ||
}; | ||
} | ||
const autocompleteSources = [ | ||
{ | ||
source: autocomplete.sources.hits(indexApi, { hitsPerPage: 5 }), | ||
source: newHitsSource(indexApi, { hitsPerPage: 5 }), | ||
displayKey: '', | ||
templates: { | ||
suggestion(suggestion) { | ||
|
@@ -56,7 +69,7 @@ | |
} | ||
}, | ||
{ | ||
source: autocomplete.sources.hits(indexMain, { hitsPerPage: 5 }), | ||
source: newHitsSource(indexMain, { hitsPerPage: 5 }), | ||
displayKey: '', | ||
templates: { | ||
suggestion(suggestion) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters