Skip to content

Commit

Permalink
Await an async cacheWillUpdate (#2287)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored Nov 22, 2019
1 parent 8b22c15 commit f674841
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/workbox-precaching/src/PrecacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class PrecacheController {
// Use a callback if provided. It returns a truthy value if valid.
// NOTE: invoke the method on the plugin instance so the `this` context
// is correct.
cacheWillUpdatePlugin.cacheWillUpdate!({event, request, response}) :
await cacheWillUpdatePlugin.cacheWillUpdate!({event, request, response}) :
// Otherwise, default to considering any response status under 400 valid.
// This includes, by default, considering opaque responses valid.
response.status < 400;
Expand Down
30 changes: 30 additions & 0 deletions test/workbox-precaching/sw/test-PrecacheController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,36 @@ describe(`PrecacheController`, function() {
// This should succeed.
await precacheController.install({plugins});
});

it(`should properly await an async cacheWillUpdate plugin`, async function() {
sandbox.stub(fetchWrapper, 'fetch').resolves(new Response('', {
status: 203,
}));

const precacheController = new PrecacheController();
const cacheList = [
'/will-be-error.html',
];
precacheController.addToCacheList(cacheList);

const plugins = [{
cacheWillUpdate: async ({request, response}) => {
expect(request).to.exist;

if (response.status === 203) {
return null;
}
return response;
},
}];

// Assuming the async plugin function is properly await-ed, an error
// will be thrown.
return expectError(
() => precacheController.install({plugins}),
'bad-precaching-response'
);
});
});

describe(`activate()`, function() {
Expand Down

0 comments on commit f674841

Please sign in to comment.