routerservice-workersw-toolbox

How does sw-toolbox router rule order/priority work?


In my service worker (which uses sw-toolbox library) I have setup two routes as follows:

toolbox.router.any("/user/*", toolbox.networkOnly);
toolbox.router.any("/user/logout", toolbox.logoutHandler);

I assumed that the second rule which is specific to the "/user/logout" path, would act as an exception to the first rule (which is a blanket rule for the path "/user/*") however, I can confirm that it does not.

Am I using this sw-toolbox route config correctly?


Solution

  • I think the rules are independent, first matching rule wins. So this should works:

    toolbox.router.any("/user/logout", toolbox.logoutHandler);
    toolbox.router.any("/user/*", toolbox.networkOnly);
    

    See Jeff's comment on this issues: "The routing to handlers should match in the order they're registered"