react-router-hono-server icon indicating copy to clipboard operation
react-router-hono-server copied to clipboard

Issue When Setting basename on RR Config

Open s-cochrane opened this issue 10 months ago • 9 comments

We have our RR server instance behind a reverse proxy and use the basename property in react-router.config.ts as well as the base property in vite.config.ts to make this work. This moves the RR base path as well as the base for the static assets. When following the docs for setting this same property with react-router-hono-server, the route moves to the new path set by 'basename', but all the static assets continue to be served at the root path. This breaks the app when placed behind the reverse proxy as the asset requests aren't routed properly. Is there a way to get all assets as well as the RR app routes served based on the basename?

I cloned the repo and ran the custom-mount example app while changing the basename config to /v2. This resulted in the /v2 path resolving correctly for RR, but the first asset request was for http://localhost:5173/app/styles/tailwind.css, and all the other assets follow the same pattern. Setting the base config in vite.config.ts to match results in the request path from the client being correct, but the assets aren't resolved and result in a 404, http://localhost:5173/v2/app/styles/tailwind.css.

Running a production build with the basename change only also results in the static assets still being requested to the root, http://127.0.0.1:3000/assets/tailwind-p7bQns9R.css.

s-cochrane avatar Jan 29 '25 04:01 s-cochrane

👋 Thanks for reporting

I will double-check because I don't remember why I intentionally did that. Maybe it was because the client-side code was not able to fetch assets with the base name.

Let's try again, I will ping you asap.

rphlmr avatar Jan 29 '25 10:01 rphlmr

Update

It's still an issue https://github.com/remix-run/react-router/issues/12295

rphlmr avatar Feb 09 '25 15:02 rphlmr

We're also monitoring the associated fix, remix-run/react-router/pull/12898. We've applied the patch associated with that thread @rphlmr mentioned with good results until the fix is merged.

s-cochrane avatar Feb 12 '25 17:02 s-cochrane

@s-cochrane I will try to patch a demo project and see how I can solve this ticket 👀

rphlmr avatar Feb 12 '25 19:02 rphlmr

Okay, so this issue is now closed and should be released in the next RR version.

I will work on it when it's published.

rphlmr avatar Feb 26 '25 07:02 rphlmr

Reviewing this again, it seems that it is still an issue for resources 🧐

Front end still try to load from / and not /{basename}

rphlmr avatar Mar 09 '25 17:03 rphlmr

Just confirming on our end that the 7.3.0 release of RR did indeed fix the basename issue for us when used without rr-hono-server. We were using a patch that we've since been able to remove.

s-cochrane avatar Mar 09 '25 19:03 s-cochrane

I think I haven’t re-read carefully and I forgot the vite config part 🤦‍♂️

I will try again to see if I need to set the basename to “resources” middleware as well.

rphlmr avatar Mar 09 '25 20:03 rphlmr

Update on this, I encountered this same issue and I sort of fixed it in my own hacky way with a custom patch on my shipped version of react-router-hono-server. I'm only running on Node, so I don't really know much about the other platforms but this is how I fixed it:

https://github.com/rphlmr/react-router-hono-server/blob/07c7eb7a07790efdf03c4cd29b7d2bb7f1f98b8c/src/adapters/node.ts#L135-L148.

That first app.use was changed to account for my basename before the / and the Hono module to serve static files on Node.js supports a rewriteRequestPath function, so for both the app.use calls here, I added this:

# __vitedef_BASENAME is my own custom define thing.
rewriteRequestPath: path => path.replace(__vitedef_BASENAME, "/")

tale avatar Apr 05 '25 22:04 tale