fix(dev): stub /api/* in vite dev to avoid @vercel/kv transform crash; allow owners to create map entries

This commit is contained in:
Satyam Rastogi
2026-05-29 01:32:55 +05:30
parent 73387366fe
commit 97597737df
2 changed files with 35 additions and 3 deletions
+2 -2
View File
@@ -231,8 +231,8 @@ function Maps() {
// 2. Click Empty Map (Create New) // 2. Click Empty Map (Create New)
const handleMapCreate = async (latlng) => { const handleMapCreate = async (latlng) => {
// Only Field Agents (employees) can create // Field Agents, Admins, and Owners can create new map entries
if (user?.role !== 'FIELD_AGENT' && user?.role !== 'ADMIN') return; if (!['FIELD_AGENT', 'ADMIN', 'OWNER'].includes(user?.role)) return;
setSelectedPropertyId(null); setSelectedPropertyId(null);
setNewPropertyLocation({ lat: latlng.lat, lng: latlng.lng, address: '' }); setNewPropertyLocation({ lat: latlng.lat, lng: latlng.lng, address: '' });
+33 -1
View File
@@ -1,8 +1,40 @@
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react' 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({ export default defineConfig({
plugins: [react()], plugins: [react(), devApiStub],
server: { server: {
// Don't watch or serve /api — those are Vercel serverless functions, not frontend code // Don't watch or serve /api — those are Vercel serverless functions, not frontend code
watch: { ignored: ['**/api/**'] }, watch: { ignored: ['**/api/**'] },