Przejdź do głównej zawartości

Nuxt 3 Development Patterns

Ta treść nie jest jeszcze dostępna w Twoim języku.

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.

  1. Open Cursor and create a new folder
  2. Launch Agent mode (Cmd/Ctrl + I)
  3. Prompt: “Create a new Nuxt 3 project with TypeScript, Tailwind CSS, Pinia, and @nuxt/test-utils for testing. Include ESLint and Prettier configuration”
  4. Enable YOLO mode for automatic execution
  5. Review the nuxt.config.ts and project structure
.cursor/rules/nuxt-patterns.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:

Cursor/Claude Prompt
Create a complete CRUD API for products in server/api/:
1. GET /api/products - list with pagination
2. GET /api/products/[id] - single product
3. POST /api/products - create with validation
4. PUT /api/products/[id] - update
5. DELETE /api/products/[id] - soft delete
Include:
- Zod validation schemas
- Error handling middleware
- TypeScript types shared with frontend
- Rate limiting
- Authentication checks

Prisma with Nuxt:

Configure Prisma for our Nuxt app:
1. Set up Prisma with SQLite for development
2. Create schema for users, posts, comments
3. Generate Prisma client
4. Create server utils for database access
5. Add migration scripts
6. Implement connection pooling
7. Set up seed data
Data Fetching Examples
Create examples showing:
1. Parallel data fetching with Promise.all
2. Dependent queries with watch
3. Infinite scroll with useFetch
4. Real-time updates with EventSource
5. Optimistic updates
6. Global error handling
7. Request deduplication

Global State with Pinia:

  1. 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
  2. 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.config
2. Create type-safe composables
3. Configure component auto-imports
4. Set up utility auto-imports
5. Handle naming collisions
6. 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 rules
2. Configure edge function deployment
3. Implement ISR (Incremental Static Regeneration)
4. Set up caching headers
5. Configure CDN integration
6. Optimize cold starts
7. 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
Nuxt Testing Recipe
Write tests for our Nuxt components:
1. Set up @nuxt/test-utils
2. Test pages with route parameters
3. Mock useFetch responses
4. Test error boundaries
5. Verify SEO meta tags
6. Test suspense boundaries
7. 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 detection
2. Tenant-specific middleware
3. Database isolation strategies
4. Theme customization per tenant
5. Tenant-specific API routes
6. Shared component library

WebSocket Integration:

Add real-time features with Nitro WebSockets:
1. Set up WebSocket server
2. Implement chat functionality
3. Add presence indicators
4. Handle reconnection logic
5. Scale with Redis pub/sub
6. 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 devtools
2. Add client-only wrappers where needed
3. Fix date/time formatting issues
4. Handle user-specific content
5. 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
Help me migrate from Nuxt 2 to Nuxt 3:
1. Audit current modules usage
2. Update configuration syntax
3. Convert to Composition API
4. Update data fetching patterns
5. Migrate Vuex to Pinia
6. Update build configuration
7. Test and fix edge cases