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
Vercel is a platform for deploying the fastest React sites. You can deploy your site with zero configuration to the best frontend infrastructure.
This feature has been deprecated because the new structure of Routes is that they should act like components, so you should take advantage of component lifecycle methods instead.
React Router is the most popular library for implementing routing for React apps. It was first released in May 2014 and it grew through trial and error as React's API changed from version to version.
To enable routing in our React app, we first need to import BrowserRouter from react-router-dom . This should hold everything in our app where routing is needed. That means, if we need routing in our entire app, we must wrap our higher component with BrowserRouter .
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
In order to make the React Router work in Vercel I had to specify each route in the vercel.json
file, as mentioned in Surbina's answer. (Thanks btw, I used this solution for a quite some time)
{
"routes": [
{ "src": "/", "dest": "/" },
{ "src": "/recruit", "dest": "/" }
{ "src": "/tos", "dest": "/" }
// ....
]
}
But this may not be optimal depending on how many routes your application has, and honestly sometimes I forget to add new routes.
I tried the "match all" Regex as the source route [{ "src": "/*", "dest": "/" }]
, like I used to do in other hosting services, but for some Reason, Vercel server uses this routing for all requests, even when index.html
needs to request a file like bundle.js
, breaking the application.
And the solution I've found is using a Regex that matches all routes, except for routes that include a dot (.), which are usually files, like bundle.js
. Then you can request a url like /recruit
and it gets routed to /
since it doesn't have a dot.
The regex is /[^.]+
(Turns into ^/[^.]+$
on Vercel).
Please note that this is a very simple Regex and may not cover all cases, however, I haven't got any issues with it to the moment of this comment.
So my vercel.json
file looks like this:
{
"routes": [{ "src": "/[^.]+", "dest": "/", "status": 200 }]
}
I hope it is useful for you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With