Skip to content

Commit 08237b4

Browse files
committed
Coveo: Handle failed to fetch credentials from endpoint
1 parent 3e52910 commit 08237b4

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

assets/js/coveo.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,40 @@ async function getValidSearchCredentials() {
7171
}
7272

7373
async function atomicCoveo() {
74-
await customElements.whenDefined('atomic-search-interface');
75-
const credentials = await getValidSearchCredentials();
76-
77-
document.querySelectorAll('atomic-search-interface').forEach(async (el) => {
78-
await el.initialize({
79-
...credentials,
80-
analytics: { analyticsMode: 'legacy' },
81-
preprocessRequest: (request) => {
82-
const body = JSON.parse(request.body);
83-
body.q = `<@- ${body.q} -@>`;
84-
request.body = JSON.stringify(body);
85-
return request;
86-
},
74+
try {
75+
await customElements.whenDefined('atomic-search-interface');
76+
const credentials = await getValidSearchCredentials();
77+
78+
document.querySelectorAll('atomic-search-interface').forEach(async (el) => {
79+
await el.initialize({
80+
...credentials,
81+
analytics: { analyticsMode: 'legacy' },
82+
preprocessRequest: (request) => {
83+
const body = JSON.parse(request.body);
84+
body.q = `<@- ${body.q} -@>`;
85+
request.body = JSON.stringify(body);
86+
return request;
87+
},
88+
});
89+
90+
// No standalone searchboxes should be getting executing first search.
91+
if (el.id === 'search-v2') await el.executeFirstSearch();
8792
});
8893

89-
// No standalone searchboxes should be getting executing first search.
90-
if (el.id === 'search-v2') await el.executeFirstSearch();
91-
});
92-
93-
const headerSearchBar = document.querySelector('#search-standalone-header');
94-
if (headerSearchBar?.shadowRoot) {
95-
hideShadowElement(headerSearchBar.shadowRoot, 'atomic-relevance-inspector');
94+
const headerSearchBar = document.querySelector('#search-standalone-header');
95+
if (headerSearchBar?.shadowRoot) {
96+
hideShadowElement(
97+
headerSearchBar.shadowRoot,
98+
'atomic-relevance-inspector'
99+
);
100+
}
101+
} catch (error) {
102+
// Handle coveo error from only a LACK of credentials.
103+
// INCORRECT credentials will cause the page to load but spin waiting.
104+
const coveoErrorContainer = document.getElementById(
105+
'coveo-error-container'
106+
);
107+
coveoErrorContainer.style.display = 'block';
96108
}
97109
}
98110

layouts/search/single.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
<section class="search no-sidebar">
55
{{ partial "coveo-atomic.html" .}}
66
</section>
7+
<div class="content" data-testid="content" id="coveo-error-container" style="display: none;">
8+
<div class="not-found-container" data-testid="not-found-container">
9+
<h1 class="info-header">
10+
Search functionality is unavailable.
11+
</h1>
12+
<a href="{{ .Site.BaseURL | relLangURL }}" aria-label="Return home">Return to the {{ .Site.Title }} homepage.</a>
13+
</div>
14+
</div>
715
{{ else }}
816
<div class="content" data-testid="content">
917
<div class="not-found-container" data-testid="not-found-container">

0 commit comments

Comments
 (0)