If you're seeing this error:
Error loading webview: Error: Could not register service worker: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid state.
Don't worry! This is a known VSCode issue, not a problem with the Claude Code extension itself.
This is the most reliable solution:
-
Uninstall VSCode
- macOS: Move VSCode from Applications to Trash
- Windows: Use "Add or Remove Programs"
- Linux: Use your package manager (
sudo apt remove code)
-
Download fresh VSCode
- Go to code.visualstudio.com
- Download and install the latest version
-
Restore your setup
- Your extensions will sync automatically if you use Settings Sync
- Otherwise, reinstall your extensions
✅ The error should be completely resolved!
If you prefer not to reinstall:
-
Close VSCode completely
- Close all VSCode windows
- Make sure no VSCode processes are running
-
Clear VSCode cache On macOS:
rm -rf "$HOME/Library/Application Support/Code/Service Worker"On Linux:
rm -rf "$HOME/.config/Code/Service Worker"On Windows (PowerShell):
Remove-Item "$env:APPDATA\Code\Service Worker" -Recurse -Force
-
Restart VSCode Open VSCode normally - the error should be resolved.
If the quick fix doesn't work, try these in order:
code --no-sandboxOn macOS:
rm -rf "$HOME/Library/Application Support/Code/Cache"
rm -rf "$HOME/Library/Application Support/Code/Service Worker"On Linux:
rm -rf "$HOME/.config/Code/Cache"
rm -rf "$HOME/.config/Code/Service Worker"On Windows:
Remove-Item "$env:APPDATA\Code\Cache" -Recurse -Force
Remove-Item "$env:APPDATA\Code\Service Worker" -Recurse -Force- Open Command Palette (
Cmd+Shift+PorCtrl+Shift+P) - Type and select "Developer: Reload Window"
On macOS/Linux:
killall codeOn Windows:
- Open Task Manager
- Find all "Visual Studio Code" processes
- End each task
- Restart VSCode
This error occurs due to a VSCode internal issue where:
- VSCode automatically tries to register service workers for webview content
- Sometimes the document gets into an "invalid state" during this process
- Corrupted cache files can cause persistent problems
This affects many extensions with webviews, not just Claude Code.
To reduce the likelihood of this error:
- Update VSCode regularly - newer versions have fixes for some cases
- Clear cache occasionally - run the cache clearing command monthly
- Close VSCode properly - don't force-quit or kill the process unless necessary
If none of these solutions work:
- Check your VSCode version: Some versions (1.82.2, 1.100.3) are more prone to this issue
- Try a different workspace: Open VSCode in a different folder to test
- Report to VSCode team: This is ultimately a VSCode platform issue
- Contact us: Open an issue at github.com/codeflow-studio/claude-code-chat/issues
For developers and advanced users:
- The error originates from VSCode's webview service worker registration system
- The Claude Code extension doesn't contain any service worker code
- The issue is related to document state timing and cache corruption
- Content Security Policy restrictions may contribute to the problem
This same error has been reported for:
- Julia VSCode extension
- Jupyter notebooks
- Sourcegraph extension
- Postman extension
- Many other webview-based extensions
You're not alone - this is a widespread VSCode platform issue!