Files
RepoDructor/plugins/proxy-headers.client.ts

39 lines
1.4 KiB
TypeScript

export default defineNuxtPlugin(() => {
// Handle proxy headers and ensure proper request handling
if (process.client) {
// Handle WebSocket connection errors gracefully
const originalConsoleError = console.error
console.error = function(...args) {
// Suppress noisy WebSocket HMR connection errors
if (args[0] && typeof args[0] === 'string' &&
(args[0].includes('WebSocket connection') ||
args[0].includes('failed to connect to websocket'))) {
return // Suppress these specific errors
}
originalConsoleError.apply(console, args)
}
// Override fetch to handle proxy correctly
const originalFetch = window.fetch
window.fetch = function(input: RequestInfo | URL, init?: RequestInit) {
// Ensure all API calls use relative URLs to work with proxy
if (typeof input === 'string' && input.startsWith('/api/')) {
// Add proper headers for proxy compatibility
return originalFetch(input, {
...init,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
...init?.headers
}
})
}
return originalFetch(input, init)
}
// Handle Nuxt/Vite specific issues with proxy
if ('__NUXT__' in window) {
console.log('[PROXY] Nuxt app loaded through proxy successfully')
}
}
})