diff --git a/infra/testing/sw-env-mocks/Request.js b/infra/testing/sw-env-mocks/Request.js index 0967c6d45..82131802f 100644 --- a/infra/testing/sw-env-mocks/Request.js +++ b/infra/testing/sw-env-mocks/Request.js @@ -33,6 +33,9 @@ class Request { this.url = url; this.method = options.method || 'GET'; this.mode = options.mode || 'cors'; + // See https://fetch.spec.whatwg.org/#concept-request-credentials-mode + this.credentials = options.credentials || (this.mode === 'navigate' ? + 'include' : 'omit'); this.headers = new Headers(options.headers); this._body = new Blob('body' in options ? [options.body] : []); diff --git a/packages/workbox-precaching/models/PrecacheEntry.mjs b/packages/workbox-precaching/models/PrecacheEntry.mjs index b1ba034bd..8138edfaa 100644 --- a/packages/workbox-precaching/models/PrecacheEntry.mjs +++ b/packages/workbox-precaching/models/PrecacheEntry.mjs @@ -36,7 +36,7 @@ export default class PrecacheEntry { this._originalInput = originalInput; this._entryId = url; this._revision = revision; - const requestAsCacheKey = new Request(url); + const requestAsCacheKey = new Request(url, {credentials: 'same-origin'}); this._cacheRequest = requestAsCacheKey; this._networkRequest = shouldCacheBust ? this._cacheBustRequest(requestAsCacheKey) : requestAsCacheKey; @@ -53,7 +53,9 @@ export default class PrecacheEntry { */ _cacheBustRequest(request) { let url = request.url; - const requestOptions = {}; + const requestOptions = { + credentials: 'same-origin', + }; if ('cache' in Request.prototype) { // Make use of the Request cache mode where we can. // Reload skips the HTTP cache for outgoing requests and updates diff --git a/test/workbox-background-sync/node/test-Queue.mjs b/test/workbox-background-sync/node/test-Queue.mjs index ddacfc6f9..b6594dc7b 100644 --- a/test/workbox-background-sync/node/test-Queue.mjs +++ b/test/workbox-background-sync/node/test-Queue.mjs @@ -128,6 +128,7 @@ describe(`[workbox-background-sync] Queue`, function() { 'body', 'headers', 'mode', + 'credentials', ]); }); diff --git a/test/workbox-precaching/node/controllers/test-PrecacheController.mjs b/test/workbox-precaching/node/controllers/test-PrecacheController.mjs index d93de7afe..c3abe2aa2 100644 --- a/test/workbox-precaching/node/controllers/test-PrecacheController.mjs +++ b/test/workbox-precaching/node/controllers/test-PrecacheController.mjs @@ -408,6 +408,21 @@ describe(`[workbox-precaching] PrecacheController`, function() { expect(fetchWrapper.fetch.args[0][2]).to.equal(testPlugins); expect(cacheWrapper.put.args[0][3]).to.equal(testPlugins); }); + + it(`it should set credentials: 'same-origin' on the precaching requests`, async function() { + sandbox.spy(fetchWrapper, 'fetch'); + + const precacheController = new PrecacheController(); + const cacheList = [ + '/index.1234.html', + ]; + precacheController.addToCacheList(cacheList); + + await precacheController.install(); + + const request = fetchWrapper.fetch.args[0][0]; + expect(request.credentials).to.eql('same-origin'); + }); }); describe(`cleanup()`, function() {