cors apifix
This commit is contained in:
@@ -7,13 +7,13 @@ import { LEGACY_BASE_URL, REST_BASE_URL } from '../data/topics'
|
|||||||
const ADMIN_SECRET = 'nearle-admin-secret'
|
const ADMIN_SECRET = 'nearle-admin-secret'
|
||||||
|
|
||||||
function toProxyPath(fullUrl) {
|
function toProxyPath(fullUrl) {
|
||||||
// In dev, route REST through the Vite proxy (/live → jupiter.nearle.app)
|
// REST: always strip to a relative /live/... path so the request goes through
|
||||||
// to bypass CORS restrictions on localhost.
|
// the local server proxy (Vite dev/preview, or nginx in production).
|
||||||
if (import.meta.env.DEV && fullUrl.startsWith(REST_BASE_URL)) {
|
// This avoids CORS entirely — the browser never talks to jupiter.nearle.app directly.
|
||||||
|
if (fullUrl.startsWith(REST_BASE_URL)) {
|
||||||
return fullUrl.slice(REST_BASE_URL.length)
|
return fullUrl.slice(REST_BASE_URL.length)
|
||||||
}
|
}
|
||||||
// Legacy (api.workolik.com): CORS open, admin secret injected in headers.
|
// Legacy (api.workolik.com): CORS is open, admin secret injected in headers.
|
||||||
// REST in production: deployed origin is whitelisted by jupiter.nearle.app.
|
|
||||||
return fullUrl
|
return fullUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,28 +11,34 @@ export default defineConfig(({ mode }) => {
|
|||||||
console.warn('[xpress-docs] HASURA_ADMIN_SECRET is not set in .env.local; proxied requests will hit the API without auth.')
|
console.warn('[xpress-docs] HASURA_ADMIN_SECRET is not set in .env.local; proxied requests will hit the API without auth.')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const proxyConfig = {
|
||||||
|
'/api': {
|
||||||
|
target: 'https://api.workolik.com',
|
||||||
|
changeOrigin: true,
|
||||||
|
secure: true,
|
||||||
|
configure: (proxy) => {
|
||||||
|
proxy.on('proxyReq', (proxyReq) => {
|
||||||
|
if (secret) proxyReq.setHeader('x-hasura-admin-secret', secret)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'/live': {
|
||||||
|
target: 'https://jupiter.nearle.app',
|
||||||
|
changeOrigin: true,
|
||||||
|
secure: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
plugins: [react()],
|
plugins: [react()],
|
||||||
server: {
|
server: {
|
||||||
port: 5173,
|
port: 5173,
|
||||||
open: true,
|
open: true,
|
||||||
proxy: {
|
proxy: proxyConfig,
|
||||||
'/api': {
|
},
|
||||||
target: 'https://api.workolik.com',
|
preview: {
|
||||||
changeOrigin: true,
|
port: 4173,
|
||||||
secure: true,
|
proxy: proxyConfig,
|
||||||
configure: (proxy) => {
|
|
||||||
proxy.on('proxyReq', (proxyReq) => {
|
|
||||||
if (secret) proxyReq.setHeader('x-hasura-admin-secret', secret)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'/live': {
|
|
||||||
target: 'https://jupiter.nearle.app',
|
|
||||||
changeOrigin: true,
|
|
||||||
secure: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user