Replies: 4 comments 1 reply
-
|
server.origin only affects asset URLs generated by plugins, not the core HTML transforms Vite injects (the /@vite/client script, /src/ imports). |
Beta Was this translation helpful? Give feedback.
-
|
puedes configurar el base y el server.origin en vite.config para que genere las urls absolutas apuntando al servidor de vite: // vite.config.ts
export default defineConfig({
server: {
origin: "http://localhost:5173",
}
})con eso el html que devuelve vite ya incluye la url completa en vez del path relativo, asi cuando tu backend de go sirve el html los scripts apuntan al sitio correcto. basicamente le dices a vite desde donde se van a cargar sus assets |
Beta Was this translation helpful? Give feedback.
-
|
To answer your follow-up about HMR: yes, several things trigger a full reload instead of a hot update.
For your reverse-proxy setup specifically: HMR WebSocket connections also need to reach the Vite server. If you're proxying, make sure WebSocket upgrade requests to // Make sure your Go proxy handles WebSocket upgrades for HMR
mux.Handle("/__vite_hmr", viteProxy) // or use a wildcard |
Beta Was this translation helpful? Give feedback.
-
|
It helps with asset URLs generated by plugins, but it does not rewrite Vite’s core HTML injections such as: <script type="module" src="/@vite/client"></script>
<script type="module" src="/src/main.jsx"></script>So if you fetch Best solution: reverse proxy Vite through the backendInstead of fetch-and-re-serve, proxy Vite dev requests through the Go server so the browser stays on one origin while Vite still serves its own dev resources. At minimum proxy:
Example: viteProxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: "localhost:5173",
})
mux.Handle("/@vite/", viteProxy)
mux.Handle("/src/", viteProxy)
mux.Handle("/node_modules/", viteProxy)
mux.Handle("/__vite_hmr", viteProxy)Why proxying is the safest approachIt automatically keeps working for:
No manual HTML rewriting needed. If you still need backend HTML injectionA safe pattern is:
For example: <script>
window.__BOOTSTRAP__ = {...}
</script>but do not try to own Vite’s module URLs manually unless necessary. Fallback if proxying is not possibleExplicit string replacement works, but it is brittle: html = strings.ReplaceAll(html, `"/@vite/client"`, `"http://localhost:5173/@vite/client"`)
html = strings.ReplaceAll(html, `"/src/`, `"http://localhost:5173/src/`)This can break later if Vite changes injected paths. About HMR / reload behaviorEven with proxying, some changes still trigger full reload instead of hot updates:
So occasional full reloads are expected. Bottom lineFor this architecture, reverse proxying Vite through the backend is currently the cleanest and most future-proof solution. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
In a Go backend dev setup, I’m fetching
index.htmlfrom the Vite dev server and then serving it from a different origin (e.g.http://localhost:8080).By default, the HTML returned by Vite includes root-relative paths like:
When this HTML is re-served from another origin, those paths resolve incorrectly.
Is there a supported way to make the dev server emit fully-qualified URLs (e.g.
http://localhost:5173/@vite/client) in the injected HTML?I’ve tried setting
server.origin, but the returned HTML still contains root-relative paths.Is this possible ?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions