@@ -25,26 +25,44 @@ mermaid.init(undefined, ".lang-mermaid");
2525 * Initialize Algolia DocSearch
2626 * This function initializes DocSearch for the element with id 'docsearch'.
2727 */
28- let docSearchRetryCount = 0;
29- const MAX_DOCSEARCH_RETRIES = 50; // Maximum retry duration: 5 seconds (50 attempts * 100ms intervals)
28+ const MAX_DOCSEARCH_RETRIES = 8; // Limit the total number of retries
29+ const DOCSEARCH_INITIAL_RETRY_DELAY_MS = 200; // Initial delay before first retry
30+ const DOCSEARCH_MAX_RETRY_DELAY_MS = 5000; // Upper bound for exponential backoff delay
3031
3132 function initializeDocSearch() {
3233 console.log("initializeDocSearch called");
34+
35+ // Initialize retry counter storage on the function itself to avoid global mutable state
36+ if (typeof initializeDocSearch._retryCount === 'undefined') {
37+ initializeDocSearch._retryCount = 0;
38+ }
3339
3440 // Check if docsearch function is available
3541 if (typeof docsearch === 'undefined') {
36- docSearchRetryCount ++;
37- if (docSearchRetryCount >= MAX_DOCSEARCH_RETRIES) {
42+ initializeDocSearch._retryCount ++;
43+ if (initializeDocSearch._retryCount > MAX_DOCSEARCH_RETRIES) {
3844 console.error("DocSearch library failed to load after " + MAX_DOCSEARCH_RETRIES + " retries");
3945 return;
4046 }
41- console.log("DocSearch library not loaded yet, retrying... (attempt " + docSearchRetryCount + "/" + MAX_DOCSEARCH_RETRIES + ")");
42- setTimeout(initializeDocSearch, 100);
47+
48+ var delay = DOCSEARCH_INITIAL_RETRY_DELAY_MS * Math.pow(2, initializeDocSearch._retryCount - 1);
49+ delay = Math.min(delay, DOCSEARCH_MAX_RETRY_DELAY_MS);
50+
51+ console.log(
52+ "DocSearch library not loaded yet, retrying in " +
53+ delay +
54+ "ms (attempt " +
55+ initializeDocSearch._retryCount +
56+ "/" +
57+ MAX_DOCSEARCH_RETRIES +
58+ ")"
59+ );
60+ setTimeout(initializeDocSearch, delay);
4361 return;
4462 }
4563
4664 // Reset retry count on success
47- docSearchRetryCount = 0;
65+ initializeDocSearch._retryCount = 0;
4866
4967 try {
5068 const searchBox = document.getElementById("docsearch");
0 commit comments