Skip to content

Commit

Permalink
Adds a fetchDidSucceed callback (#1772)
Browse files Browse the repository at this point in the history
* New fetchDidSucceed callback

* Include event to fetchDidSuccess callback

* Testing fix.
  • Loading branch information
jeffposnick authored Dec 17, 2018
1 parent a6ea44a commit f9b0ae6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/workbox-core/_private/cacheWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ const matchWrapper = async ({
logger.debug(`No cached response found in '${cacheName}'.`);
}
}
for (let plugin of plugins) {

for (const plugin of plugins) {
if (pluginEvents.CACHED_RESPONSE_WILL_BE_USED in plugin) {
cachedResponse = await plugin[pluginEvents.CACHED_RESPONSE_WILL_BE_USED]
.call(plugin, {
Expand All @@ -154,6 +155,7 @@ const matchWrapper = async ({
}
}
}

return cachedResponse;
};

Expand Down
26 changes: 24 additions & 2 deletions packages/workbox-core/_private/fetchWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,42 @@ const wrappedFetch = async ({
const pluginFilteredRequest = request.clone();

try {
const fetchResponse = await fetch(request, fetchOptions);
let fetchResponse = await fetch(request, fetchOptions);
if (process.env.NODE_ENV !== 'production') {
logger.debug(`Network request for `+
`'${getFriendlyURL(request.url)}' returned a response with ` +
`status '${fetchResponse.status}'.`);
}

for (const plugin of plugins) {
if (pluginEvents.FETCH_DID_SUCCEED in plugin) {
fetchResponse = await plugin[pluginEvents.FETCH_DID_SUCCEED]
.call(plugin, {
event,
request: pluginFilteredRequest,
response: fetchResponse,
});

if (process.env.NODE_ENV !== 'production') {
if (fetchResponse) {
assert.isInstance(fetchResponse, Response, {
moduleName: 'Plugin',
funcName: pluginEvents.FETCH_DID_SUCCEED,
isReturnValueProblem: true,
});
}
}
}
}

return fetchResponse;
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.error(`Network request for `+
`'${getFriendlyURL(request.url)}' threw an error.`, error);
}

for (let plugin of failedFetchPlugins) {
for (const plugin of failedFetchPlugins) {
await plugin[pluginEvents.FETCH_DID_FAIL].call(plugin, {
error,
event,
Expand Down
1 change: 1 addition & 0 deletions packages/workbox-core/models/pluginEvents.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default {
CACHE_WILL_UPDATE: 'cacheWillUpdate',
CACHED_RESPONSE_WILL_BE_USED: 'cachedResponseWillBeUsed',
FETCH_DID_FAIL: 'fetchDidFail',
FETCH_DID_SUCCEED: 'fetchDidSucceed',
REQUEST_WILL_FETCH: 'requestWillFetch',
};
42 changes: 42 additions & 0 deletions test/workbox-core/node/_private/test-fetchWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,47 @@ describe(`workbox-core fetchWrapper`, function() {
const fetchRequest = global.fetch.args[0][0];
expect(fetchRequest.url).to.equal('/test/failingRequest/1');
});

it(`should call the fetchDidSucceed method in plugins`, async function() {
const originalRequest = new Request('/testing');

sandbox.stub(global, 'fetch').resolves(new Response('', {
headers: {
'x-count': 1,
},
}));

const fetchDidSucceed = sandbox.stub().callsFake(({response}) => {
const count = response.headers.get('x-count');
return new Response('', {
headers: {
'x-count': count + 1,
},
});
});

const event = new FetchEvent('fetch', {request: originalRequest});

const finalResponse = await fetchWrapper.fetch({
event,
request: originalRequest,
plugins: [
// Two plugins, both with the same callback.
{fetchDidSucceed},
{fetchDidSucceed},
],
});

expect(fetchDidSucceed.callCount).to.eql(2);

for (const args of fetchDidSucceed.args) {
expect(args[0].request).to.be.instanceOf(Request);
expect(args[0].response).to.be.instanceOf(Response);
expect(args[0].event).to.be.instanceOf(FetchEvent);
}

const finalCount = finalResponse.headers.get('x-count');
expect(finalCount).to.eql(3);
});
});
});

0 comments on commit f9b0ae6

Please sign in to comment.