Skip to content

Make Route components contribute their path contextually, like Router components #30

@webJose

Description

@webJose

Motivation

Currently, when a Route component is declared to be a match, the matching path is not implicitly available for inner routers or routes. In other words: Even though we can confidently state that the URL is there (because the route matched), one has to repeat the path segments that we know already matched in routers or routes that are found in the matched Route component's children. This seems to be redundant work for the developer.

An example:

<Router basePath={import.meta.ENV.BASE_URL}>
  <Route path="/admin" and={isAdmin()}>
    <!-- The internal admin router:  Must repeat the path (/admin) -->
    <Router id="redundant" path="/admin">
      ...
    </Router>
    <!-- Much nicer would be to simply type: -->
    <Router id="inheriting">
      ...
    </Router>
  </Route>
</Router>

Technical Requirements

  1. Currently, the RouterEngine class takes care of path inheritance. It needs to be decoupled.
  2. The decoupled solution must be available through Svelte's context mechanism.
  3. Only Route components with a string path property value can automagically contribute.
  4. Route components without path cannot contribute.
  5. Route components with a rest parameter will contribute excluding the rest of the path.

As bonus, Route components that have its path specified via a regular expression should accept a path provider property that accepts a function that returns the path to be contributed contextually. Something like this, perhaps:

<script lang="ts">
  function calculatePathContext(routeCtx: TypeToBeDefined) {
    return `${routeCtx.basePath}/users/${routeCtx.params.userId}`
  }
</script>

<Route path={/users\/(?<userId>\d+)/i} pathCtx={calculatePathContext}>...</Route>

Basically, allow a function to provide a calculation of the path segments to be contributed.

If this bonus is added, then the technical requirement would be to always use pathCtx if provided, and otherwise fall back to the requirements listed above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Blocked!A block (internal or external) exists that prevent the item from advancing.Priority: MediumMedium priority. Upvote to request a higher priority. Downvote to attempt to lower priority.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions