Skip to content
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

Document localResourceRoot for Webview codicons.css net::ERR_ABORTED 401 #720

Open
rolandostar opened this issue Sep 30, 2022 · 2 comments
Assignees

Comments

@rolandostar
Copy link

When following the webview-codicons-sample I found the following error was returned on the extension host console:

GET https://file+.vscode-resource.vscode-cdn.net/ [...] /node_modules/%40vscode/codicons/dist/codicon.css net::ERR_ABORTED 401

This was caused due to Serialization and WebView Options:

if (vscode.window.registerWebviewPanelSerializer) {
    // Make sure we register a serializer in activation event
    vscode.window.registerWebviewPanelSerializer(TTSConsolePanel.viewType, {
      async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state: unknown) {
        console.log(`Got state: ${state}`);
        // Reset the webview options so we use latest uri for `localResourceRoots`.
        webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
        TTSConsolePanel.revive(webviewPanel, context.extensionUri);
      },
    });
  }

According to the localResourceRoot docs, this option should by default allow resources within extension's install directory, but I've found this is only true if localResourceRoots is not set. If there's any values passed, then the extension's install directory must also be explicitly allowed like so:

export function getWebviewOptions(extensionUri: vscode.Uri): vscode.WebviewOptions {
  return {
    enableScripts: true,
    localResourceRoots: [
      vscode.Uri.joinPath(extensionUri, 'node_modules', '@vscode/codicons', 'dist'), // <-- This was missing
      vscode.Uri.joinPath(extensionUri, 'assets'),
    ],
  };
}

This behavior was difficult to trace down and I believe it should be either included on the example or mentioned in the README.

@mjbvz mjbvz self-assigned this Nov 4, 2022
@Kuro-P
Copy link

Kuro-P commented May 10, 2023

Same error in my project, really helpful, thanks.

@unrealisticguy
Copy link

saved my night, thank you, you're a true hero. @rolandostar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants