jamesddavies / lit-route

A client-side router for lit-html applications.
8 stars 2 forks source link

DefaultRoute "always" loaded when Route with match is used #4

Closed pboissonneault closed 3 years ago

pboissonneault commented 4 years ago

The DefaultRoute is loaded even when a route exists. This only happens when a route with params is used.

Ex. :

const Router = () => html`
    ${new Route('/', () => Home(), true).mount()}
    ${new Route('/thisone/works', () => Works()).mount()}
    ${new Route('/messages/:lang', (match) => Messages(match)).mount()}
    ${new DefaultRoute(() => Butts()).mount()}

Home and Works are loaded as expected. When the route /messages/:lang is loaded, it works, but the DefaultRoute is loaded too.

pboissonneault commented 4 years ago

What I've done to fix it :

    routeExists() {
        return this.routes.filter((route) => {
            if(route.path !== path) {
                let match = matchPath(path, {exact: false, path: route.path});
                return (match !== null) && (Object.keys(match.params).length > 0)
            }
            return (route.path === path);
        }).length > 0;
    }

It seems already fixed in master, but the npm package is v.0.4.3 but it's different from this repo.

jamesddavies commented 4 years ago

Hi mate, I think this issue came up previously - are you saying it's fixed on the master branch, but that fix isn't present in the npm package?

pboissonneault commented 4 years ago

Exactly