Skip to content

Commit

Permalink
feat(vite): support RegExp strings as server.proxy keys (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
timozander committed Jan 13, 2021
1 parent 7746674 commit f39a2aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ export default ({ command, mode }) => {

- **Type:** `Record<string, string | ProxyOptions>`

Configure custom proxy rules for the dev server. Expects an object of `{ key: options }` pairs. Uses [`http-proxy`](https://github.com/http-party/node-http-proxy). Full options [here](https://github.com/http-party/node-http-proxy#options).
Configure custom proxy rules for the dev server. Expects an object of `{ key: options }` pairs. If the key starts with `^`, it will be interpreted as a `RegExp`.

Uses [`http-proxy`](https://github.com/http-party/node-http-proxy). Full options [here](https://github.com/http-party/node-http-proxy#options).

**Example:**

Expand All @@ -255,6 +257,12 @@ export default ({ command, mode }) => {
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
// with RegEx
'^/fallback/.*': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/fallback/, '')
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export function proxyMiddleware({
return (req, res, next) => {
const url = req.url!
for (const context in proxies) {
if (url.startsWith(context)) {
if (
(context.startsWith('^') && new RegExp(context).test(url)) ||
url.startsWith(context)
) {
const [proxy, opts] = proxies[context]

if (opts.bypass) {
Expand Down

0 comments on commit f39a2aa

Please sign in to comment.