Search code examples
reactjsreact-routervercelparceljs

Why does react-router not works at vercel?


I am trying to publish a serverless web to vercel. I want to use react-router and this works good on my computer but when I deploy it It doesn't works Can somebody help me?

(I want to do it without server)

// My main code
import React from 'react'
import { BrowserRouter, Switch, Route } from 'react-router-dom'

import Main from './routes/Main'
import Tos from './routes/Tos'
import Privacy from './routes/Privacy'
import NotFound from './routes/NotFound'
import Recruit from './routes/Recruit'

const App = () => {
    return (
        <BrowserRouter>
            <Switch>
                <Route exact path = '/' component = {Main} />
                <Route exact path = '/recruit' component = {Recruit} />
                <Route exact path = '/tos' component = {Tos} />
                <Route exact path = '/privacy' component = {Privacy} />
                <Route component = {NotFound} />
            </Switch>
        </BrowserRouter>
    )
}

export default App

Result


Solution

  • Add a vercel.json file at the root of your project, and use "rewrites" to rewrite all incoming paths to refer to your index path.

    For example:

    {
      "rewrites":  [
        {"source": "/(.*)", "destination": "/"}
      ]
    }
    

    Check here for further information: https://vercel.com/docs/configuration#project/rewrites