Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(vite): Hide API proxy error on start #8705

Merged
merged 8 commits into from
Jun 27, 2023
35 changes: 35 additions & 0 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,41 @@ export default function redwoodPluginVite(): PluginOption[] {
changeOrigin: true,
// Remove the `.redwood/functions` part, but leave the `/graphql`
rewrite: (path) => path.replace(rwConfig.web.apiUrl, ''),
configure: (proxy) => {
// @MARK: this is a hack to prevent showing confusing proxy errors on startup
// because Vite launches so much faster than the API server.
let waitingForApiServer = true

// Wait for 2.5s, then restore regular proxy error logging
setTimeout(() => {
waitingForApiServer = false
}, 2500)

proxy.on('error', (err, _req, res) => {
if (
waitingForApiServer &&
err.message.includes('ECONNREFUSED')
) {
err.stack =
'⌛ API Server launching, please refresh your page...'
}
const msg = {
errors: [
{
message:
'The RedwoodJS API server is not available or is currently reloading. Please refresh.',
},
],
}

res.writeHead(203, {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
})
res.write(JSON.stringify(msg))
res.end()
})
},
},
},
},
Expand Down