fix(dev): stub /api/* in vite dev to avoid @vercel/kv transform crash; allow owners to create map entries
This commit is contained in:
+33
-1
@@ -1,8 +1,40 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
/**
|
||||
* Dev-only stub for /api/* requests.
|
||||
*
|
||||
* The files in /api are Vercel serverless functions — they run only on Vercel,
|
||||
* not in the local Vite dev server. When the frontend polls an endpoint like
|
||||
* /api/hail-status during local dev, Vite would otherwise resolve that URL to
|
||||
* the on-disk api/*.js file and push it through its module-transform pipeline,
|
||||
* which crashes on server-only imports such as `@vercel/kv`.
|
||||
*
|
||||
* This middleware intercepts /api/* before Vite transforms anything and returns
|
||||
* a 503. Every frontend hook that hits these endpoints already degrades
|
||||
* gracefully on a failed fetch (falls back to mock data / inactive storm state),
|
||||
* so the app behaves correctly in local dev — just without the live API.
|
||||
*/
|
||||
const devApiStub = {
|
||||
name: 'dev-api-stub',
|
||||
apply: 'serve',
|
||||
configureServer(server) {
|
||||
server.middlewares.use((req, res, next) => {
|
||||
if (req.url && req.url.startsWith('/api/')) {
|
||||
res.statusCode = 503
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.end(JSON.stringify({
|
||||
error: 'API routes run only on Vercel and are unavailable in local dev.',
|
||||
}))
|
||||
return
|
||||
}
|
||||
next()
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
plugins: [react(), devApiStub],
|
||||
server: {
|
||||
// Don't watch or serve /api — those are Vercel serverless functions, not frontend code
|
||||
watch: { ignored: ['**/api/**'] },
|
||||
|
||||
Reference in New Issue
Block a user