Choosing rewrite over pathRewrite in Vite Proxy: What about dynamic target resolution?
0 reputation · 19 Jul 2020, 20:48 UTC
0 reputation · 19 Jul 2020, 20:48 UTC
Vite’s dev‑server proxy accepts an object mapping request paths to options. The documented rewrite function transforms the incoming URL before forwarding, whereas the legacy pathRewrite option is no longer supported. In addition, the official docs only illustrate a static string for the target property, leaving the possibility of a dynamic target function ambiguous.
When a proxy entry needs to route requests to different back‑ends based on runtime data (e.g., request method, headers, or query parameters), can the target be a function that returns a URL per request? If so, how does Vite evaluate this function—once at startup or on each request—and how does it interact with the rewrite logic, especially regarding query parameters?
target option be defined as a function that returns a URL based on the incoming request?rewrite function preserve query parameters when stripping a prefix, and how does this behavior change when combined with a dynamic target?35275 reputation · 19 Jul 2020, 23:55 UTC
Can the target be a function?
No. In Vite 4.x/5.x the target property is defined as a string URL. Passing a function will throw a configuration error because the underlying http-proxy middleware expects a concrete host string.
When is a dynamic target evaluated?
Dynamic routing is achieved through the router option, which receives the incoming req and returns a target URL string. This callback is invoked for every request, so you can inspect headers, query parameters, or method to decide the backend.
Does rewrite preserve query parameters?
The rewrite function only changes the req.url path component. The query string is left untouched unless you explicitly remove it inside the rewrite logic. When combined with a dynamic router, the final request sent to the backend includes the original query string.
Define a static target as a placeholder; it can be any valid URL because the router will override it per request.
Implement a router callback that returns the correct target based on runtime data.
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://placeholder', // required but unused
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
router: (req) => {
// Example: route by a custom header
const env = req.headers['x-env'];
if (env === 'prod') return 'http://api.prod.example.com';
if (env === 'staging') return 'http://api.staging.example.com';
return 'http://api.dev.example.com';
},
},
},
},
});Test the setup by inspecting the backend logs. The request path will be rewritten, and the query string will remain intact.
target is static; dynamic routing must use router.rewrite function runs on every request and only mutates the path.rewrite unless you strip them yourself.To fine‑tune the router logic, you might want to log the incoming request path and headers. If the behavior still doesn’t match expectations, check the Vite version and ensure you’re using the latest stable release.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.