Skip to content

Commit 9b2614d

Browse files
authored
fix(docs): Prevent "Ask Sui UI" button from disappearing after site navigation (#21940)
## How to Reproduce - Open the main page: https://docs.sui.io - Navigate to the "Guides" section by clicking the button in the navbar (will get you to https://docs.sui.io/guides) - The bug: - On the live site, the "Ask Sui AI" button disappears until the page is refreshed. - On this branch, the "Ask Sui AI" button stays visible as expected.
1 parent 5e3fbff commit 9b2614d

File tree

1 file changed

+23
-15
lines changed
  • docs/site/src/plugins/askcookbook

1 file changed

+23
-15
lines changed

docs/site/src/plugins/askcookbook/index.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
// It is going to be exposed in HTTP requests anyway, so it's fine to just hardcode it here.
5-
const COOKBOOK_PUBLIC_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjU5ODBiNDAwZTliZDQ2MzcwZDlhNzYiLCJpYXQiOjE3MTcxNDE2ODQsImV4cCI6MjAzMjcxNzY4NH0.0JCgi4bJ_f6ILFgEyYAP-KeCm1dzOKwH30tC3jEs2_A";
5+
const COOKBOOK_PUBLIC_API_KEY =
6+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjU5ODBiNDAwZTliZDQ2MzcwZDlhNzYiLCJpYXQiOjE3MTcxNDE2ODQsImV4cCI6MjAzMjcxNzY4NH0.0JCgi4bJ_f6ILFgEyYAP-KeCm1dzOKwH30tC3jEs2_A";
67

78
async function askCookbookPlugin() {
89
return {
@@ -19,26 +20,33 @@ async function askCookbookPlugin() {
1920
},
2021
`
2122
<script>
22-
function initCookbook() {
23-
try {
24-
if (window.__cookbook_script) {
25-
window.__cookbook_script.remove();
26-
}
27-
const script = document.createElement('script');
28-
script.src = 'https://cdn.jsdelivr.net/npm/@cookbookdev/docsbot/dist/standalone/index.cjs.js';
29-
script.async = true;
30-
document.body.appendChild(script);
31-
window.__cookbook_script = script;
32-
} catch (e) {
33-
console.error("Error while initializing Cookbook:", e);
23+
window.initCookbook = function initCookbook() {
24+
// It's a public API key, so it's safe to expose it here
25+
const PUBLIC_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NzU4Y2YyODM4NTcxODU5MjU0MGIyMDciLCJpYXQiOjE3MzM4NzM0NDgsImV4cCI6MjA0OTQ0OTQ0OH0.Z3cv3HuMkYq3aYZYzCKkkYuM5LI3KG-kuA0R-GaSMV4";
26+
27+
let cookbookContainer = document.getElementById("__cookbook");
28+
if (!cookbookContainer) {
29+
cookbookContainer = document.createElement("div");
30+
cookbookContainer.id = "__cookbook";
31+
cookbookContainer.dataset.apiKey = "${COOKBOOK_PUBLIC_API_KEY}";
32+
document.body.appendChild(cookbookContainer);
33+
}
34+
35+
let cookbookScript = document.getElementById("__cookbook-script");
36+
if (!cookbookScript) {
37+
cookbookScript = document.createElement("script");
38+
cookbookScript.id = "__cookbook-script";
39+
cookbookScript.src = "https://cdn.jsdelivr.net/npm/@cookbookdev/docsbot/dist/standalone/index.cjs.js";
40+
cookbookScript.async = true;
41+
document.head.appendChild(cookbookScript);
3442
}
35-
};
43+
}
3644
</script>
3745
`,
3846
],
3947
};
4048
},
4149
};
42-
};
50+
}
4351

4452
module.exports = askCookbookPlugin;

0 commit comments

Comments
 (0)