Skip to content

Flexible file router API #2238

Closed
Closed

Description

@platosha

Extracted from #2146,

Hilla should provide an API around createBrowserRouter with more flexibility and additional features:

  • builtin support for protectRoutes
  • merging layouts from client side with server routes
  • allow the user to remove unused route sources in case of using client/server-side only routes

The API usage could look like this in routes.tsx:

import { RouterBuilder } from '@vaadin/hilla-file-router';
import serverRoute from 'Frontend/generated/flow/server-route.js';

const routerBuilder = new RouterBuilder()
  .withReactRoutes([
    { path: '/login', element: <Login />, handle: { title: 'Login' } },
  ]);
  .withFileRoutes(fileRoutes)
  .withServerFallback(serverRoute)
  .protect();

export const routes = routerBuilder.routes;

export default routerBuilder.build();

Activity

  1. Legioth commented on Mar 21, 2024

    @Legioth
    Member

    I don't think it can be this easy:

      .with(fileRoutes)
      .with(serverRoutes)
    

    The reason for this is that we want layouts to (by default) be "inherited" also to Flow views. If I have views/foo/$layout.tsx, then I would want a Flow view with @Route("foo/bar") to be rendered inside that layout. This in turn means that a separate /* catch-all that delegates to the server would be needed on every level in the config that has a layout. serverRoutes doesn't know which parts of fileRoutes has layouts and with doesn't know that serverRoutes should be applied in a different way compared to fileRoutes.

  2. Legioth commented on Mar 22, 2024

    @Legioth
    Member

    There's one thing that is not clear to me based on that example: what mental model should I use to understand whether '/login' will be wrapped in side views/$layout.tsx and how should I change the code if I want to make it work in the opposite way?

  3. platosha commented on Mar 25, 2024

    @platosha
    ContributorAuthor

    There's one thing that is not clear to me based on that example: what mental model should I use to understand whether '/login' will be wrapped in side views/$layout.tsx and how should I change the code if I want to make it work in the opposite way?

    Good question. I'd suggest that it is not wrapped by default, unless explicitly declared so:

    const routerBuilder = new RouterBuilder()
      .withReactRoutes([
        {
          element: <Layout />,
          children: [{ path: '/login', element: <Login />, handle: { title: 'Login' } }],
        }
      ]);

    This way creates feels less magic to me, hopefully more flexible and easier to understand.

  4. Legioth commented on Mar 25, 2024

    @Legioth
    Member

    That's with an explicitly defined <Layout /> component. How about making manually defined routes render inside views/$layout.tsx.

  5. Legioth commented on Mar 26, 2024

    @Legioth
    Member

    I think there should be some kind low-level API that allows making arbitrary changes to the data structure that will eventually be converted into a React Router configuration. It should be possible for expert users to do those kinds of changes at any point during processing (i.e. before or after any of the default steps that are shown in the current code example). All high-level API that we introduce (e.g. withFileRoutes) would then just be shorthands on top of that generic operation. The key point is just that the generic operation also needs to be exposed and documented.

    Another thought is that withServerFallback might be a weird name from a semantic point of view. The point with is just that it's a fallback that is applied in all cases when no explicitly defined route matches while still following intermediate layouts. In that way, it's a "universal" fallback but not in any way specific to "server" (except maybe some support for asynchronous resolving but that can also be seen as a universal feature).

  6. mshabarov commented on Mar 27, 2024

    @mshabarov
    Contributor

    By the way, will this new implementation of file route register an empty route always as previously? Seems like it shouldn't, so as to avoid issues like not being able to use AtRoute("") in Flow.

  7. vaadin-bot commented on Apr 10, 2024

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 24.4.0.alpha21 and is also targeting the upcoming stable 24.4.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions