fix(serviceworker): Improve cache handling and response header management in service worker.

This commit is contained in:
2025-02-04 13:01:30 +01:00
parent cbb10d7c19
commit 569fa4fc46
3 changed files with 13 additions and 2 deletions

View File

@@ -124,7 +124,7 @@ export class CacheManager {
});
// If the response status is an error or the response is opaque, do not cache it.
if (newResponse.status > 299 || newResponse.type === 'opaque') {
if (newResponse.status > 299 || newResponse.type === 'opaque' || (newResponse.headers.get('access-control-allow-origin') === null && !matchRequest.url.startsWith(this.losslessServiceWorkerRef.serviceWindowRef.location.origin))) {
logger.log(
'error',
`NOTCACHED: not caching response for ${matchRequest.url} due to status ${newResponse.status} and type ${newResponse.type}`
@@ -155,6 +155,10 @@ export class CacheManager {
if (!headers.has('Access-Control-Allow-Origin')) {
headers.set('Access-Control-Allow-Origin', '*');
}
headers.set('Vary', 'Origin');
if (!headers.has('Access-Control-Expose-Headers')) {
headers.set('Access-Control-Expose-Headers', '*')
}
if (!headers.has('Access-Control-Allow-Methods')) {
headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}