Error loading webview: Could not register service workers

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Error loading webview: Could not register service workers

The "Error loading webview: Error: Could not register service workers: TypeError: Failed to register a ServiceWorker for scope" occurs when there is another VS Code process running in the background when you open a webview.

To solve the error, close all VS Code processes and reopen VS Code.

The error also occurs when you open a Markdown Preview window.
shell
Error loading webview: Error: Could not register service workers: TypeError: Failed to register a ServiceWorker for scope ('vscode-webview://') with script ('vscode-webview://service-worker.js?platform=electron&id=some-id&vscode-resource-origin=https%3A%2F%2Fsome-id.vscode-webview-test.com'): ServiceWorker cannot be started.

If you use Linux (e.g. Ubuntu) or macOS, open your terminal and run the following command to close the running VS Code processes.

shell
killall code

stop all running vscode processes

If you get a permissions error, rerun the command with sudo.

shell
sudo killall code

You can also run the following command to close all VS Code processes on Linux and macOS.

shell
ps -ef | grep code | awk {'print $2'} | xargs kill -9

stop all vscode processes using grep

Once you run the command, reopen VS Code and open the webview.

You can also try to close all VS Code windows and reopen a new instance.

If you use Windows:

  1. Close Visual Studio Code.

  2. Click on the search field, type Task manager and start the application.

click task manager start application

  1. In the Processes tab, right-click on Visual Studio Code and click End Task for every VS Code instance that you see.

end vscode task windows

Try to start a new VS Code instance and open a webview after closing the tasks.

If the error persists on Windows:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Issue the taskkill /F /IM code.exe command.
CMD
taskkill /F /IM code.exe

stop vscode processes with taskkill on windows

# Clear the cache if the error persists

If the error persists, clear the cache and restart VS Code.

On Windows, open CMD and use the following 2 commands to clear the cache.

The following command deletes the contents of the Cache directory.

cmd
# Windows del /F /Q /S "%APPDATA%\Code\Cache\*"

If you get prompted for confirmation, type Y and press Enter.

The following command deletes the contents of the CachedData directory.

cmd
# Windows del /F /Q /S "%APPDATA%\Code\CachedData\*"

If you get prompted, type Y and press Enter.

On macOS, open bash and run the following commands to clear the cache.

shell
# macOS rm -rf "~/Library/Application\ Support/Code/Cache/*"

If you get prompted, type y and press Enter.

The following command deletes the contents of the CachedData directory.

shell
# macOS rm -rf "~/Library/Application\ Support/Code/CachedData/*"

On Linux, open bash and run the following commands to clear the cache.

shell
# Linux rm -rf "$HOME/.config/Code/Cache/*"

If you get prompted, type y and press Enter.

The following command deletes the contents of the CachedData directory.

shell
# Linux rm -rf "$HOME/.config/Code/CachedData/*"

Close VS Code and start a new session before you try to open a webview.

# Try to open VS Code in no sandbox mode

If the error persists:

  1. Close all VS Code instances.

  2. Try to open VS Code in no sandbox mode.

shell
code --no-sandbox

If you want to open the current directory in no sandbox mode, run the following command.

shell
code . --no-sandbox

Disabling sandboxing mode might resolve the issue when dealing with the webview API.

You can read more about the VS Code bug that causes the error in this GitHub issue.

# Make sure you don't block third-party cookies

If you get the error when using GitHub CodeSpaces or in a browser environment, you have to allow third-party cookies for the specific site in the browser settings.

For example, if you need to allow third-party cookies for a site on Chrome:

  1. Click on the three dots icon at the top right corner and select Settings.

click three dots and click settings

  1. Click on Privacy and Security and then click Site settings.

click privacy and security and then click site settings

  1. Click on Cookies and site data.

click cookies and site data

  1. Make sure the Block third-party cookies and Block all cookies settings are not ticked.

make sure not all cookies are blocked

  1. Alternatively, you can enable cookies only for the specific site.

  2. Scroll down to the Sites that can always use cookies and click on the Add button.

sites that can always use cookies

  1. Add the site you want to enable cookies on.

add site you want to enable cookies on

The syntax is [*.]google.com. Make sure to replace google.com with the site on which you want to enable cookies.

enabled cookies for site

If you got the error in Firefox, try to disable Enhanced Tracking Protection:

  1. Click on the Shield icon to the left of the domain's name.

  2. Disable the Enhanced Tracking Protection feature.

disable enhanced tracking protection

# Run VS Code without elevated permissions

If the error persists, make sure to not start VS Code as an administrator or with elevated permissions.

Try to close all running instances and open a new VS Code window without elevated permissions.

# Try restarting your computer

If the error persists, try to restart your computer.

Restarting your computer will automatically close all VS Code instances.

Having multiple, glitched VS Code processes that run in the background is the main cause of the error.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.