Skip to content

Commit 05302d6

Browse files
authored
Replace hardcoded Coveo api keys with a call to Netlify function (#35)
* remove hardcoded key values, use variables instead * update variable names to COVEO_API_* * whitespace * remove conditional expression for each hugo build environment * use COVEO_SEARCH_TOKEN env var when calling js.Build * update comment * call Netlify function to get API key from environment * actually call function * use coveokey variable * call getSearchToken() function * remove passing search token as param to js.Build * make entire function async * fix path to function * get token string * use absolute path for search token api * try window.location.origin as prefix for token api
1 parent d9ebb8a commit 05302d6

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

assets/js/coveo.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
import * as params from '@params';
2-
document.addEventListener('DOMContentLoaded', function () {
3-
Coveo.SearchEndpoint.configureCloudV2Endpoint("", params.coveokey);
1+
document.addEventListener('DOMContentLoaded', async function () {
2+
3+
// Netlify function to get the coveo search token via API
4+
async function getSearchToken() {
5+
const response = await fetch(
6+
window.location.origin+"/.netlify/functions/search_token"
7+
);
8+
return response.json();
9+
}
10+
11+
const searchToken = await getSearchToken()
12+
Coveo.SearchEndpoint.configureCloudV2Endpoint("", searchToken.token);
13+
414
const root = document.getElementById("search");
515
const searchBoxRoot = document.getElementById("searchbox");
616
Coveo.initSearchbox(searchBoxRoot, "/search.html");

layouts/partials/scripts.html

+3-12
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,10 @@
2020

2121
<script src="https://static.cloud.coveo.com/searchui/v2.10104/0/js/templates/templates.js" integrity="sha512-CR0Yk/LIwgh1MsKqjecDp/r6F9ipKc6gA+4+E1pplT3N3r1pk+la/HaqyDiKtjOFwrwIIbIYBFrUJgPql93QHw==" crossorigin="anonymous"></script>
2222

23-
<!-- build a different coveo.js (with different key) based on build env -->
24-
23+
<!-- build coveo.js -->
2524
{{ $.Scratch.Set "coveoSc" "" }}
26-
{{ if eq hugo.Environment "production" }}
27-
{{- $coveo := resources.Get "js/coveo.js" | js.Build ( dict "params" ( dict "coveokey" "xx3b887403-1ec2-43cf-9e3c-d6dd3c1056c0" )) -}}
28-
{{ $.Scratch.Set "coveoSc" $coveo}}
29-
{{ else if eq hugo.Environment "staging" }}
30-
{{- $coveo := resources.Get "js/coveo.js" | js.Build ( dict "params" ( dict "coveokey" "xxababbbee-d1e5-44e7-8e49-876ece453e60" )) -}}
31-
{{ $.Scratch.Set "coveoSc" $coveo}}
32-
{{ else }}
33-
{{- $coveo := resources.Get "js/coveo.js" | js.Build ( dict "params" ( dict "coveokey" "xx6bbcd723-fec1-4af0-a13d-1c499cf5a15f" )) -}}
34-
{{ $.Scratch.Set "coveoSc" $coveo}}
35-
{{ end }}
25+
{{- $coveo := resources.Get "js/coveo.js" | js.Build -}}
26+
{{ $.Scratch.Set "coveoSc" $coveo}}
3627
{{ $secureCoveo := $.Scratch.Get "coveoSc" | minify | fingerprint "sha512" }}
3728

3829

0 commit comments

Comments
 (0)