chore: enhance Nginx configuration for direct access and update main entry point to dynamically resolve Router basename based on environment settings

This commit is contained in:
Alexis
2026-05-13 13:07:40 +02:00
parent a3b9082a71
commit 0265afad5b
2 changed files with 41 additions and 9 deletions
+5
View File
@@ -6,6 +6,11 @@ server {
root /app/dist;
index index.html;
# Acceso directo al puerto (sin Apache): misma app bajo el prefijo del build.
location = / {
return 301 /orcid2words/;
}
# Apache forwards the full path; strip /orcid2words/ before resolving files under dist/.
location = /orcid2words {
return 301 /orcid2words/;
+36 -9
View File
@@ -1,13 +1,40 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import App from './App.jsx'
import './index.css'
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import App from "./App.jsx";
import "./index.css";
createRoot(document.getElementById('root')).render(
/**
* `import.meta.env.BASE_URL` es fijo en build (p. ej. `/orcid2words/` en prod).
* Los assets siguen bajo ese prefijo, pero la URL del documento puede ser `/`
* (acceso directo `http://host:8073/`). React Router exige que el pathname
* empiece por el basename; si no, no renderiza nada.
*/
function resolveRouterBasename() {
const configured = import.meta.env.BASE_URL ?? "/";
const withSlash = configured.endsWith("/") ? configured : `${configured}/`;
if (withSlash === "/") {
return "/";
}
const { pathname } = window.location;
if (pathname === "/" || pathname === "") {
return "/";
}
const prefix = withSlash.replace(/\/$/, "");
if (pathname === prefix || pathname.startsWith(`${prefix}/`)) {
return withSlash;
}
return "/";
}
createRoot(document.getElementById("root")).render(
<StrictMode>
<BrowserRouter basename={import.meta.env.BASE_URL}>
<BrowserRouter basename={resolveRouterBasename()}>
<App />
</BrowserRouter>
</StrictMode>
)
</StrictMode>,
);