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

Decouple Router~handleRequest from events #1682

Merged
merged 2 commits into from
Oct 8, 2018
Merged

Conversation

philipwalton
Copy link
Member

@philipwalton philipwalton commented Oct 6, 2018

R: @jeffposnick

Fixes #1630

This PR updates the routing and strategy packages in a few ways.

Breaking changes:

  1. the Router~handleRequest() method from just taking an Event object, to taking an options object where options.request is required and options.event is optional. Note that technically this change is mostly backwards compatible with the previous version since fetch events have a request property. One thing to watch out for, though, is if people were using this method directly and passing an event, no strategies or handlers would be able to call event.waitUntil(). We could support both a FetchEvent argument as well an options Object argument -- or we could just consider this a breaking change and provide upgrade instructions.

  2. The strategy classes' handle() methods no longer require an options.event parameter, and they now work essentially the same as their makeRequest methods. With this change, we don't really need two separate methods, so we may want to consider getting rid of one of them.

  3. The match and handler callbacks are now also passed an options.request in addition to options.url, and optionally options.event. I'm not sure if anyone was ever using the options.event property, but since Router~handleRequest() can now be called without an event, options.event will now sometimes not be present and so is thus documented as an optional argument.

  4. Probably more of a bug fix than a breaking change, but previous if a match callback returned a truthy string, that string would be passed as the options.params argument to the handler. I'm assuming this wasn't intentional, so I've now updated that logic to only pass options.params if the return value is a non-empty object or array.

Non-breaking changes:

  1. I've slightly refactored the Router~_findHandlerAndParams() method and renamed it to findMatchingRoute(), meaning it's now a public method. I figure it may be useful for someone who wants to use the router without executing a handler right away.
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.

Just a couple of small things, but otherwise 👍

I'm on board, I hope this makes the "pass in a list of URLs and have them handled" code you're trying to write cleaner.

* @param {URL} options.url
* @param {Request} options.request
* @param {Event} [options.event]
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove options.event from the JSDoc.

let handler = null;
let params = null;
let debugMessages = [];
let {params, route} = this.findMatchingRoute({url, request, event});
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like all these lets can be changed to const after your refactoring.

Copy link
Member Author

Choose a reason for hiding this comment

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

route gets reassigned and params doesn't, but since one of them does, in order to use destructuring I had to do both as let...

@workbox-pr-bot
Copy link
Collaborator

PR-Bot Size Plugin

Changed File Sizes

File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.51 KB 3.09 KB -12% 1.39 KB 🎉
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.12 KB 1.88 KB +68% 935 B ☠️
packages/workbox-build/build/index.js 4.02 KB 3.64 KB -9% 1.36 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.88 KB 3.12 KB -20% 1.28 KB 🎉
packages/workbox-cli/build/app.js 6.76 KB 4.64 KB -31% 1.69 KB 🎉
packages/workbox-cli/build/bin.js 2.32 KB 1.16 KB -50% 580 B 🎉
packages/workbox-core/build/workbox-core.prod.js 7.47 KB 5.98 KB -20% 2.62 KB 🎉
packages/workbox-google-analytics/build/workbox-google-analytics.prod.js 2.12 KB 1.93 KB -9% 975 B
packages/workbox-precaching/build/workbox-precaching.prod.js 5.81 KB 5.03 KB -13% 2.12 KB 🎉
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.63 KB 1.49 KB -8% 744 B
packages/workbox-routing/build/workbox-routing.prod.js 2.87 KB 2.88 KB +1% 1.29 KB
packages/workbox-strategies/build/workbox-strategies.prod.js 5.09 KB 4.74 KB -7% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.57 KB 1.38 KB -12% 680 B 🎉
packages/workbox-webpack-plugin/build/generate-sw.js 8.04 KB 5.29 KB -34% 1.84 KB 🎉
packages/workbox-webpack-plugin/build/index.js 742 B 349 B -53% 255 B 🎉
packages/workbox-webpack-plugin/build/inject-manifest.js 10.30 KB 6.91 KB -33% 2.40 KB 🎉

New Files

No new files have been added.

All File Sizes

View Table
File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.51 KB 3.09 KB -12% 1.39 KB 🎉
packages/workbox-broadcast-cache-update/build/workbox-broadcast-cache-update.prod.js 1.12 KB 1.88 KB +68% 935 B ☠️
packages/workbox-build/build/_types.js 41 B 41 B 0% 61 B
packages/workbox-build/build/index.js 4.02 KB 3.64 KB -9% 1.36 KB
packages/workbox-cache-expiration/build/workbox-cache-expiration.prod.js 3.88 KB 3.12 KB -20% 1.28 KB 🎉
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 587 B 587 B 0% 347 B
packages/workbox-cli/build/app.js 6.76 KB 4.64 KB -31% 1.69 KB 🎉
packages/workbox-cli/build/bin.js 2.32 KB 1.16 KB -50% 580 B 🎉
packages/workbox-core/build/workbox-core.prod.js 7.47 KB 5.98 KB -20% 2.62 KB 🎉
packages/workbox-google-analytics/build/workbox-google-analytics.prod.js 2.12 KB 1.93 KB -9% 975 B
packages/workbox-navigation-preload/build/workbox-navigation-preload.prod.js 660 B 660 B 0% 319 B
packages/workbox-precaching/build/workbox-precaching.prod.js 5.81 KB 5.03 KB -13% 2.12 KB 🎉
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.63 KB 1.49 KB -8% 744 B
packages/workbox-routing/build/workbox-routing.prod.js 2.87 KB 2.88 KB +1% 1.29 KB
packages/workbox-strategies/build/workbox-strategies.prod.js 5.09 KB 4.74 KB -7% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.57 KB 1.38 KB -12% 680 B 🎉
packages/workbox-sw/build/workbox-sw.js 1.50 KB 1.50 KB 0% 811 B
packages/workbox-webpack-plugin/build/generate-sw.js 8.04 KB 5.29 KB -34% 1.84 KB 🎉
packages/workbox-webpack-plugin/build/index.js 742 B 349 B -53% 255 B 🎉
packages/workbox-webpack-plugin/build/inject-manifest.js 10.30 KB 6.91 KB -33% 2.40 KB 🎉

Workbox Aggregate Size Plugin

9.43KB gzip'ed (63% of limit)
23.28KB uncompressed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 participants