Skip to content

Commit

Permalink
Replace hardcoded Coveo api keys with a call to Netlify function (#35)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sean-breen authored Jan 4, 2024
1 parent d9ebb8a commit 05302d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
16 changes: 13 additions & 3 deletions assets/js/coveo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as params from '@params';
document.addEventListener('DOMContentLoaded', function () {
Coveo.SearchEndpoint.configureCloudV2Endpoint("", params.coveokey);
document.addEventListener('DOMContentLoaded', async function () {

// Netlify function to get the coveo search token via API
async function getSearchToken() {
const response = await fetch(
window.location.origin+"/.netlify/functions/search_token"
);
return response.json();
}

const searchToken = await getSearchToken()
Coveo.SearchEndpoint.configureCloudV2Endpoint("", searchToken.token);

const root = document.getElementById("search");
const searchBoxRoot = document.getElementById("searchbox");
Coveo.initSearchbox(searchBoxRoot, "/search.html");
Expand Down
15 changes: 3 additions & 12 deletions layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@

<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>

<!-- build a different coveo.js (with different key) based on build env -->

<!-- build coveo.js -->
{{ $.Scratch.Set "coveoSc" "" }}
{{ if eq hugo.Environment "production" }}
{{- $coveo := resources.Get "js/coveo.js" | js.Build ( dict "params" ( dict "coveokey" "xx3b887403-1ec2-43cf-9e3c-d6dd3c1056c0" )) -}}
{{ $.Scratch.Set "coveoSc" $coveo}}
{{ else if eq hugo.Environment "staging" }}
{{- $coveo := resources.Get "js/coveo.js" | js.Build ( dict "params" ( dict "coveokey" "xxababbbee-d1e5-44e7-8e49-876ece453e60" )) -}}
{{ $.Scratch.Set "coveoSc" $coveo}}
{{ else }}
{{- $coveo := resources.Get "js/coveo.js" | js.Build ( dict "params" ( dict "coveokey" "xx6bbcd723-fec1-4af0-a13d-1c499cf5a15f" )) -}}
{{ $.Scratch.Set "coveoSc" $coveo}}
{{ end }}
{{- $coveo := resources.Get "js/coveo.js" | js.Build -}}
{{ $.Scratch.Set "coveoSc" $coveo}}
{{ $secureCoveo := $.Scratch.Get "coveoSc" | minify | fingerprint "sha512" }}


Expand Down

0 comments on commit 05302d6

Please # to comment.