Skip to content

Allow some dynamic routes to match before literal routes #354

Description

@nielsenko

Problem to Solve

Given two routes:

  1. /host/**
  2. /*/assets/something

The path /host/assets/something will match route 1, since we always match literal segments first (host match host and assets/something match **).

But what if you want it to match 2?

Two natural work-arounds exists. Either

  • Change 1. to /host/app/** (will solve the issue by backtracking, since assets doesn't match app), or
  • Ensure the handler of 2 is also registered at /host/assets/something (will solve the issue, since literal match always wins over dynamic).

But both workarounds require you to change your registered routes. The first (1) requires clients to use a less pretty url like https://foo.domain.com/app (as opposed to just https://foo.domain.com) and the second (2) require setting up two routes for the same handler (duplication and not obvious).

Short of the work-arounds I don't currently see an elegant way to expose reversing the match order for some dynamic routes like /*/assets/something (so * match before host), since this is definitely not something you want in general. But the use-case is real, so tracking this.

Currently matching is NOT dependent on registration order, which is something I'm very loathe to give up, as it composes much better in large code bases, where route registration is spread out across multiple modules.

Proposal

Not sure yet

Use Case

Vanity urls for SPA combined with first-segment dynamic routes. This came up on scloud when they try to register two flutter apps on the root of different virtual hosts, while also using their dynamic config trick.

Maybe others..

Alternatives

Work arounds given above

Additional context

No response

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routingIssues related to routing.enhancementNew feature or request

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions