Route Middleware
Create auth middleware that:- Checks JWT tokens- Redirects to login- Handles refresh tokens- Works with SSR/SPA modes
Master full-stack Vue development with Nuxt 3 using Cursor IDE and Claude Code. These patterns cover server-side rendering, API routes, data fetching, deployment, and the unique features that make Nuxt a powerful framework for production applications.
claude
in terminal/init
to create Nuxt-specific CLAUDE.md# Nuxt 3 Development Rules
## Framework Conventions- Use Nuxt 3 auto-imports (no manual imports for Vue, composables)- File-based routing in pages/ directory- Server routes in server/api/- Composables in composables/ directory- Middleware in middleware/ directory
## Code Standards- TypeScript for all files- Composition API with <script setup>- Use Nuxt composables (useFetch, useAsyncData, useState)- Server-side data fetching in setup
## Performance Guidelines- Lazy load components with Lazy prefix- Use nitro prerender for static pages- Implement proper caching strategies- Optimize images with @nuxt/image
Creating Server Routes:
Create a complete CRUD API for products in server/api/:1. GET /api/products - list with pagination2. GET /api/products/[id] - single product3. POST /api/products - create with validation4. PUT /api/products/[id] - update5. DELETE /api/products/[id] - soft delete
Include:- Zod validation schemas- Error handling middleware- TypeScript types shared with frontend- Rate limiting- Authentication checks
Type-Safe API with tRPC:
Set up tRPC in our Nuxt app:1. Install and configure @trpc/server for Nuxt2. Create typed router with procedures3. Set up client-side hooks4. Share types between client/server5. Add authentication context6. Implement error handling
Prisma with Nuxt:
Configure Prisma for our Nuxt app:1. Set up Prisma with SQLite for development2. Create schema for users, posts, comments3. Generate Prisma client4. Create server utils for database access5. Add migration scripts6. Implement connection pooling7. Set up seed data
Create examples showing:1. Parallel data fetching with Promise.all2. Dependent queries with watch3. Infinite scroll with useFetch4. Real-time updates with EventSource5. Optimistic updates6. Global error handling7. Request deduplication
Global State with Pinia:
Store Setup
Create a Pinia store for our e-commerce app that:- Integrates with Nuxt's useFetch- Handles SSR hydration properly- Implements persist plugin for client-side- Has actions that work on both server and client
Cross-Request State Pollution Prevention
Show me how to prevent state pollution in SSR by:- Using useState for request-scoped state- Properly resetting Pinia stores- Implementing factory functions
Analyze our Nuxt app and:1. Set up custom auto-imports in nuxt.config2. Create type-safe composables3. Configure component auto-imports4. Set up utility auto-imports5. Handle naming collisions6. Generate proper TypeScript declarations
Route Middleware
Create auth middleware that:- Checks JWT tokens- Redirects to login- Handles refresh tokens- Works with SSR/SPA modes
Server Middleware
Implement server middleware for:- Request logging- CORS handling- Rate limiting- Security headers
Production Configuration:
Configure Nitro for production:1. Set up prerendering rules2. Configure edge function deployment3. Implement ISR (Incremental Static Regeneration)4. Set up caching headers5. Configure CDN integration6. Optimize cold starts7. Set up health checks
Optimize our Nuxt app bundles:- Analyze with @nuxt/analyze- Implement route-based splitting- Lazy load heavy components- Extract vendor chunks- Tree-shake unused code- Optimize Tailwind CSS
Set up @nuxt/image module:- Configure providers (Cloudinary, Vercel)- Implement responsive images- Add lazy loading- Set up blur placeholders- Configure WebP generation- Implement art direction
Write tests for our Nuxt components:1. Set up @nuxt/test-utils2. Test pages with route parameters3. Mock useFetch responses4. Test error boundaries5. Verify SEO meta tags6. Test suspense boundaries7. Mock authentication state
Create Playwright tests for critical flows:- Test SSR/hydration- Verify API routes work- Test form submissions- Check SEO elements- Test error pages- Verify redirects- Test i18n routing
Implement multi-tenant architecture:1. Domain-based tenant detection2. Tenant-specific middleware3. Database isolation strategies4. Theme customization per tenant5. Tenant-specific API routes6. Shared component library
WebSocket Integration:
Add real-time features with Nitro WebSockets:1. Set up WebSocket server2. Implement chat functionality3. Add presence indicators4. Handle reconnection logic5. Scale with Redis pub/sub6. Add authentication
Implement comprehensive SEO:- Dynamic og:image generation- JSON-LD structured data- Sitemap generation- Robots.txt management- Canonical URL handling- Multi-language SEO- Social media cards
Debugging Pattern:
My Nuxt app has hydration errors. Help me:1. Identify the source using Vue devtools2. Add client-only wrappers where needed3. Fix date/time formatting issues4. Handle user-specific content5. Debug with --no-ssr flag
Analyze and fix memory leaks:- Check event listener cleanup- Review reactive data usage- Audit third-party plugins- Profile with Chrome DevTools- Implement proper dispose methods
Configure for Vercel deployment:- Set up vercel.json- Configure edge functions- Set environment variables- Implement preview deployments- Add analytics
Create production Docker setup:- Multi-stage Dockerfile- Optimize image size- Configure health checks- Set up compose file- Add nginx proxy
Help me migrate from Nuxt 2 to Nuxt 3:1. Audit current modules usage2. Update configuration syntax3. Convert to Composition API4. Update data fetching patterns5. Migrate Vuex to Pinia6. Update build configuration7. Test and fix edge cases