2.8 KiB
2.8 KiB
React Setup Skill (Vite + Django Backend)
Spin up a fresh React app (Vite) that talks to a Django backend over HTTPS with correct CORS/CSRF handling, Axios defaults, and a basic router/layout scaffold.
What This Skill Does
- Initializes a new Vite React project in
frontend/. - Configures
vite.config.jsfor HTTPS dev using mkcert certs fromcerts/, port 5173, and proxying/apitohttps://localhost:8000. - Adds CSRF service and Axios interceptors that inject
X-CSRFTokenfor mutating requests. - Sets up React Router layout (
Root), navbar/footer, home page, and backend health check component that verifies CSRF +/api/health/. - Provides a minimal API config file to centralize endpoints.
- Validates the build with
npm --prefix ./frontend run build.
Prerequisites
- Django backend running over HTTPS (see
django-setupskill) with endpoints/api/csrf/and/api/health/. - mkcert certificates stored in project
certs/(key/cert filenames discovered in skill steps). - Node.js 18+ and npm available.
Setup Summary (see SKILL.md for exact commands)
- Create project:
npm create vite@latest frontend -- --template react --yes; thennpm --prefix ./frontend install react-router-dom axios. - Configure
vite.config.js: importfs/path; load mkcert key/cert fromcerts/; setserver.https,server.port=5173, and proxy/api→https://localhost:8000withsecure:false. - API config
src/config/api.js: exportAPI_BASE_URL(empty to use proxy) andENDPOINTSwithCSRF: '/api/csrf/'. - CSRF service
src/services/csrf.js: fetch/api/csrf/once, readcsrftokencookie, cache/token helpers. - Axios instance
src/services/api.js:withCredentials:true, JSON headers; request interceptor addsX-CSRFTokenviagetCsrfToken()for POST/PUT/PATCH/DELETE. - UI scaffold: navbar/footer components;
Rootlayout withOutlet;Homepage usingBackendStatuscomponent that pings CSRF then/api/health/. - Routing:
src/router/AppRoutes.jsxexportscreateAppRouter()config;src/router/index.jsxwrapsRouterProvider;App.jsxrenders<Router />. - Verify:
npm --prefix ./frontend run buildto ensure config/imports succeed.
Outputs/Artifacts
- Vite React app under
frontend/with HTTPS dev server ready to hit Django. vite.config.jswith mkcert HTTPS + API proxy.- Axios + CSRF utilities wired for Django session auth.
- Minimal router/layout/pages demonstrating backend connectivity.
Notes & Gotchas
- Make sure
certs/exists and filenames match what mkcert generated; updatevite.config.jsaccordingly. - Backend must expose
/api/csrf/to set thecsrftokencookie; otherwise Axios interceptor will fail. - If you change backend host/port, update the proxy target in
vite.config.jsand any hardcodedFRONTEND_URLon the Django side.