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

Makes it easier to use custom Routes within WorkboxSW. #639

Closed
wants to merge 1 commit into from

Conversation

jeffposnick
Copy link
Contributor

R: @addyosmani @gauntface
CC: @jmtt89 @mikefowler

This is something of a straw-man PR, and feedback is welcome.

This partially addresses #385 and #636, but I'm not sure that I'd call it a "fix", so I'm inclined to leave them open.

Still, it should make it easier to accomplish what @jmtt89 was trying to do, by letting you use a method other than 'GET' when calling workboxSW.router.registerRoute().

It also provides an escape hatch granting greater flexibility by letting developers import and use Route objects created from the workbox.routing namespace, by relaxing the assertions to just check whether the object passed to registerRoute() has the properties that a Route should have, not necessarily whether it's a subclass of a specific definition of a Route object.

This PR doesn't attempt to add in a factory method for creating new Route objects exposed viaworkboxSW.router, although that's an idea that's been discussed in the past and might still be on the table.

@mikefowler
Copy link

@jeffposnick this seems like a reasonable solution to me.

What was the original/current thinking on not including Route in the public interface for an instance of WorkboxSW? I'll admit, it was confusing to see the class in the source code but have no way of accessing it.

@jeffposnick
Copy link
Contributor Author

I don't remember the original thinking—@gauntface and I had some back and forth about it, but I'm not even sure which side each of us took 😄

It's slightly awkward to expose the Route class directly, because workboxSW.router is an automatically-constructed instance of a subclass of Router.

We could expose a workboxSW.router.createRoute() factory method (though that would preclude subclassing), or we could expose the Route class statically as workboxSW.Route.

I guess my vote is for the factory method, but that can be implemented separately from this PR.

for (let item of object[parameter]) {
for (let property of expectedProperties) {
if (!(property in item)) {
throwError(message);

Choose a reason for hiding this comment

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

same as above, the message should be updated to handle the missing property

import logHelper from '../../../../lib/log-helper.js';
import normalizeHandler from './normalize-handler';

const ROUTE_INTERFACE = ['match', 'handler', 'method'];

Choose a reason for hiding this comment

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

I'm not a huge fan of having this manually defined and it being defined away from the class it represents.


for (let property of expectedProperties) {
if (!(property in object[parameter])) {
throwError(message);

Choose a reason for hiding this comment

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

Would it be worth adding the missing parameter to the message?

@gauntface
Copy link

In the early versions we originally exposed it, but developer feedback was that it caused confusion have it exposed so closely to router.

@@ -97,9 +99,9 @@ class Router extends SWRoutingRouter {
if (capture.length === 0) {
throw ErrorFactory.createError('empty-express-string');
}
route = new ExpressRoute({path: capture, handler});
route = new ExpressRoute({path: capture, handler, method});

Choose a reason for hiding this comment

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

why did you add the method?

@gauntface
Copy link

Discussed with @jeffposnick , we can move the capture argument to accept a function, this will serve as the 'match' input for a Route that will be constructed behinds the scenes.

Sorry for this wrinkle.There is a largely re-think we should probably have about how we expose these values.

@mikefowler
Copy link

@gauntface, to clarify, your proposal is to allow for the following?

workboxSW.router.registerRoute(
  
  // Capture
  ({ url, event }) => {
    return true;
  },
  
  // Handler
  ({ event }) => {
    return caches.match(event.request);
  },

); 

@jeffposnick
Copy link
Contributor Author

That's correct, @mikefowler. I'm going to create a fresh PR that takes that approach.

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.

3 participants