-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
40 lines (35 loc) · 1.18 KB
/
Copy pathvite.config.js
File metadata and controls
40 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig(({ mode }) => {
// Load env file based on `mode` in the current working directory.
const env = loadEnv(mode, process.cwd(), '')
return {
plugins: [react()],
server: {
proxy: {
'/api/serpapi': {
target: 'https://serpapi.com',
changeOrigin: true,
rewrite: (path) => {
// Extract query parameter from path
const url = new URL(path, 'http://localhost:5173');
const query = url.searchParams.get('q');
const apiKey = env.VITE_SERPAPI_KEY || '';
if (!apiKey) {
console.error('VITE_SERPAPI_KEY is not set in .env file');
}
if (query) {
return `/search.json?engine=google_scholar&q=${encodeURIComponent(query)}&api_key=${apiKey}`;
}
return '/search.json';
},
configure: (proxy, _options) => {
proxy.on('error', (err, _req, _res) => {
console.log('proxy error', err);
});
},
},
},
},
}
})