-
Notifications
You must be signed in to change notification settings - Fork 822
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
Router.handleRequest() Proposal #721
Comments
Note that you can still have a convenience function to have the “old” behavior: const router = new Router();
router.registerFetchHandler(); The point is to give developers a choice. |
I think |
A few thoughts:
E.g. Instead of const router = new Router();
const handler = new CacheFirst({cacheName: 'images'});
const route = new RegExpRoute(/\.jpg$/, handler);
router.registerRoute({route});
self.addEventListener('fetch', (event) => {
// Oops, I don't have access to the request/responses for .jpgs here.
}); you could just not use the const handler = new CacheFirst({cacheName: 'images'});
const route = new RegExpRoute(/\.jpg$/, handler);
self.addEventListener('fetch', (event) => {
// Create a URL object to pass in to route.match()
const url = new URL(event.request.url);
event.waitUntil(async () => {
let response;
if (route.match({event, url}) {
response = await route.handle({event});
}
// You can now do whatever you want with the response before it's used.
if (response) {
await event.respondWith(response);
}
});
}); or even just use the const handler = new CacheFirst({cacheName: 'images'});
self.addEventListener('fetch', (event) => {
event.waitUntil(async () => {
let response;
if (event.request.url.endsWith('.jpg')) {
response = await handler.handle({event});
}
// You can now do whatever you want with the response before it's used.
if (response) {
await event.respondWith(response);
}
});
}); Does that address your use case? It's not something that we emphasize in the |
|
At a high level the lower level modules should be explicit and magic should be handled by workbox-sw. With that in mind, we should remove the handleFetch option and make it so that given a request, we run it through our router (with any extra's that are set up) and then resolve to the response. Using @jeffposnick how do you think this "neuters" the Router class? @jakearchibald what extra data is in the FetchEvent that might be needed? I'm probably missing something simple here. |
The various client IDs. We don't implement them yet, but we will at some point. If you want to reimplement something like appcache, you need to know which page is making the request, and if it's a navigation, which page it'll replace. |
Got you. Not useful right now necessarily, but will be in the future. Only fear - curious what you think - what happens if a developer calls event.respondWith() or other methods that mess with event for future handlers? |
I create an object that's FetchEvent-like, but not quite https://github.com/jakearchibald/sw-router/blob/master/src/index.js#L5 |
Ignore me - we pass the FetchEvent - corrected my shiz. |
Some of the value of the While my preference is to continue to support the I think we're agreed that users of
Could you clarify what you mean by that? The alternatives that I presented—using the |
Router handles the linking of Routes and Handlers, removing that functionality feels like an awful lot to lose reducing to little win. Plus the Router has the default and catch handlers which would be lost by Routes and Handlers alone. Keeping in workbox-sw is fine, I see that as a the easiest path, but I would like the lower-levels to be the most flexible. I'd personally prefer replacing the handleFetch constructor arg to a method on the class like Surma suggested and just have workbox-sw use that. This to me is the best of all worlds. |
Proposal based on some of the discussion we've had:
|
@jeffposnick I preferred Surma's suggestion of Otherwise LGTM |
Updated my proposal based on that. |
For workbox-precaching, should we add We can then seperate the skipWaiting and clientsClaim out into seperate install and activate events in workbox-sw. |
I've moved the |
From the now moved over issue:
|
That's a good point. How about |
+1 registerFetchListener. |
I know bikeshedding is cheap, but I used |
I'll aim for a PR soon to get this into the |
I wrote the docs for https://github.com/jakearchibald/sw-routes in case we ever want to consider a composable API. I'm not going to publish it or anything, was just exploring the types of patterns that could be done declaratively. |
At the moment the Router is fairly all encompassing meaning that if developers wanted to use the routing logic with logic before and / or after the routers log, they couldn't.
NOTE: This is largely a suggestion from @jakearchibalds proposal from #700 .
Add explicit "handleRequest()" method to Router class
At the moment the Router class itself adds a fetch handler.
We should alter it to take a similar approach where the "instance" of the module (i.e. the friendlier / 90% use case of the classes) creates the fetch handler and under the hood it calls a "handleRequest()" method.
So instead of in the Router code:
We'd have to do the following when using the Router directly:
The end result for most users is that they'd use an instance of the router module that would handle this on behalf of the developer (i.e. they'd never write this code).
Pros:
Cons:
The text was updated successfully, but these errors were encountered: