mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-26 19:44:25 +01:00
Rewrite webview urls to be more url-ish
## Problem Webview uris currently have the form: ``` https://uuid.vscode-webview.net/vscode-resource/scheme/authority/path... ``` We have this syntax because we need to be able to recover the original scheme and authority of the resource in order to load it from disk However this syntax means that absolute urls don't behave as you'd expect. For example, if you have a webview that sets a `<base>` to a document in the workspace, an absolute url `/abs/path.png` ends up being resolved to: ``` https://uuid.vscode-webview.net/abs/path.png ``` This drops the original scheme and authority, which prevents us from loading the resource ## Fix With this change, I've moved the original scheme and authority into the authority of the webview uri instead of the path: ``` https://scheme+authority.vscode-resource.uuid.vscode-webview.net/path... ``` With this change, absolute paths should correctly be resolved
This commit is contained in:
@@ -20,13 +20,11 @@ export interface WebviewInitData {
|
||||
* This is hardcoded because we never expect to actually hit it. Instead these requests
|
||||
* should always go to a service worker.
|
||||
*/
|
||||
export const webviewResourceAuthority = 'vscode-webview.net';
|
||||
export const webviewResourceBaseHost = 'vscode-webview.net';
|
||||
|
||||
export const webviewResourceOrigin = (id: string) => `https://${id}.${webviewResourceAuthority}`;
|
||||
export const webviewRootResourceAuthority = (id: string) => `vscode-resource.${id}.${webviewResourceBaseHost}`;
|
||||
|
||||
export const webviewResourceRoot = (id: string) => `${webviewResourceOrigin(id)}/vscode-resource/{{resource}}`;
|
||||
|
||||
export const webviewGenericCspSource = `https://*.${webviewResourceAuthority}`;
|
||||
export const webviewGenericCspSource = `https://*.${webviewResourceBaseHost}`;
|
||||
|
||||
/**
|
||||
* Construct a uri that can load resources inside a webview
|
||||
@@ -35,7 +33,7 @@ export const webviewGenericCspSource = `https://*.${webviewResourceAuthority}`;
|
||||
* we know where to load the resource from (remote or truly local):
|
||||
*
|
||||
* ```txt
|
||||
* scheme/resource-authority/path...
|
||||
* ${scheme}+${resource-authority}.vscode-resource.${id}.vscode-webview.net/${path}
|
||||
* ```
|
||||
*
|
||||
* @param uuid Unique id of the webview.
|
||||
@@ -59,10 +57,10 @@ export function asWebviewUri(
|
||||
});
|
||||
}
|
||||
|
||||
const uri = webviewResourceRoot(uuid)
|
||||
.replace('{{resource}}', resource.scheme + '/' + encodeURIComponent(resource.authority) + resource.path)
|
||||
.replace('{{uuid}}', uuid);
|
||||
return URI.parse(uri).with({
|
||||
return URI.from({
|
||||
scheme: Schemas.https,
|
||||
authority: `${resource.scheme}+${encodeURIComponent(resource.authority)}.${webviewRootResourceAuthority(uuid)}`,
|
||||
path: resource.path,
|
||||
fragment: resource.fragment,
|
||||
query: resource.query,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user