Skip to content

Webview fails to load: Could not register service worker (VS Code platform bug) #8512

Description

@markijbema

Description

The Kilo Code webview fails to load with the following error:

Error loading webview: Error: Could not register service worker: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid state..

The webview panel is completely blank/black with no UI rendered.

Root Cause

This is a known VS Code / Chromium bug. VS Code uses an internal service worker to load resources in webviews — extensions do not register this service worker themselves. Occasionally, the Chromium engine fails to register this service worker because the document enters an invalid state before registration completes.

The root issue is tracked upstream at microsoft/vscode#125993 (open since 2021, labeled bug, upstream, chromium, webview, papercut). Multiple VS Code issues have been closed as duplicates of it:

The same error affects other webview-based extensions:

How it happens

VS Code webviews are rendered inside iframes. VS Code registers a service worker (sw.js) inside each webview iframe to intercept fetch requests and load extension resources via the vscode-resource: protocol. When the iframe document is torn down or navigated away from before the service worker registration promise resolves, Chromium throws InvalidStateError: The document is in an invalid state. This is a race condition — it can happen when:

  1. The webview panel is created and immediately hidden or disposed (e.g., switching tabs quickly)
  2. VS Code restarts or reloads while webviews are initializing
  3. The system is under memory pressure or high CPU load, causing delays in the service worker lifecycle
  4. Chromium's internal state management for service worker scopes encounters a stale or detached document

Since the service worker is required for resource loading, a failed registration means no CSS/JS can be loaded, resulting in a blank webview.

Workaround

Reloading the VS Code window (Developer: Reload Window) or closing and reopening the webview panel typically resolves the issue.

Impact

  • Users see a blank Kilo Code panel with no way to interact
  • The error toast is the only indication of what went wrong
  • No action from the extension side can prevent this — it's a VS Code platform issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions