Skip to content

useNavigate, and probably every other navigation feature, discards the base path. #4427

Closed
@adnanbrq

Description

@adnanbrq

Which project does this relate to?

Router

Describe the bug

There's a strange error where useNavigate generates the wrong URL because it does not consider the base path defined in the router.

I don't know the exact version where that behavior was introduced, but I guess it's from 1.120.2 or later.

Your Example Website or App

stackblitz.com/edit/github-lafdjhpd

Steps to Reproduce the Bug or Issue

  1. Set the basepath to something like this
    basepath: '/app',

  2. Create a basic route like this

import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/test')({
  component: RouteComponent,
});

function RouteComponent() {
  const nav = Route.useNavigate();
  const option = Route.useSearch() as { value: string | undefined };

  return (
    <div>
      <select
        value={option.value ?? undefined}
        onChange={(e) => nav({ search: { value: e.currentTarget.value } })}
      >
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
      </select>
    </div>
  );
}
  1. Start the server and go to that specific route. In my case, the URL looked like:
    http://localhost:4321/app/test

  2. Select an option, and you will end up on the following URL:
    http://localhost:4321/test?value=...

As you can tell, the base path "/app" is no longer there.

Expected behavior

I expect the base path to be used when generating new routes, as this behavior breaks the application.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Safari
  • Version: 18.5 (20621.2.5.11.8)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions