404 with BrowserRouter

maxivalliHOBBY

a year ago

I have a project built with Express, Node.js, and Vite. If I set up routing with HashRouter, I have no issues. But if I use BrowserRouter, I get 404 errors when I refresh on a route other than the root. I understand it's a server-side routing issue, but I don't know how to solve it. Can anyone help me?

40 Replies

a year ago

Can you share your repository so I can give you a better solution?


maxivalliHOBBY

a year ago

Yes. d9e723dd-08af-409b-bf3b-2afbf84f0169


a year ago

Okay, please share your repository.


maxivalliHOBBY

a year ago

Ok. Give me a minute because is private


maxivalliHOBBY

a year ago


a year ago

I'm assuming you have two services in Railway for the client and server?


maxivalliHOBBY

a year ago

Yes


a year ago

What do you mean by testing? that would be the proper way to setup this kind of stack in Railway.


maxivalliHOBBY

a year ago

I got confused, I thought you were asking about both frontend services. My english is not good


a year ago

Both? I only see one frontend (client) app in the repo.


maxivalliHOBBY

a year ago

Ah ok, yes, in the repo is only client.


a year ago

No, it's not. there is a server folder here too.


maxivalliHOBBY

a year ago

Sorry, yes, both frontend and Server are in the repository.


a year ago

Okay, and do you have a Railway project with both those services deployed? can you attach a screenshot of your Railway project please?


maxivalliHOBBY

a year ago

Okey

Attachments


a year ago

Do you have the respective root directories set for the frontend and server services?


maxivalliHOBBY

a year ago

Yes, at the moment everything is working because I have routing configured with HashRouter, but if I change it to BrowserRouter, it doesn't work.


a year ago

Great, just needed to make sure you have the Railway side of things setup properly.

https://github.com/brody192/vite-react-template

  • Copy the nixpacks.toml and Caddyfile from this repo into your Client folder.

  • Delete your railway.json file in the Client's folder

  • Change the build script to just vite build in your Client's package.json

  • If you have any build or start commands set in your Client's Railway service settings, remove them.


maxivalliHOBBY

a year ago

Ok. What repo? I don't see any link? Sorry


a year ago

Oh, I'm so sorry, the formatting of this form removed it from my comment. Updated.


maxivalliHOBBY

a year ago

Now I see it. Thank you very much, I'll test it out and come back with updates


a year ago

Sounds good!


maxivalliHOBBY

a year ago

I have this error during build

20 [stage-0 14/15] RUN caddy fmt --overwrite Caddyfile

20 0.429 Error: reading input file: open Caddyfile: no such file or directory

20 ERROR: process "/bin/bash -ol pipefail -c caddy fmt --overwrite Caddyfile" did not complete successfully: exit code: 1

[stage-0 14/15] RUN caddy fmt --overwrite Caddyfile:

0.429 Error: reading input file: open Caddyfile: no such file or directory

Dockerfile:34

32 | # fmt phase

33 | COPY . /app/.

34 | >>> RUN caddy fmt --overwrite Caddyfile

35 |

36 |

ERROR: failed to solve: process "/bin/bash -ol pipefail -c caddy fmt --overwrite Caddyfile" did not complete successfully: exit code: 1

Error: Docker build failed


a year ago

Your Caddyfile is named Caddyfile.txt


maxivalliHOBBY

a year ago

Maybe when it was downloaded, the extension was added. Now I will try again. Thanks


maxivalliHOBBY

a year ago

WORKS GREAT!! πŸ₯³ ThankU!!


a year ago

Awesome! happy to help!


maxivalliHOBBY

a year ago

Ohhh, some routes doesn't work 😫


a year ago

Make sure you are doing client side routing.


maxivalliHOBBY

a year ago

Yes, I'm doing it. I see this error in the console of the browser: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.


a year ago

Can you send me a link to a page where I could see that error for myself?



a year ago

Okay, I see the issue, i'll try to come up with a solution.


maxivalliHOBBY

a year ago

Okay, thankU again!



maxivalliHOBBY

a year ago

Okey, I will try now


maxivalliHOBBY

a year ago

You are the BOSS!! Now works


a year ago

Awsome!


maxivalliHOBBY

a year ago

🫢🏻


a year ago

<3