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

Await an async cacheWillUpdate in PrecacheController.install() #2287

Merged
merged 1 commit into from
Nov 22, 2019
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
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

This comment was marked as spam.

// will be thrown.
return expectError(
() => precacheController.install({plugins}),
'bad-precaching-response'
);
});
});

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