Last active
June 27, 2025 21:38
-
-
Save xfournet/068592b3d1ddd488427b874b23f707bf to your computer and use it in GitHub Desktop.
Vite support for HTTP2 and proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import proxy from 'http2-proxy'; | |
import type { Plugin, ProxyOptions } from 'vite'; | |
export const pluginHttp2Proxy = (): Plugin => { | |
let routes: Record<string, string | ProxyOptions>; | |
return { | |
name: 'vite-plugin-http2-proxy', | |
config: (config) => { | |
const { server } = config; | |
routes = server?.proxy ?? {}; | |
if (server) { | |
server.proxy = undefined; | |
} | |
return config; | |
}, | |
configureServer: ({ config: { logger }, middlewares }) => { | |
Object.entries(routes).forEach(([route, target]) => { | |
if (typeof target !== 'string') { | |
throw new Error('ProxyOptions target are not supported yet, only string target are currently supported'); | |
} | |
const { protocol, hostname, port } = new URL(target); | |
const options = { | |
protocol: protocol as 'http' | 'https', | |
hostname, | |
port: Number(port), | |
proxyTimeout: 60000, | |
}; | |
middlewares.use(route, (req, res) => { | |
proxy.web(req, res, { ...options, path: req.originalUrl }, (err) => { | |
if (err) { | |
logger.error(`[http2-proxy] Error when proxying request on '${req.originalUrl}'`, { timestamp: true, error: err }); | |
} | |
}); | |
}); | |
}); | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@c1aphas I'm seeing a lack of 304 status on unchanged files (the thousands of them I have ๐ )
is there a secret to retaining browser-cache?
Edit: my network tab's "Disable browser cache" checkbox was checked again ๐
But now I get a bunch of
net::ERR_HTTP2_PROTOCOL_ERROR
My code (includes mkcert)