feat: add HTTP and WebSocket development proxy
This commit is contained in:
@@ -60,6 +60,45 @@ export default defineConfig({
|
||||
|
||||
Files in `public/` are served from the site root without bundling. State-preserving JavaScript HMR, framework integrations, SSR, and a preview server are not implemented yet.
|
||||
|
||||
## Development proxy
|
||||
|
||||
The development server can proxy selected HTTP and WebSocket routes to an independently running backend. Production builds do not contain this proxy; production deployments must configure their own routing.
|
||||
|
||||
```js
|
||||
import { defineConfig } from '@fretuks/iceberg';
|
||||
|
||||
export default defineConfig({
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': 'http://localhost:3001',
|
||||
'/socket.io': { target: 'http://localhost:3001', ws: true },
|
||||
'/socket': {
|
||||
target: 'http://localhost:3001',
|
||||
ws: true,
|
||||
changeOrigin: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
String shorthand is equivalent to `{ target: '...' }`. Proxy paths match complete path segments, use the longest matching prefix, preserve query strings and request/response bodies, and stream HTTP and WebSocket traffic. `changeOrigin` changes the outgoing host/origin behavior; HTTPS certificate verification is enabled by default and can be disabled for one trusted local target with `secure: false`.
|
||||
|
||||
To rewrite a path while preserving its query string:
|
||||
|
||||
```js
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:3001',
|
||||
rewrite: path => path.replace(/^\/api(?=\/|$)/, '') || '/'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Proxy failures return `502 Bad Gateway` (or `504 Gateway Timeout`) as JSON and are logged without stopping Iceberg. `/__iceberg_ws` is reserved for Iceberg's own live-reload connection and cannot be configured as a proxy route. Exposing the dev server with `--host 0.0.0.0` also exposes configured proxy routes, so only do this on a trusted network.
|
||||
|
||||
The complete frontend/backend example is in `examples/proxy`. Install dependencies in both `examples/proxy/frontend` and `examples/proxy/backend`, start the backend with `npm start`, then run `npm run dev` in the frontend directory.
|
||||
|
||||
## Configuration
|
||||
|
||||
Iceberg works without configuration. The optional `iceberg.config.js` file supports:
|
||||
|
||||
Reference in New Issue
Block a user