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

Implement plugin and strategy changes proposal #2455

Merged
merged 3 commits into from
Apr 16, 2020
Merged

Conversation

philipwalton
Copy link
Member

R: @jeffposnick

Fixes #2446

This PR implements the proposal outlined in #2446. It should not involve any breaking changes (in theory), but I'm merging it into the v6 branch just in case.

One thing to note here is that I've moved the cacheWrapper and fetchWrapper modules from workbox-core into workbox-precaching, and I've implemented them via a custom StrategyHandler instance. While this is not ideal, it's only a temporary change since—once workbox-precaching is updated to use a custom strategy under the hood—the cacheWrapper and fetchWrapper modules can be deleted. For the moment, however, I wanted to move them out of workbox-core because I didn't want workbox-core to have a dependency on workbox-stategies.

Another note is that I've made minimal changes to the existing tests, just to make sure I wasn't breaking or missing any of the existing behavior. Once we're comfortable merging this PR, we can go back and refactor the tests as needed.

packages/workbox-core/src/types.ts Outdated Show resolved Hide resolved
}

export interface HandlerWillStartCallback {
(param: HandlerWillStartCallbackParam): Promise<void | null | undefined>;
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the significance to Promise<void | null | undefined> here?

I know the difference between null and undefined, but not sure how void comes into play. I'm also not sure why all three are possible values that the we want to support the Promise fulfilling with.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's tricky and I'm honestly not sure how best to solve this (I think it's a TypeScript bug/quirk).

void is recommended to only be used with functions (to denote no return value), but in the case of async functions that doesn't return a value, you have to use Promise<void>. However, as soon as you have any other code that stores the return value of an async function like this, you need to account for some functions explicitly returning undefined and some functions not having a return statement.

In the case of third-party plugin code, I think we need to support all three...

(Let me know if you have any thoughts on how to improve this.)

Copy link
Contributor

Choose a reason for hiding this comment

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

In that case, I wonder whether letting folks explicitly return undefined or null is necessary vs. just requiring no return statement by using Promise<void>.

In other words, why do we want folks to be able to explicitly return undefined or null from their async callback function?

packages/workbox-strategies/src/CacheFirst.ts Show resolved Hide resolved
packages/workbox-strategies/src/Strategy.ts Outdated Show resolved Hide resolved
packages/workbox-strategies/src/Strategy.ts Outdated Show resolved Hide resolved
test/workbox-strategies/sw/test-Strategy.mjs Outdated Show resolved Hide resolved
test/workbox-strategies/sw/test-StrategyHandler.mjs Outdated Show resolved Hide resolved
@philipwalton
Copy link
Member Author

OK, I made a few updates based on your feedback, and I rebased against master to get the Node 12 changes.

Copy link
Contributor

@jeffposnick jeffposnick left a comment

Choose a reason for hiding this comment

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

I'm excited for these changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants