How to host V7 Framework SPA behind a reverse proxy #14799
Unanswered
George-Madeley
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Does anyone know how to host a v7 framework SPA behind a reverse proxy (I.e., nginx)?
I know you should set the
baseattribute on the Vite config to "./" to make all asset imports relative instead of absolute. Running this behind a reverse proxy allows me to successfully fetch any non-route components; components that are not pages or layouts. It fails to fetch the route components because it for some reason the app duplicates the asset directory in the path (I.e., instead of fetching the route component from "/assets/home_[hash].js", the imports tries to import the component "/assets/assets/home_[hash].js". This path does not exist and therefore fails.).In my basic C++ HTTP server, I just added a piece of code that replaces all instances of "/assets/assets" with "/assets". This has fixed the issue but it seems to he a stupid way to solve it. However, this has shown the head of another issue regarding client-side routing in the reverse proxy. Let's say I host the website behind the reverse proxy at
localhost:8080/my_site/, if I have an/aboutroute in my app, when I navigate to that route, instead of going tolocalhost:8080/my_site/about, it instead navigates tolocalhost:8080/about(as if the app was not run behind a reverse proxy). This is an issue with navigation within the app as I am using a purely CSR SPA; no SSR.To resolve this navigation issue, do I need to set the
basenameto "./" in the react router config? I tried to do this but I got the following build error:ErrorResponseImpl { status: 404, statusText: 'Not Found', internal: false, data: 'Refusing to prerender the `` path because it does not start with the basename `./`' } ✗ Build failed in 1.23s [react-router] SPA Mode: Received a 404 status code from `entry.server.tsx` while prerendering your `index.html` file. Not Found at handleSpaMode (/node_modules/@react-router/dev/dist/vite.js:4281:13) at Object.handler (/node_modules/@react-router/dev/dist/vite.js:3679:13) at async Promise.all (index 0) at PluginDriver.hookParallel (file:///node_modules/rollup/dist/es/shared/node-entry.js:22458:9) at file:///node_modules/rollup/dist/es/shared/node-entry.js:23542:13 at catchUnfinishedHookActions (file:///node_modules/rollup/dist/es/shared/node-entry.js:22902:16) at buildEnvironment (file:///node_modules/vite/dist/node/chunks/config.js:33543:64) at Object.build (file:///node_modules/vite/dist/node/chunks/config.js:33900:19) at buildEnvironment (/node_modules/@react-router/dev/dist/cli/index.js:2025:5) at async Promise.all (index 0) { code: 'PLUGIN_ERROR', plugin: 'react-router', hook: 'writeBundle' }This seems to be because React Router will force...
This error originates from the
derive()function.I have tried to build by setting
prerenderattribute to false andbasenameto "/" in thereact-router.config.tsbut when I open the website behind the reverse proxy, any fetch request to my HTTP server is still absolute. (I have reserved the/api/*route for my C++ HTTP server so I can make requests to that backend instead of loaders/actions within the react router app. This is simply done by doingfetch("/api/somePATH");within my apps code. This is still absolute tolocalhost:8080and not relative tolocalhost:8080/my_site/.) Getting the favicon also has the same issue. Looking the console logs, I get errors for those two issues but I also get another error coming form theentry.client.tsx(I have not revealed this file let alone modified it)TypeError: Cannot read properties of null (reading 'useContext') at Ha.W.useContext (chunk-JZWAC4HX-DUJYhASH.js:1:7785) at Sr (chunk-JZWAC4HX-DUJYhASH.js:2:21383) at Ti (chunk-JZWAC4HX-DUJYhASH.js:4:13161) at chunk-JZWAC4HX-DUJYhASH.js:4:13253 at Gf (entry.client-CqBigqCN.js:8:47856) at ac (entry.client-CqBigqCN.js:8:70530) at _m (entry.client-CqBigqCN.js:8:80854) at uv (entry.client-CqBigqCN.js:8:116380) at j1 (entry.client-CqBigqCN.js:8:115462) at Dc (entry.client-CqBigqCN.js:8:115300)Has anyone got a React Router CSR app (specifically v7 Framework SPA) running successfully behind a reverse proxy?
Build config
Beta Was this translation helpful? Give feedback.
All reactions