Skip to content

Commit 12e6084

Browse files
committed
Add caching
1 parent 4400045 commit 12e6084

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

scripts/vscode.patch

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,17 @@ index 1d9a0b8308..d8204187c6 100644
151151
</head>
152152

153153
diff --git a/src/vs/code/browser/workbench/workbench.js b/src/vs/code/browser/workbench/workbench.js
154-
index 2f09f53e43..ca969f19f6 100644
154+
index 2f09f53e43..0f5eef1c9e 100644
155155
--- a/src/vs/code/browser/workbench/workbench.js
156156
+++ b/src/vs/code/browser/workbench/workbench.js
157-
@@ -8,24 +8,52 @@
157+
@@ -8,24 +8,53 @@
158158

159159
(function () {
160160

161161
+ const basePath = window.location.pathname.replace(/\/+$/, '');
162162
+ const base = window.location.origin + basePath;
163+
+ const options = JSON.parse(document.getElementById('vscode-workbench-web-configuration').getAttribute('data-settings'));
164+
+ options.webviewEndpoint = `${base}/webview/`;
163165
+ let nlsConfig;
164166
+ try {
165167
+ nlsConfig = JSON.parse(document.getElementById('vscode-remote-nls-configuration').getAttribute('data-settings'));
@@ -187,9 +189,10 @@ index 2f09f53e43..ca969f19f6 100644
187189
/** @type any */
188190
const amdLoader = require;
189191

192+
+ const staticBase = base + `/static${options.productConfiguration && options.productConfiguration.commit ? `-${options.productConfiguration.commit}` : ''}`;
190193
amdLoader.config({
191194
- baseUrl: `${window.location.origin}/static/out`,
192-
+ baseUrl: `${base}/static/out`,
195+
+ baseUrl: `${staticBase}/out`,
193196
paths: {
194197
- 'vscode-textmate': `${window.location.origin}/static/node_modules/vscode-textmate/release/main`,
195198
- 'onigasm-umd': `${window.location.origin}/static/node_modules/onigasm-umd/release/main`,
@@ -199,20 +202,19 @@ index 2f09f53e43..ca969f19f6 100644
199202
- 'semver-umd': `${window.location.origin}/static/node_modules/semver-umd/lib/semver-umd.js`,
200203
- '@microsoft/applicationinsights-web': `${window.location.origin}/static/node_modules/@microsoft/applicationinsights-web/dist/applicationinsights-web.js`,
201204
- }
202-
+ 'vscode-textmate': `${base}/static/node_modules/vscode-textmate/release/main`,
203-
+ 'onigasm-umd': `${base}/static/node_modules/onigasm-umd/release/main`,
204-
+ 'xterm': `${base}/static/node_modules/xterm/lib/xterm.js`,
205-
+ 'xterm-addon-search': `${base}/static/node_modules/xterm-addon-search/lib/xterm-addon-search.js`,
206-
+ 'xterm-addon-web-links': `${base}/static/node_modules/xterm-addon-web-links/lib/xterm-addon-web-links.js`,
207-
+ 'semver-umd': `${base}/static/node_modules/semver-umd/lib/semver-umd.js`,
208-
+ '@microsoft/applicationinsights-web': `${base}/static/node_modules/@microsoft/applicationinsights-web/dist/applicationinsights-web.js`,
205+
+ 'vscode-textmate': `${staticBase}/node_modules/vscode-textmate/release/main`,
206+
+ 'onigasm-umd': `${staticBase}/node_modules/onigasm-umd/release/main`,
207+
+ 'xterm': `${staticBase}/node_modules/xterm/lib/xterm.js`,
208+
+ 'xterm-addon-search': `${staticBase}/node_modules/xterm-addon-search/lib/xterm-addon-search.js`,
209+
+ 'xterm-addon-web-links': `${staticBase}/node_modules/xterm-addon-web-links/lib/xterm-addon-web-links.js`,
210+
+ 'semver-umd': `${staticBase}/node_modules/semver-umd/lib/semver-umd.js`,
211+
+ '@microsoft/applicationinsights-web': `${staticBase}/node_modules/@microsoft/applicationinsights-web/dist/applicationinsights-web.js`,
209212
+ },
210213
+ 'vs/nls': nlsConfig
211214
});
212215

213216
amdLoader(['vs/workbench/workbench.web.api'], function (api) {
214-
const options = JSON.parse(document.getElementById('vscode-workbench-web-configuration').getAttribute('data-settings'));
215-
+ options.webviewEndpoint = `${base}/webview/`;
217+
- const options = JSON.parse(document.getElementById('vscode-workbench-web-configuration').getAttribute('data-settings'));
216218
api.create(document.body, options);
217219
});
218220
})();

