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

No longer register a fetch handler for each Router by default #752

Merged
merged 2 commits into from
Aug 18, 2017

Conversation

jeffposnick
Copy link
Contributor

R: @addyosmani @gauntface
CC: @jakearchibald @surma

Fixes #721

This changes the default behavior of Router, as discussed in the proposal.

*
* It also allows you to define a "default" handler that applies to any requests
* that don't explicitly match a `Route`, and a "catch" handler that responds
* to any requests that throw an exception while being routed.
*
* By default, the `Router` class won't register a `fetch` event handler,

Choose a reason for hiding this comment

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

Nit: Could we rephrase to:

You can use the handleRequest() method to pass a fetch event through the router and ultimately get a "Routed response" back. If you'd like this to be handled automatically for you, calling addFetchListener() will cause the Router to respond to requests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

* router.registerRoute({route: imageRoute});
* router.addFetchListener();
*/
addFetchListener() {

Choose a reason for hiding this comment

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

We should check to see if this method has already been called and if so, display a warning and return early.

Copy link
Member

Choose a reason for hiding this comment

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

Out of interest, what happens with this implementation if addFetchListener is called multiple times?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I left that bit out—we agreed on returning a boolean based on whether that call actually registered the listener, and to prevent the listener from being registered multiple times. Will adjust.

*/
addFetchListener() {
self.addEventListener('fetch', (event) => {
const responsePromise = this.handleRequest({event});

Choose a reason for hiding this comment

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

Q: Do you need to call waitUntil at all? or does a Promise that resolves to a Response cause the service worker to wait and respond correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The latter.

waitUntil is only needed if there are multiple asynchronous things that are part of separate promise chains going on inside the event handler. When there's just a single promise chain that resolves with the Response, it can be passed directly to respondWith().

}
}

/**
* This method will actually add the fetch event listener.
* Checks the incoming even.request against the registered routes, and if

Choose a reason for hiding this comment

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

Nit: minor typo event.request

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

*
* @param {FetchEvent} input.event
* @param {URL} input.url
* @return {Object} Returns an object with `handler` and `params` properties

Choose a reason for hiding this comment

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

Nit: Can drop the "set to appropriate values".

Copy link
Member

Choose a reason for hiding this comment

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

👍 Matt's suggestion. If I might suggest one further edit "Returns an object with handler and params properties. They are populated if a matching route was found or undefined otherwise".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, with @addyosmani's language.

* @param {URL} input.url
* @return {Object} Returns an object with `handler` and `params` properties
* set to appropriate values if there was a match, and set to `undefined` if
* there was not matching route.

Choose a reason for hiding this comment

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

Nit: Change "not" to "no"

Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -73,4 +49,32 @@ describe(`Test of the Router class`, function() {
expect(router._routes.get('GET')).to.have.members([getRoute1]);
expect(router._routes.get('PUT')).to.have.members([putRoute1]);
});

it(`should call self.addEventListener('fetch') when addFetchListener() is called`, function() {
const stub = sinon.stub(self.__proto__.__proto__.__proto__, 'addEventListener');

Choose a reason for hiding this comment

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

why so many proto?

Copy link
Member

Choose a reason for hiding this comment

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

This seems super strange :) I'm curious about the proto depth here, but imagine there's a good reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a comment explaining what's going on. If there's a better approach for stubbing out self.addEventListener() without triggering a mocha global leak warning, I'm definitely open, as I had to bang my head for a while before I was able to get even that to work.

Copy link

@gauntface gauntface left a comment

Choose a reason for hiding this comment

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

Only one proper change - prevent multiple callls to addEventlistener

Copy link
Member

@addyosmani addyosmani left a comment

Choose a reason for hiding this comment

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

Approved with a few nits along the way.

* register a `fetch` handler and respond to requests. This can be set to
* `false` in a development environment, to prevent the service worker
* from responding with cached responses.
* Constructs a new instance, without any registered routes.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: constructs a new Router instance

* router.registerRoute({route: imageRoute});
* router.addFetchListener();
*/
addFetchListener() {
Copy link
Member

Choose a reason for hiding this comment

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

Out of interest, what happens with this implementation if addFetchListener is called multiple times?

}
}

/**
* This method will actually add the fetch event listener.
* Checks the incoming even.request against the registered routes, and if
Copy link
Member

Choose a reason for hiding this comment

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

👍

*
* @param {FetchEvent} input.event
* @param {URL} input.url
* @return {Object} Returns an object with `handler` and `params` properties
Copy link
Member

Choose a reason for hiding this comment

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

👍 Matt's suggestion. If I might suggest one further edit "Returns an object with handler and params properties. They are populated if a matching route was found or undefined otherwise".

* @param {URL} input.url
* @return {Object} Returns an object with `handler` and `params` properties
* set to appropriate values if there was a match, and set to `undefined` if
* there was not matching route.
Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -73,4 +49,32 @@ describe(`Test of the Router class`, function() {
expect(router._routes.get('GET')).to.have.members([getRoute1]);
expect(router._routes.get('PUT')).to.have.members([putRoute1]);
});

it(`should call self.addEventListener('fetch') when addFetchListener() is called`, function() {
const stub = sinon.stub(self.__proto__.__proto__.__proto__, 'addEventListener');
Copy link
Member

Choose a reason for hiding this comment

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

This seems super strange :) I'm curious about the proto depth here, but imagine there's a good reason.

@jeffposnick jeffposnick added the Breaking Change Denotes a "major" semver change. label Aug 18, 2017
@jeffposnick
Copy link
Contributor Author

PTAL.

@gauntface gauntface merged commit f4f450a into v2 Aug 18, 2017
@gauntface gauntface deleted the router-addFetchHandler branch August 18, 2017 23:05
gauntface pushed a commit that referenced this pull request Aug 24, 2017
* Adds JSDocs for the lifecycle methods, and awaits the return of each. (#736)

* Move towards using 'workbox' over 'workbox-cli'. (#744)

* New library to support Range requests (#710)

* WIP.

* Still needs JSDocs.

* Renaming, bug fixes, JSDoc.

* Changed a few comments.

* Updated tests to use new bundling infra, allowing us to remove private exports.

* Modified a warning.

* Rename cacheWillMatch to cachedResponseWillBeUsed (#748)

* No longer register a fetch handler for each Router by default (#752)

* No longer register a fetch handler for each Router by default.

* Review feedback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Denotes a "major" semver change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants