Skip to content

Commit b566b66

Browse files
committed
Fix service worker scope when there is a base path
1 parent 7389d9e commit b566b66

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ directory.
110110
Our changes include:
111111
- Add a `code-server` schema.
112112
- Allow multiple extension directories (both user and built-in).
113-
- Rewrite assets requested by the browser to use the base URL.
114-
- Modify the loader, websocket, webview, and service worker to use the URL of
115-
the page as a base (and TLS if necessary for the websocket).
113+
- Modify the loader, websocket, webview, service worker, and asset requests to
114+
use the URL of the page as a base (and TLS if necessary for the websocket).
116115
- Send client-side telemetry through the server.
117116
- Add a file prefix to ignore for temporary files created during upload.
118117
- Insert our upload service for use in editor windows and explorer.

scripts/vscode.patch

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ index 99bd930a91..319c4bd3c3 100644
6060
];
6161

6262
diff --git a/src/vs/base/browser/dom.ts b/src/vs/base/browser/dom.ts
63-
index fa12f62900..1dbaac1640 100644
63+
index fa12f62900..a4c72fee8e 100644
6464
--- a/src/vs/base/browser/dom.ts
6565
+++ b/src/vs/base/browser/dom.ts
6666
@@ -1187,6 +1187,7 @@ export function animate(fn: () => void): IDisposable {
@@ -80,7 +80,7 @@ index fa12f62900..1dbaac1640 100644
8080
// rewrite vscode-remote-uris to uris of the window location
8181
// so that they can be intercepted by the service worker
8282
- return _location.with({ path: '/vscode-resources/fetch', query: `u=${JSON.stringify(uri)}` });
83-
+ return _location.with({ path: `${basePath}/vscode-resources/${uri.fsPath}` });
83+
+ return _location.with({ path: `${basePath}/vscode-resources/fetch`, query: `u=${JSON.stringify(uri)}` });
8484
}
8585
return uri;
8686
}
@@ -715,6 +715,33 @@ index 9235c739fb..32d203eb32 100644
715715
this._register(logService.onDidChangeLogLevel(level => logLevelClient.setLevel(level)));
716716
}
717717
}
718+
diff --git a/src/vs/workbench/contrib/resources/browser/resourceServiceWorker.ts b/src/vs/workbench/contrib/resources/browser/resourceServiceWorker.ts
719+
index 622bb7889b..66dd4b0bbc 100644
720+
--- a/src/vs/workbench/contrib/resources/browser/resourceServiceWorker.ts
721+
+++ b/src/vs/workbench/contrib/resources/browser/resourceServiceWorker.ts
722+
@@ -36,7 +36,8 @@ self.addEventListener('activate', event => {
723+
//#region --- fetching/caching
724+
725+
const _cacheName = 'vscode-resources';
726+
-const _resourcePrefix = '/vscode-resources/fetch';
727+
+const rootPath = self.location.pathname.replace(/\/out\/vs\/workbench\/contrib\/resources\/browser\/resourceServiceWorkerMain.js$/, '');
728+
+const _resourcePrefix = `${rootPath}/vscode-resources/fetch`;
729+
const _pendingFetch = new Map<string, Function>();
730+
731+
self.addEventListener('message', event => {
732+
diff --git a/src/vs/workbench/contrib/resources/browser/resourceServiceWorkerClient.ts b/src/vs/workbench/contrib/resources/browser/resourceServiceWorkerClient.ts
733+
index dfda6a1cfb..44a01fb0fb 100644
734+
--- a/src/vs/workbench/contrib/resources/browser/resourceServiceWorkerClient.ts
735+
+++ b/src/vs/workbench/contrib/resources/browser/resourceServiceWorkerClient.ts
736+
@@ -24,7 +24,7 @@ const _serviceWorker = new class ServiceWorkerStarter {
737+
private _messageHandler?: (event: ExtendableMessageEvent) => void;
738+
739+
constructor() {
740+
- navigator.serviceWorker.register(ServiceWorkerStarter._url, { scope: '/' }).then(reg => {
741+
+ navigator.serviceWorker.register(ServiceWorkerStarter._url, { scope: window.location.pathname.replace(/\/+$/, '') }).then(reg => {
742+
// console.debug('SW#reg', reg);
743+
return reg.update();
744+
// }).then(() => {
718745
diff --git a/src/vs/workbench/contrib/update/electron-browser/update.contribution.ts b/src/vs/workbench/contrib/update/electron-browser/update.contribution.ts
719746
index e39fa57979..3c775c9a06 100644
720747
--- a/src/vs/workbench/contrib/update/electron-browser/update.contribution.ts

src/server.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export abstract class Server {
198198
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
199199
"Content-Type": getMediaMime(payload.filePath),
200200
...(payload.redirect ? { Location: this.withBase(request, payload.redirect) } : {}),
201-
...(request.headers["service-worker"] ? { "Service-Worker-Allowed": this.options.basePath + "/" } : {}),
201+
...(request.headers["service-worker"] ? { "Service-Worker-Allowed": this.options.basePath || "/" } : {}),
202202
...payload.headers,
203203
});
204204
response.end(payload.content);
@@ -442,7 +442,20 @@ export class MainServer extends Server {
442442
): Promise<Response> {
443443
switch (base) {
444444
case "/": return this.getRoot(request, parsedUrl);
445-
case "/vscode-resources": return this.getResource(requestPath);
445+
case "/vscode-resources":
446+
if (requestPath === "/fetch") {
447+
// For some reason VS Code encodes the = so the query doesn't parse
448+
// correctly. We'll look through what's available and try to find it.
449+
for (let value in parsedUrl.query) {
450+
if (value && typeof value === "string") {
451+
const query = querystring.parse(value);
452+
if (typeof query.u === "string") {
453+
return this.getResource(JSON.parse(query.u).path);
454+
}
455+
}
456+
}
457+
}
458+
throw new HttpError("Not found", HttpCode.NotFound);
446459
case "/webview":
447460
if (requestPath.indexOf("/vscode-resource") === 0) {
448461
return this.getResource(requestPath.replace(/^\/vscode-resource/, ""));

0 commit comments

Comments
 (0)