ReactTraining / react-history

Manage session history with React
267 stars 23 forks source link

Warn when pathname does not contain basename #19

Closed pshrmn closed 7 years ago

pshrmn commented 7 years ago

Given a history with a basename:

const history = createBrowserHistory({ basename: '/base' })

When the history gets the location from the URL, it will strip the basename off of the pathname.

if (basename)
  path = stripPrefix(path, basename)

This means that a pathname without the basename is treated the same as a pathname with the basename.

"/base/my-page" == "/my-page"

It could be argued that this problem exists only when a user is improperly creating the history (you should only create a history with a basename when you are on a page with that basename), but at the very least I think that a warning would be helpful.

warning(
  !(basename && pathname.indexOf(basename) !== 0),
  "You are attempting to use a basename on a page whose URL has no basename."
)
pshrmn commented 7 years ago

Wow, I submitted this to the wrong repo :blush: