Js Noob
Js Noob

Reputation: 21

React Router with a base path

I would like to add "/app" as my base path for all routes in react routes. So I am trying -

.... more routes

I am unable to make webpack dev server serve pages with URL localhost:8080/app. It gives me a "Cannot get /app" error. If I try localhost:8080/ - it gives me an error that it cannot match a route with "/".

What should be a basic webpack dev server configuration for this scenario?

Upvotes: 2

Views: 845

Answers (1)

Sasha Chedygov
Sasha Chedygov

Reputation: 130797

The Webpack historyApiFallback config option is what you're looking for. Just set that to true and all requests that don't route to an asset will be rewritten to /. You can also pass in an object with custom rewrites:

historyApiFallback: {
  rewrites: [
    { from: /^\/$/, to: '/views/landing.html' },
    { from: /^\/subpage/, to: '/views/subpage.html' },
    { from: /./, to: '/views/404.html' }
  ]
}

(Example taken from the documentation page linked above.)

Upvotes: 0

Related Questions