Skip to content

Commit 09a7440

Browse files
committed
feat: optimize reverse proxy
1 parent 5a00069 commit 09a7440

2 files changed

Lines changed: 121 additions & 1 deletion

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Frontend Reverse Proxy Rule
2+
3+
When implementing a frontend-backend separated web application, always configure a reverse proxy in the frontend to forward API requests to the backend server.
4+
5+
## Background
6+
7+
The current online preview environment can only expose a single port. Therefore, when developing applications with separate frontend and backend services, the frontend must include a reverse proxy configuration to route API requests to the backend.
8+
9+
## Guidelines
10+
11+
When implementing a frontend for a web application with a separate backend:
12+
13+
1. **Configure reverse proxy**: Set up a proxy in the frontend development server to forward API requests to the backend
14+
2. **Use consistent API prefix**: Typically use `/api` as the prefix for backend requests
15+
3. **Ensure both services start**: The preview should start both frontend and backend services
16+
17+
## Implementation by Framework
18+
19+
### Vite (Vue, React, etc.)
20+
21+
In `vite.config.ts` or `vite.config.js`:
22+
23+
```typescript
24+
export default defineConfig({
25+
server: {
26+
proxy: {
27+
'/api': {
28+
target: 'http://localhost:3001', // Backend server address
29+
changeOrigin: true,
30+
// rewrite: (path) => path.replace(/^\/api/, '') // Optional: remove /api prefix
31+
}
32+
}
33+
}
34+
})
35+
```
36+
37+
### Create React App
38+
39+
In `package.json`:
40+
41+
```json
42+
{
43+
"proxy": "http://localhost:3001"
44+
}
45+
```
46+
47+
Or use `src/setupProxy.js` for more control:
48+
49+
```javascript
50+
const { createProxyMiddleware } = require('http-proxy-middleware');
51+
52+
module.exports = function(app) {
53+
app.use('/api', createProxyMiddleware({
54+
target: 'http://localhost:3001',
55+
changeOrigin: true,
56+
}));
57+
};
58+
```
59+
60+
### Next.js
61+
62+
In `next.config.js`:
63+
64+
```javascript
65+
module.exports = {
66+
async rewrites() {
67+
return [
68+
{
69+
source: '/api/:path*',
70+
destination: 'http://localhost:3001/api/:path*',
71+
},
72+
];
73+
},
74+
};
75+
```
76+
77+
### Webpack Dev Server
78+
79+
In `webpack.config.js`:
80+
81+
```javascript
82+
module.exports = {
83+
devServer: {
84+
proxy: {
85+
'/api': {
86+
target: 'http://localhost:3001',
87+
changeOrigin: true,
88+
},
89+
},
90+
},
91+
};
92+
```
93+
94+
## Startup Script
95+
96+
Create a startup script that runs both services. Example `start.sh`:
97+
98+
```bash
99+
#!/bin/bash
100+
# Start backend server in background
101+
cd backend && npm run dev &
102+
BACKEND_PID=$!
103+
104+
# Start frontend server (this will be the exposed port)
105+
cd frontend && npm run dev
106+
107+
# Cleanup on exit
108+
trap "kill $BACKEND_PID" EXIT
109+
```
110+
111+
## Rationale
112+
113+
- Online preview environment only exposes one port to the user
114+
- The frontend development server port is typically the one exposed for preview
115+
- Reverse proxy allows API requests to reach the backend without CORS issues
116+
- This setup mimics production deployment where a single entry point handles all requests

.ai-ready/skills/deploy-website/SKILL.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ fi
133133

134134
4. Present the preview URL to the user:
135135
- Output a clickable hyperlink pointing to the preview address returned by the tool
136-
- Inform the user: If access is denied due to IP not being whitelisted, they can use the "Online Preview" button in the top right corner of the page to manually add their IP to the whitelist
136+
137+
5. Handle IP whitelist issues:
138+
- If the user reports that access is denied due to IP not being whitelisted, ask the user for their IP address(es) which reported by the deny page
139+
- Call MCP Tool `request_preview` again with the `additional_ip_whitelist` field to add the user's IP(s) to the whitelist
140+
- The `additional_ip_whitelist` field accepts multiple IPv4 addresses separated by commas (e.g., `"192.168.1.100,10.0.0.50"`)
137141

138142
## Notes
139143

0 commit comments

Comments
 (0)