-
Notifications
You must be signed in to change notification settings - Fork 830
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
Conversation
* | ||
* 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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why so many proto?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
There was a problem hiding this 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. |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
PTAL. |
* 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.
R: @addyosmani @gauntface
CC: @jakearchibald @surma
Fixes #721
This changes the default behavior of
Router
, as discussed in the proposal.