Vite with SSR in Docker behind Traefik #20575
designermonkey
started this conversation in
General
Replies: 1 comment
-
The fundamental issue is that WebSocket connections require HTTP server capability for the upgrade handshake, which Express alone doesn't provide in the way Vite HMR expects when you run Vite in middlewareMode. You need to give Vite’s HMR a real Node HTTP server to attach its WS upgrade handler to. import { createServer as createHmrServer } from 'node:http'
import express from 'express'
import type { ViteDevServer } from 'vite'
const base = process.env.BASE || '/'
const hmrHost = process.env.HMR_HOST || 'localhost'
const app = express()
const hmrServer = createHmrServer(app) // <- real HTTP server for HMR
let vite: ViteDevServer
const { createServer } = await import('vite')
vite = await createServer({
server: {
middlewareMode: true,
hmr: {
server: hmrServer, // <- hand Vite the server
host: hmrHost, // <- your public domain behind Traefik
clientPort: 80, // <- 80 if Traefik terminates TLS; use 443 + 'wss' if not
protocol: 'ws',
},
watch: { usePolling: true },
},
appType: 'custom',
base,
})
// then mount Vite middlewares as usual
app.use(vite.middlewares)
// start your main app server as you normally do |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
I am at my wits' end here after two days of reading and re-reading the same posts on the internet about how to get this setup working.
As far as I can see, this is a common enough setup that I am really surprised there is no useful documentation or even an official tutorial on how to do this. I even resorted to asking ChatGPT and Perplexity, both of the examples provided still didn't work!
The issue is this:
The HMR config is set inside the SSR server call to make a
ViteDevServer
(using the example fromvite-create-ssr
(I think it's called that)). It is as follows:As you can see, I have even tried the proxy. With it configured as above, it simple fails in the browser with no message on the server. Client errors:
Can anyone out there get this working?
Beta Was this translation helpful? Give feedback.
All reactions