Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for changing the options used when calling fetch() #1340

Merged
merged 1 commit into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/workbox-strategies/CacheFirst.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ class CacheFirst {
* [workbox-core]{@link workbox.core.cacheNames}.
* @param {string} options.plugins [Plugins]{@link https://docs.google.com/document/d/1Qye_GDVNF1lzGmhBaUvbgwfBWRQDdPgwUAgsbs8jhsk/edit?usp=sharing}
* to use in conjunction with this caching strategy.
* @param {Object} options.fetchOptions Values passed along to the
* [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
* of all fetch() requests made by this strategy.
*/
constructor(options = {}) {
this._cacheName = cacheNames.getRuntimeName(options.cacheName);
this._plugins = options.plugins || [];
this._fetchOptions = options.fetchOptions || null;
}

/**
Expand Down Expand Up @@ -133,7 +137,7 @@ class CacheFirst {
async _getFromNetwork(event) {
const response = await fetchWrapper.fetch(
event.request,
null,
this._fetchOptions,
this._plugins
);

Expand Down
6 changes: 6 additions & 0 deletions packages/workbox-strategies/NetworkFirst.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class NetworkFirst {
* [workbox-core]{@link workbox.core.cacheNames}.
* @param {string} options.plugins [Plugins]{@link https://docs.google.com/document/d/1Qye_GDVNF1lzGmhBaUvbgwfBWRQDdPgwUAgsbs8jhsk/edit?usp=sharing}
* to use in conjunction with this caching strategy.
* @param {Object} options.fetchOptions Values passed along to the
* [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
* of all fetch() requests made by this strategy.
* @param {number} options.networkTimeoutSeconds If set, any network requests
* that fail to respond within the timeout will fallback to the cache.
*
Expand Down Expand Up @@ -77,6 +80,8 @@ class NetworkFirst {
});
}
}

this._fetchOptions = options.fetchOptions || null;
}

/**
Expand Down Expand Up @@ -181,6 +186,7 @@ class NetworkFirst {
try {
response = await fetchWrapper.fetch(
event.request,
this._fetchOptions,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bug in the current codebase—the plugins array was being passed as the second parameter in this invocation, instead of the third parameter.

(The joy of positional arguments...)

this._plugins
);
} catch (err) {
Expand Down
6 changes: 5 additions & 1 deletion packages/workbox-strategies/NetworkOnly.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ class NetworkOnly {
* [workbox-core]{@link workbox.core.cacheNames}.
* @param {string} options.plugins [Plugins]{@link https://docs.google.com/document/d/1Qye_GDVNF1lzGmhBaUvbgwfBWRQDdPgwUAgsbs8jhsk/edit?usp=sharing}
* to use in conjunction with this caching strategy.
* @param {Object} options.fetchOptions Values passed along to the
* [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
* of all fetch() requests made by this strategy.
*/
constructor(options = {}) {
this._cacheName = cacheNames.getRuntimeName(options.cacheName);
this._plugins = options.plugins || [];
this._fetchOptions = options.fetchOptions || null;
}

/**
Expand Down Expand Up @@ -72,7 +76,7 @@ class NetworkOnly {
try {
response = await fetchWrapper.fetch(
event.request,
null,
this._fetchOptions,
this._plugins
);
} catch (err) {
Expand Down
7 changes: 6 additions & 1 deletion packages/workbox-strategies/StaleWhileRevalidate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class StaleWhileRevalidate {
* [workbox-core]{@link workbox.core.cacheNames}.
* @param {string} options.plugins [Plugins]{@link https://docs.google.com/document/d/1Qye_GDVNF1lzGmhBaUvbgwfBWRQDdPgwUAgsbs8jhsk/edit?usp=sharing}
* to use in conjunction with this caching strategy.
* @param {Object} options.fetchOptions Values passed along to the
* [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
* of all fetch() requests made by this strategy.
*/
constructor(options = {}) {
this._cacheName = cacheNames.getRuntimeName(options.cacheName);
Expand All @@ -66,6 +69,8 @@ class StaleWhileRevalidate {
// No plugins passed in, use the default plugin.
this._plugins = [cacheOkAndOpaquePlugin];
}

this._fetchOptions = options.fetchOptions || null;
}

/**
Expand Down Expand Up @@ -134,7 +139,7 @@ class StaleWhileRevalidate {
async _getFromNetwork(event) {
const response = await fetchWrapper.fetch(
event.request,
null,
this._fetchOptions,
this._plugins
);

Expand Down
18 changes: 16 additions & 2 deletions test/workbox-strategies/node/test-CacheFirst.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe(`[workbox-strategies] CacheFirst`, function() {
expect(fetch.callCount).to.equal(0);
});

it(`should be able to cache a non-existant request to custom cache`, async function() {
it(`should be able to cache a non-existent request to custom cache`, async function() {
const cacheName = 'test-cache-name';
const request = new Request('http://example.io/test/');
const event = new FetchEvent('fetch', {request});
Expand Down Expand Up @@ -220,7 +220,7 @@ describe(`[workbox-strategies] CacheFirst`, function() {
// Wait until cache.put is finished.
await cachePromise;

// The cache should be overriden
// The cache should be overridden.
const firstCachedResponse = await cache.match(request);

await compareResponses(firstCachedResponse, fetchResponse, true);
Expand All @@ -243,4 +243,18 @@ describe(`[workbox-strategies] CacheFirst`, function() {
expect(err).to.equal(injectedError);
}
});

it(`should use the fetchOptions provided`, async function() {
const fetchOptions = {credentials: 'include'};
const cacheFirst = new CacheFirst({fetchOptions});

const fetchStub = sandbox.stub(global, 'fetch').resolves(new Response());
const request = new Request('http://example.io/test/');
const event = new FetchEvent('fetch', {request});

await cacheFirst.handle({event});

expect(fetchStub.calledOnce).to.be.true;
expect(fetchStub.calledWith(request, fetchOptions)).to.be.true;
});
});
14 changes: 14 additions & 0 deletions test/workbox-strategies/node/test-NetworkFirst.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,18 @@ describe(`[workbox-strategies] NetworkFirst`, function() {
const cachedResponse = await cache.match(request);
expect(cachedResponse).to.not.exist;
});

it(`should use the fetchOptions provided`, async function() {
const fetchOptions = {credentials: 'include'};
const networkFirst = new NetworkFirst({fetchOptions});

const fetchStub = sandbox.stub(global, 'fetch').resolves(new Response());
const request = new Request('http://example.io/test/');
const event = new FetchEvent('fetch', {request});

await networkFirst.handle({event});

expect(fetchStub.calledOnce).to.be.true;
expect(fetchStub.calledWith(request, fetchOptions)).to.be.true;
});
});
14 changes: 14 additions & 0 deletions test/workbox-strategies/node/test-NetworkOnly.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,18 @@ describe(`[workbox-strategies] NetworkOnly`, function() {
const keys = await cache.keys();
expect(keys).to.be.empty;
});

it(`should use the fetchOptions provided`, async function() {
const fetchOptions = {credentials: 'include'};
const networkOnly = new NetworkOnly({fetchOptions});

const fetchStub = sandbox.stub(global, 'fetch').resolves(new Response());
const request = new Request('http://example.io/test/');
const event = new FetchEvent('fetch', {request});

await networkOnly.handle({event});

expect(fetchStub.calledOnce).to.be.true;
expect(fetchStub.calledWith(request, fetchOptions)).to.be.true;
});
});
14 changes: 14 additions & 0 deletions test/workbox-strategies/node/test-StaleWhileRevalidate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,18 @@ describe(`[workbox-strategies] StaleWhileRevalidate`, function() {
});
expect(staleWhileRevalidate._plugins).to.equal(plugins);
});

it(`should use the fetchOptions provided`, async function() {
const fetchOptions = {credentials: 'include'};
const staleWhileRevalidate = new StaleWhileRevalidate({fetchOptions});

const fetchStub = sandbox.stub(global, 'fetch').resolves(new Response());
const request = new Request('http://example.io/test/');
const event = new FetchEvent('fetch', {request});

await staleWhileRevalidate.handle({event});

expect(fetchStub.calledOnce).to.be.true;
expect(fetchStub.calledWith(request, fetchOptions)).to.be.true;
});
});