src/server.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface Options {
8484
}
8585

8686
export interface Response {
87+
cache?: boolean;
8788
code?: number;
8889
content?: string | Buffer;
8990
filePath?: string;
@@ -207,12 +208,13 @@ export abstract class Server {
207208

208209
private onRequest = async (request: http.IncomingMessage, response: http.ServerResponse): Promise<void> => {
209210
try {
210-
const payload = await this.preHandleRequest(request);
211+
const parsedUrl = request.url ? url.parse(request.url, true) : { query: {}};
212+
const payload = await this.preHandleRequest(request, parsedUrl);
211213
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
212-
// "Cache-Control": "public, max-age=31536000",
213214
"Content-Type": getMediaMime(payload.filePath),
214215
...(payload.redirect ? { Location: this.withBase(request, payload.redirect) } : {}),
215216
...(request.headers["service-worker"] ? { "Service-Worker-Allowed": this.options.basePath || "/" } : {}),
217+
...(payload.cache ? { "Cache-Control": "public, max-age=31536000" } : {}),
216218
...payload.headers,
217219
});
218220
response.end(payload.content);
@@ -225,13 +227,12 @@ export abstract class Server {
225227
}
226228
}
227229

228-
private async preHandleRequest(request: http.IncomingMessage): Promise<Response> {
230+
private async preHandleRequest(request: http.IncomingMessage, parsedUrl: url.UrlWithParsedQuery): Promise<Response> {
229231
const secure = (request.connection as tls.TLSSocket).encrypted;
230232
if (this.options.cert && !secure) {
231233
return { redirect: request.url };
232234
}
233235

234-
const parsedUrl = request.url ? url.parse(request.url, true) : { query: {}};
235236
const fullPath = decodeURIComponent(parsedUrl.pathname || "/");
236237
const match = fullPath.match(/^(\/?[^/]*)(.*)$/);
237238
let [/* ignore */, base, requestPath] = match
@@ -250,19 +251,32 @@ export abstract class Server {
250251
this.ensureGet(request);
251252
}
252253

254+
// Allow for a versioned static endpoint. This lets us cache every static
255+
// resource underneath the path based on the version without any work and
256+
// without adding query parameters which have their own issues.
257+
// REVIEW: Discuss whether this is the best option; this is sort of a quick
258+
// hack almost to get caching in the meantime but it does work pretty well.
259+
if (/static-.+/.test(base)) {
260+
base = "/static";
261+
}
262+
253263
switch (base) {
254264
case "/":
255265
switch (requestPath) {
256266
case "/favicon.ico":
257267
case "/manifest.json":
258-
return this.getResource(this.serverRoot, "media", requestPath);
268+
const response = await this.getResource(this.serverRoot, "media", requestPath);
269+
response.cache = true;
270+
return response;
259271
}
260272
if (!this.authenticate(request)) {
261273
return { redirect: "/login" };
262274
}
263275
break;
264276
case "/static":
265-
return this.getResource(this.rootPath, requestPath);
277+
const response = await this.getResource(this.rootPath, requestPath);
278+
response.cache = true;
279+
return response;
266280
case "/login":
267281
if (!this.options.auth || requestPath !== "/index.html") {
268282
throw new HttpError("Not found", HttpCode.NotFound);
@@ -514,10 +528,10 @@ export class MainServer extends Server {
514528
NLS_CONFIGURATION: await getNlsConfiguration(locale, environment.userDataPath),
515529
};
516530

531+
content = content.replace(/\/static\//g, `/static${product.commit ? `-${product.commit}` : ""}/`).replace("{{WEBVIEW_ENDPOINT}}", "");
517532
for (const key in options) {
518533
content = content.replace(`"{{${key}}}"`, `'${JSON.stringify(options[key as keyof Options])}'`);
519534
}
520-
content = content.replace("{{WEBVIEW_ENDPOINT}}", "");
521535

522536
return { content, filePath };
523537
}

0 commit comments

Comments
 (0)