vaadin / router

Small and powerful client-side router for Web Components. Framework-agnostic.
https://vaadin.com/router
Apache License 2.0
423 stars 51 forks source link

Vaadin router should follow standard navigation if it cannot find a route #716

Open pietrorea opened 2 years ago

pietrorea commented 2 years ago

I'm working in a situation where there are multiple frontend single page apps (e.g. auth, admin, app) and I'm having to link from one to another pretty often but it's very cumbersome. With the existing setup...

i) I can't set up a default route (e.g. { path: '(.*)', component: 'not-found-page'}) because if I try to link to another SPA or any other external resource, it will get caught the not-found-page component and that's not what I want. ii) I can't count on Vaadin to fall back on standard navigation if there isn't a route that matches. I get a Error: [Vaadin.Router] Page not found.

Ideally, I don't want to keep track of which is an internal (internal to this SPA) link and which isn't and do something different for each link. I just want to configure the router and use it for all my routing needs. The best solution I have so far is a default route with a custom action:

  { path: '(.*)', action: (context, commands) => {
    window.location.assign(context.pathname);
    commands.prevent();
  }}

I don't 100% like this solution because I have to remember to do this in all my frontend SPAs. I would prefer if this behavior were a RouterOption I could pass when first setting up the router.

platosha commented 2 years ago

Hi @pietrorea, thanks for filing this. Good idea for an enhancement, I think.

Ideally, the router’s path matching was sync, we could avoid event.preventDefault() for the click event if the path matching did not find a good route, so that the browser handling continues naturally handling the link click with standard navigation (not possible today as the path matching is async).

Sounds good to make it an opt-in behaviour also.

Related tip, if each Vaadin router in your case is only handling a specific path-prefixed scope, consider using the baseUrl option. This prevents Vaadin router from handling link clicks outside the path-prefixed scope (e. g., for <a href="/admin">Admin app</a> link it would use standard navigation if the router’s baseUrl is set to /app).