kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 105 forks source link

Nested url generation #193

Closed joezappie closed 3 years ago

joezappie commented 3 years ago

I'm submitting a ...

From what I can tell this is not currently possible: I'm looking to use the URL generation for links between my modules but I plan to have a lot of modules and giving every page a unique name would require lots of prefixes to keep things unique. I'd like to propose using the dot notation to access nested urls for generation:

Example:

[
  {
    name: 'user',
    path: '/user',
    children: [
      { 
        name: 'id',
        path: '/:id'
        children: [
          {
            name: 'posts',
            path: '/posts',
          },
        ]
      },
    ]
  },
]

url('user.id.posts', { id: 3 });
# /user/3/posts

I'm actually not sure how nested URL generation works as theres not a recipe for it, but I'm assuming giving each route a unique name such as "user.posts" would be needed. Being able to use dot notation for heavily nested apps would really help keep things cleaner. To keep a consistent naming convention, even a couple layers of nested names would become very long.

[
  {
    name: 'user',
    path: '/user',
    children: [
      { 
        name: 'user.id',
        path: '/:id'
        children: [
          {
            name: 'user.id.posts
            path: '/posts',
            children: [
              {
                name: 'user.id.posts.comments
                path: '/comments',
              },
            ]
          },
        ]
      },
    ]
  },
]

Note: Just wanted to thank you guys for this library, its been super great to use and is definitely the best client side router I've used. I'd also be willing to make a donation for this feature as it would greatly make my life easier and I'm sure others could use it too.

frenzzy commented 3 years ago

Thanks for the suggestion! I think it is a good idea, but looks like we can't enable it by default with preserving backward compatibility. I tried to add tis feature as an option here: #194. If you would have a chance to take a look it would be cool.

joezappie commented 3 years ago

That was quick! I'll definitely check this out tonight after work but at first glance it looks great. I like that you've made the separator configurable and makes sense to have it not enabled by default.