atellmer / dark

The lightweight and powerful UI rendering engine without dependencies and written in TypeScript💫 (Browser, Node.js, Android, iOS, Windows, Linux, macOS)
MIT License
41 stars 1 forks source link

web-router trailing slashes #48

Closed einar-hjortdal closed 5 months ago

einar-hjortdal commented 5 months ago

I am confused by how route paths work. I define routes without trailing slashes

  {
    path: '/contact',
    pathMatch: 'full',
    component: lazy(() => import('../pages/Contact')),
  },

When I navigate to /contact the path matches the route and it renders the Contact component. When I navigate to /contact/ is also matches. On /contact, useMatch().path returns /contact/. <RouterLink to='/contact'>Contact</RouterLink> bring me to /contact/.

This made me think that routes should always have trailing slashes, but this was a mistake.

  {
    path: '/contact/',
    pathMatch: 'full',
    component: lazy(() => import('../pages/Contact')),
  },

When I navigate to /contact the path does not match.

Is there a way to:

Trailing slashes are unusual to see. Right now I can see https://github.com/atellmer/dark/issues/new in this tab, not https://github.com/atellmer/dark/issues/new/

P.S. is this router based on another router project? I am curious

atellmer commented 5 months ago

The router specifically adds slashes at the end to bring all URLs to a single standard. Because in certain situations it becomes difficult to match many variations of url spelling. For now it just works that way. There are no workarounds.

P.S. is this router based on another router project? I am curious

No. it's fully new project, but inspired angular router API

einar-hjortdal commented 5 months ago

I stand by these proposed changes:

  1. Make RouterLink navigate to routes without trailing slashes by default: <RouterLink to='/foo'> navigates to /foo
  2. Redirect trailing slash'ed paths to paths without trailing slashes: /foo/ becomes /foo
  3. Make routes defined as /foo/ match the path /foo too: path: '/foo/' matches /foo

Point 1 and 2 is the behavior I consider normal and is reflected by the behavior of the majority of the web. Point 3 I think would offer more consistency.