Skip to content
Source
Sign inSign up
TanStack/router

when basepath exactly matches a route in the tree, route matching fails #2023

Send to my agent
ClosedSeanCassiere opened this issue 2 comments
SeanCassierecommented

Describe the bug

From #2019

When the basepath matches a route path in the tree, route matching fails.

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-jj7148?file=src%2Froutes%2F_layout%2Findex.tsx

Steps to Reproduce the Bug or Issue

  1. Create a src/routes/products.tsx
  2. Set the basepath to /products.
  3. Try to even navigate the page 😬.

Expected behavior

Should work

Screenshots or Videos

No response

Platform

TSR: 1.43.10

Additional context

No response

  1. SeanCassiere added labelsbug

  2. After a discussion with Manuel, I'm closing this for now.

    It is quite a niche scenario in which you'd have the basepath be the same as a normal route (like /posts/posts).

    In the future, should we decide to go forward with this, this test in link.test.tsx should be useful for validating the runtime behaviour.

      test('when navigating to /posts with a base url of /posts', async () => {
        const rootRoute = createRootRoute()
        const indexRoute = createRoute({
          getParentRoute: () => rootRoute,
          path: '/',
          component: () => {
            return (
              <>
                <h1>Index</h1>
                <Link to="/">Index</Link>
                <Link to="/posts">Posts</Link>
              </>
            )
          },
        })
    
        const postsRoute = createRoute({
          getParentRoute: () => rootRoute,
          path: '/posts',
          component: () => {
            return (
              <>
                <h1>Posts</h1>
                <Link to="/">Index</Link>
                <Link to="/posts" activeProps={{ className: 'active' }}>
                  Posts
                </Link>
              </>
            )
          },
        })
    
        const router = createRouter({
          routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
          basepath: '/posts',
        })
    
        render(<RouterProvider router={router} />)
    
        const postsLink = await screen.findByRole('link', { name: 'Posts' })
        expect(postsLink).toHaveAttribute('href', '/posts/posts')
    
        fireEvent.click(postsLink)
    
        const postsHeading = await screen.findByRole('heading', { name: 'Posts' })
        expect(postsHeading).toBeInTheDocument()
    
        const indexLink = await screen.findByRole('link', { name: 'Index' })
    
        expect(window.location.pathname).toBe('/posts/posts')
        expect(indexLink).not.toHaveAttribute('aria-current', 'page')
        expect(indexLink).not.toHaveAttribute('data-status', 'active')
        expect(indexLink).toHaveAttribute('href', '/posts/')
    
        expect(postsLink).toHaveAttribute('data-status', 'active')
        expect(postsLink).toHaveAttribute('aria-current', 'page')
        expect(postsLink).toHaveClass('active')
        expect(postsLink).toHaveAttribute('href', '/posts/posts')
      })
  3. SeanCassiere closed this

  4. ghostcommented

    Is there any temporary workaround? In my app, the basepath string varies depending on the user, so it may match with subsequent segments.

    It seems that prefixing all destination paths with the basepath might cause issues with type definitions.

Sign in to join this conversation.