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 a fetchDidSucceed callback #1772

Merged
merged 3 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
25 changes: 23 additions & 2 deletions packages/workbox-core/_private/fetchWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,41 @@ 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, {
request: pluginFilteredRequest,
jeffposnick marked this conversation as resolved.
Show resolved Hide resolved
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',
};
38 changes: 38 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,43 @@ 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 finalResponse = await fetchWrapper.fetch({
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);
}

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