Don't cache HEAD requests for local resources
Fixes #125980 Also makes all other types of requests return a 405 since they are not supported
This commit is contained in:
parent
18dbd5c039
commit
012b14606f
1 changed files with 20 additions and 4 deletions
|
@ -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':
|
||||
|
@ -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
|
||||
|
@ -204,6 +214,8 @@ async function processResourceRequest(event, requestUrl) {
|
|||
return notFound();
|
||||
}
|
||||
|
||||
const shouldTryCaching = (event.request.method === 'GET');
|
||||
|
||||
/**
|
||||
* @param {ResourceResponse} entry
|
||||
* @param {Response | undefined} cachedResponse
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in a new issue