-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: prevent service worker registration errors in VSCode webview #7234
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
Conversation
- Disable PostHog service worker features that are incompatible with VSCode webviews - Add safeguard to override navigator.serviceWorker.register to prevent any registration attempts - This fixes the "InvalidStateError: Failed to register a ServiceWorker" error Fixes #7233
|
The issue is not currently scoped; this is based on a wrong assumption. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewing my own code is like debugging in production - technically possible but morally questionable.
| capture_pageleave: false, | ||
| autocapture: false, | ||
| // Disable service worker to prevent registration errors in VSCode webview | ||
| disable_persistence: false, |
There was a problem hiding this comment.
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?
| disable_persistence: false, | |
| // Disable service worker to prevent registration errors in VSCode webview | |
| disable_persistence: true, |
|
|
||
| import { getHighlighter } from "./utils/highlighter" | ||
|
|
||
| // Prevent service worker registration in VSCode webview context |
There was a problem hiding this comment.
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.
| // 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")) |
There was a problem hiding this comment.
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:
| 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")) |
| 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, |
There was a problem hiding this comment.
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:
| 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, |
This PR fixes the service worker registration error that occurs in VSCode webviews.
Problem
The PostHog analytics library was attempting to register a service worker, which is not allowed in VSCode webview contexts. This caused the error:
Solution
navigator.serviceWorker.registerto prevent any service worker registration attemptsTesting
Fixes #7233
Important
Fixes service worker registration errors in VSCode webview by overriding registration and configuring PostHog to disable related features.
navigator.serviceWorker.registerinindex.tsxto prevent service worker registration in VSCode webview.TelemetryClient.tsto disable features that might use service workers, such as session recording and performance capturing.This description was created by
for 9c13edb. You can customize this summary. It will automatically update as commits are pushed.