Skip to content

IOS fix: only insert Kapa.ai script for newer versions of IOS #3803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions docusaurus.config.en.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ const config = {
async: false,
},
{
src: "https://widget.kapa.ai/kapa-widget.bundle.js",
"data-website-id": "c0b5f156-1e92-49df-8252-adacc9feb21b",
"data-project-name": "ClickHouse",
"data-project-color": "#151515",
"data-project-logo":
"https://avatars.githubusercontent.com/u/54801242?s=200&v=4",
"data-modal-disclaimer":
"This is a custom LLM for ClickHouse with access to all developer documentation, open GitHub Issues, YouTube videos, and resolved StackOverflow posts. Please note that answers are generated by AI and may not be fully accurate, so please use your best judgement.",
"data-modal-example-questions":
"How to speed up queries?,How to use materialized views?",
"data-kapa-branding-hide": "true",
src: "/docs/js/kapa_widget.js",
async: true,
defer: true, // execute after document parsing, but before firing DOMContentLoaded event
},
Expand Down
73 changes: 73 additions & 0 deletions static/js/kapa_widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function insertKapaWidget() {
// Check if user agent is iOS 16.4 or lower
function isOldiOS() {
const ua = navigator.userAgent;

// Check if it's an iOS device
const isIOS = /iPad|iPhone/.test(ua);

if (isIOS) {
// Regex to capture major version and optionally minor version.
// Examples: "OS 16_4" (major: 16, minor: 4), "OS 16" (major: 16, minor: defaults to 0)
const iosVersionMatch = ua.match(/OS (\d+)(?:_(\d+))?/);

if (iosVersionMatch) {
const majorVersion = parseInt(iosVersionMatch[1], 10);
let minorVersion = 0; // Default minor version to 0

// If minor version (group 2) was captured in the regex
if (iosVersionMatch[2]) {
minorVersion = parseInt(iosVersionMatch[2], 10);
// Fallback if parsing somehow still results in NaN (though \d+ should prevent this for a captured group)
if (isNaN(minorVersion)) {
minorVersion = 0;
}
}

// Return true if iOS version is 16.4 or lower
// (major < 16) OR (major == 16 AND minor <= 4)
return majorVersion < 16 || (majorVersion === 16 && minorVersion <= 4);
Comment on lines +27 to +29
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting 16 and 4 into named constants (e.g., MIN_SUPPORTED_MAJOR, MIN_SUPPORTED_MINOR) to improve readability and simplify future version updates.

Suggested change
// Return true if iOS version is 16.4 or lower
// (major < 16) OR (major == 16 AND minor <= 4)
return majorVersion < 16 || (majorVersion === 16 && minorVersion <= 4);
// Return true if iOS version is below the minimum supported version
// (major < MIN_SUPPORTED_MAJOR) OR (major == MIN_SUPPORTED_MAJOR AND minor <= MIN_SUPPORTED_MINOR)
return majorVersion < MIN_SUPPORTED_MAJOR || (majorVersion === MIN_SUPPORTED_MAJOR && minorVersion <= MIN_SUPPORTED_MINOR);

Copilot uses AI. Check for mistakes.

} else {
// It's an iOS device, but we couldn't parse the OS version string using the regex.
// To be safe and prevent loading the widget on a potentially old/unsupported iOS version,
// assume it IS an "old iOS" in this scenario.
console.warn('Kapa widget: iOS device detected, but OS version parsing failed. Assuming old iOS to prevent loading.');
return true; // Treat as "old" if parsing fails for an iOS device
}
}

// Not an iOS device, or parsing failed and we're not treating it as old by default (if the above 'else' was different)
return false;
}

// Only insert script if not running on older iOS as Kapa does not support it (using a look behind regex)
if (!isOldiOS()) {
const script = document.createElement('script');
script.src = 'https://widget.kapa.ai/kapa-widget.bundle.js';
script.async = true;
script.defer = true;
script.setAttribute('data-website-id', 'c0b5f156-1e92-49df-8252-adacc9feb21b');
script.setAttribute('data-project-name', 'ClickHouse');
script.setAttribute('data-project-color', '#151515');
script.setAttribute('data-project-logo', 'https://avatars.githubusercontent.com/u/54801242?s=200&v=4');
script.setAttribute('data-modal-disclaimer', 'This is a custom LLM for ClickHouse with access to all developer documentation, open GitHub Issues, YouTube videos, and resolved StackOverflow posts. Please note that answers are generated by AI and may not be fully accurate, so please use your best judgement.');
script.setAttribute('data-modal-example-questions', 'How to speed up queries?,How to use materialized views?');
script.setAttribute('data-kapa-branding-hide', 'true');

document.head.appendChild(script);
console.log('Kapa widget script added successfully');
} else {
console.log('Kapa widget not added: detected iOS 16.4 or lower');
}
}

// Run the function when DOM is fully loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', insertKapaWidget);
} else {
try {
insertKapaWidget();
} catch (e) {
console.log("An error occured while trying to load the Kapa.ai widget:", e);
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo in the log message: change occured to occurred.

Suggested change
console.log("An error occured while trying to load the Kapa.ai widget:", e);
console.log("An error occurred while trying to load the Kapa.ai widget:", e);

Copilot uses AI. Check for mistakes.

}
}