MockJS and Vite Break the Frontend's Dependency on Backend APIs
Timeline coupling with backend teams is the silent killer of frontend velocity. A one-line baseURL switch and Vite-level request interception remove the wait, letting frontend work proceed on its own schedule and making integration a configuration change rather than a rewrite.
Frontend teams often stall waiting for backend APIs, even when codebases are separated. A full-stack Todos project demonstrates how a dedicated `api/` directory—with an Axios instance, centralized baseURL, and per-module request functions—creates an interface layer that the UI depends on exclusively. Vite's `vite-plugin-mock` intercepts `/api` requests at the dev-server level and returns fake data from a `mock/` directory, so the frontend can build, route, and test every state without a running backend.
A deliberately set `timeout: 2000` in mock responses forces loading-state handling during development, preventing the common bug where instant mock data hides missing spinners and skeletons. When the real backend is ready, changing one `baseURL` line in `config.js` switches every API call from mock to the live server.
The architecture separates concerns into three layers: mock data sources, a unified request-management layer, and UI components that never know where data originates. React Router handles page routing while the mock plugin handles API routing, keeping the two routing concerns from colliding.
The `/api` prefix convention is a simple but load-bearing design decision: without it, a URL like `/todos` is ambiguous—it could be a page route or an HTTP request, and no tool can reliably tell them apart.
Mock speed is a hidden hazard. Instant responses during development train developers to ignore loading, empty, and error states, which then break under real-world latency. A deliberate delay is a cheap form of production hardening.
Frontend interface engineering treats API management as a first-class build concern, not an afterthought. The `api/` directory is the frontend equivalent of a backend's service layer, and it deserves the same discipline around configuration, timeouts, and environment switching.
The discussion centers on practical benefits of the mock-driven workflow. One point emphasizes that establishing an API contract upfront prevents back-and-forth delays during integration. Another adds that injecting artificial delays into mocks helps surface UI state bugs before they reach production.
Defining the API contract first can reduce integration-testing bottlenecks.
Simulating latency can expose state issues early.