Skip to content

Commit

Permalink
Don't cache HEAD requests for local resources
Browse files Browse the repository at this point in the history
Fixes #125980

Also makes all other types of requests return a 405 since they are not supported
  • Loading branch information
mjbvz committed Jun 11, 2021
1 parent 18dbd5c commit 012b146
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/vs/workbench/contrib/webview/browser/pre/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ const localhostRequestStore = new RequestStore();
const notFound = () =>
new Response('Not Found', { status: 404, });

const methodNotAllowed = () =>
new Response('Method Not Allowed', { status: 405, });

sw.addEventListener('message', async (event) => {
switch (event.data.channel) {
case 'version':
Expand Down Expand Up @@ -170,7 +173,14 @@ sw.addEventListener('message', async (event) => {
sw.addEventListener('fetch', (event) => {
const requestUrl = new URL(event.request.url);
if (requestUrl.protocol === 'https:' && requestUrl.hostname.endsWith('.' + resourceBaseAuthority)) {
return event.respondWith(processResourceRequest(event, requestUrl));
switch (event.request.method) {
case 'GET':
case 'HEAD':
return event.respondWith(processResourceRequest(event, requestUrl));

default:
return event.respondWith(methodNotAllowed());
}
}

// See if it's a localhost request
Expand Down Expand Up @@ -204,6 +214,8 @@ async function processResourceRequest(event, requestUrl) {
return notFound();
}

const shouldTryCaching = (event.request.method === 'GET');

/**
* @param {ResourceResponse} entry
* @param {Response | undefined} cachedResponse
Expand Down Expand Up @@ -235,7 +247,7 @@ async function processResourceRequest(event, requestUrl) {
headers
});

if (entry.etag) {
if (shouldTryCaching && entry.etag) {
caches.open(resourceCacheName).then(cache => {
return cache.put(event.request, response);
});
Expand All @@ -249,8 +261,12 @@ async function processResourceRequest(event, requestUrl) {
return notFound();
}

const cache = await caches.open(resourceCacheName);
const cached = await cache.match(event.request);
/** @type {Response | undefined} */
let cached;
if (shouldTryCaching) {
const cache = await caches.open(resourceCacheName);
cached = await cache.match(event.request);
}

const { requestId, promise } = resourceRequestStore.create();

Expand Down

0 comments on commit 012b146

Please sign in to comment.