Skip to content
Closed
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
10 changes: 10 additions & 0 deletions webview-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import "../node_modules/@vscode/codicons/dist/codicon.css"

import { getHighlighter } from "./utils/highlighter"

// Prevent service worker registration in VSCode webview context
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could there be a race condition here? The service worker override happens after imports but before the app renders. If PostHog or other libraries attempt to register service workers during their import/initialization phase, this override would be too late. Consider moving this block to the very top of the file, before any imports.

// VSCode webviews don't support service workers and attempting to register them causes errors
if ("serviceWorker" in navigator) {
// Override the register method to prevent any service worker registration attempts
navigator.serviceWorker.register = () => {
console.warn("Service worker registration is disabled in VSCode webview context")
return Promise.reject(new Error("Service worker registration is disabled in VSCode webview"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For better compatibility, could we return a DOMException instead of a generic Error? This would match the original error type:

Suggested change
return Promise.reject(new Error("Service worker registration is disabled in VSCode webview"))
return Promise.reject(new DOMException("Service worker registration is disabled in VSCode webview", "InvalidStateError"))

}
}

// Initialize Shiki early to hide initialization latency (async)
getHighlighter().catch((error: Error) => console.error("Failed to initialize Shiki highlighter:", error))

Expand Down
6 changes: 6 additions & 0 deletions webview-ui/src/utils/TelemetryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class TelemetryClient {
capture_pageview: false,
capture_pageleave: false,
autocapture: false,
// Disable service worker to prevent registration errors in VSCode webview
disable_persistence: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this intentional? The disable_persistence is set to false, which means persistence is still enabled. Should this be true to actually disable persistence and prevent localStorage/sessionStorage operations that might trigger service worker attempts?

Suggested change
disable_persistence: false,
// Disable service worker to prevent registration errors in VSCode webview
disable_persistence: true,

disable_session_recording: true,
opt_out_capturing_by_default: false,
// Explicitly disable web vitals and other features that might use service workers
capture_performance: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be helpful to add more detailed comments explaining why each option is disabled? This could help future maintainers understand the VSCode webview limitations:

Suggested change
capture_performance: false,
// Disable features that might use service workers in VSCode webview context
disable_persistence: false, // Consider setting to true
disable_session_recording: true, // Session recording requires service workers
opt_out_capturing_by_default: false,
// Web vitals and performance metrics can trigger service worker registration
capture_performance: false,

})
} else {
TelemetryClient.telemetryEnabled = false
Expand Down
Loading