Search code examples
lit

How come Lit Labs Router `goto` method isn't updating URL in address bar?


I have a call like this:

this._router.goto(`http://localhost:8000/${this.nav.selected}`);

When I do relative, the page updates without the address bar. When I do a full URL I get errors. The docs say goto "takes full URLs for top-level navigation and relative URLs for navigation within a nested route space." So I am guessing the URL doesn't update on purpose for relative, but I can't seem to get a full URL to work.

Setting the router option with path: http://localhost:8000/home will give this error:

TypeError: Failed to construct 'URLPattern': Invalid pathname pattern 'http://localhost:8000/home'. Missing parameter name at index 4.

I also tried pattern rather than path:

pattern: new URLPattern({
        pathname: '/home',
        hostname: 'localhost',
        port: '8000',
      }),

That yields: Error: No route found for /home

When I tried adding http:// to hostname, as some MDN samples suggest, it had a different error.

Not sure of the right combination here.


Solution

  • It appears goto is not meant to update URL. Lit Router has some logic for when an actual link is clicked.

    https://github.com/lit/lit/blob/f6736088fc03ebdb00175487907ca1c597f1b09c/packages/labs/router/src/router.ts#L70