Skip to content

Commit

Permalink
fix(server): not proxy websocket by default (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Feb 7, 2024
1 parent 09aa887 commit ac2f59e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/core/src/server/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export const createProxyMiddleware = (proxyOptions: ProxyOptions) => {
};

middlewares.push(middleware);
proxyMiddlewares.push(proxyMiddleware);

// only proxy WebSocket request when user specified
// fix WebSocket error when user forget filter hmr path
opts.ws && proxyMiddlewares.push(proxyMiddleware);
}

const handleUpgrade: UpgradeEvent = (req, socket, head) => {
Expand Down
17 changes: 17 additions & 0 deletions packages/document/docs/en/config/server/proxy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ export default {
};
```

### Proxy WebSocket

If you want to proxy WebSocket requests, you can enable it through set `ws` to `true`:

```js
export default {
server: {
proxy: {
'/rsbuild-hmr': {
target: 'http://localhost:3000', // will proxy to ws://localhost:3000/rsbuild-hmr
ws: true,
},
},
},
};
```

## Options

The Rsbuild Server Proxy makes use of the [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware/tree/2.x) package. Check out its documentation for more advanced usages.
Expand Down
17 changes: 17 additions & 0 deletions packages/document/docs/zh/config/server/proxy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ export default {
};
```

### 代理 WebSocket 请求

如果你希望代理 WebSocket 请求,可以通过 `ws` 开启:

```js
export default {
server: {
proxy: {
'/rsbuild-hmr': {
target: 'http://localhost:3000', // 将会代理到 ws://localhost:3000/rsbuild-hmr
ws: true,
},
},
},
};
```

## 选项

Rsbuild Server Proxy 基于 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware/tree/2.x) 实现。你可以使用 http-proxy-middleware 的所有配置项,具体可以查看文档。
Expand Down

0 comments on commit ac2f59e

Please sign in to comment.