-
Hi I couldn't make to redirect the user to the base path if it's not written by the user, this is my code: Into the index.tsx file: <BrowserRouter basename="/myapp">
<Routes>
<Route path="*" element={<MyApp />} />
</Routes>
</BrowserRouter> Into the MyApp.tsx file: <Routes>
<Route path="/login" element={<Login />} />
<Route element={<ProtectedRoutes />} >
<Route path="/home" element={<Home />} />
<Route path="/test" element={<Test />} />
<Route path="/help" element={<About />} />
</Route>
<Route path="/*" element={<DefaultRoute />} />
</Routes> The default route is simply checking that if the user is logged and types a wrong route it'll bring him to the home if not logged to the login page Also in the package.json I've setted the: "homepage":"/myapp" |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
In other words if I remove the /myapp and type directly /home or leve it blank it will not redirect the user to the base path + page name |
Beta Was this translation helpful? Give feedback.
-
@IronCub3 As per my understanding, since you are building your application for the The base path is basically used if you want to deploy your application in the |
Beta Was this translation helpful? Give feedback.
-
You're right I forgot to specify that the pages are rendered by the client side in the backend(server) I do only the work in order communicate with a DB and ensure that the communications between clients and the backend are secure. Therefore as you said the routing is done by the client side and my app run on a specific port so I was trying to understand if by using the base path Thanks @shrijan00003 |
Beta Was this translation helpful? Give feedback.
-
@IronCub3 Here is my answer with the following assumptions.
Here is the possible solution.
Thanks! |
Beta Was this translation helpful? Give feedback.
-
@shrijan00003 I've tested the method you've suggested but I've found a better way of doing this by using only react router dom without the |
Beta Was this translation helpful? Give feedback.
-
@IronCub3 Great you found the solution. The base path should be used only if you are going to host your application in some other path than root in the server like |
Beta Was this translation helpful? Give feedback.
-
Yes, to get the same result of the Into the index.tsx file: <BrowserRouter>
<Routes>
<Route path="*" element={<MyApp />} />
</Routes>
</BrowserRouter> Into the MyApp.tsx file: <Routes>
<Route path={urlPrefix}>
<Route element={<ProtectedRoutes />} >
<Route path="/home" element={<Home />} />
<Route path="/test" element={<Test />} />
<Route path="/help" element={<About />} />
</Route>
</Route>
<Route path="*" element={<DefaultRoute />} />
</Routes> Where the |
Beta Was this translation helpful? Give feedback.
Yes, to get the same result of the
basepath
what I've done is this:Into the index.tsx file:
Into the MyApp.tsx file:
Where the
urlPrefix
was my initialbasepath