Vue 3 & Nuxt 3 — Best Practices & AI-Driven Scaffolding
AI Context & Token Optimization
- Composition API &
<script setup>Only: Do NOT use the Options API. The Composition API is far more token-efficient and predictable for AI code generation. - Auto-Imports: Rely entirely on Nuxt's auto-imports. Explicitly importing Vue refs or components wastes tokens and causes syntax hallucinations.
- TypeScript Mandate: Always use
lang="ts". Strongly typed props and Pinia state are required.
Modern Nuxt 3 App Architecture
Scaffold Nuxt 3 applications using these guidelines:
- Composition API: Always use
<script setup lang="ts">with TypeScript. Banned: Options API. - Auto-Imports Leverage: Rely on Nuxt's auto-imported directory structures for
composables/,components/, and core Vue APIs (ref,computed,reactive). - State Management: Use Pinia via
@pinia/nuxt. Define stores using the store-factory function syntax (defineStore('id', () => { ... })). - SSR-Safe Data Fetching: Always use
useFetchoruseAsyncDatato ensure data loads on the server and hydrates safely on the client. Banned: standardaxiosor barefetchinside components. - Form Validation: Use Formkit or VeeValidate + Zod for robust client-side schemas.
Project Structure
project/
├── assets/ # Uncompiled assets (CSS, SCSS)
├── components/ # Auto-imported Vue components
│ └── ui/ # Reusable UI elements (buttons, inputs)
├── composables/ # Auto-imported composition functions (Vue useHooks)
├── layouts/ # Shared page layouts
├── pages/ # File-based routing
├── plugins/ # Vue plugins initialized at startup
├── public/ # Static files served at root
├── server/ # Nitro API routes (Nuxt backend)
│ └── api/
├── stores/ # Pinia state management
└── nuxt.config.ts # Main Nuxt configuration
Naming Conventions
- Components:
PascalCase(e.g.,UserProfile.vue). Multi-word names are mandatory. - Composables:
camelCasestarting withuse(e.g.,useAuth.ts). - Pages/Routes:
kebab-case(e.g.,user-settings.vue).
Architectural Patterns
- Composition API: Use
<script setup lang="ts">exclusively. Avoid the older Options API (data(),methods). - Auto-imports: Rely on Nuxt's auto-import feature for components, composables, and Vue APIs (
ref,computed). Do not manually import them. - State Management: Use
Piniafor global state. Avoid Vuex. - Data Fetching: Use
useFetchoruseAsyncDatafor SSR-friendly data fetching. Do not use standardfetchoraxiosdirectly in components.
Universal DateTime Governance
- API Boundary: All datetimes from the backend are epoch ms or ISO-8601 UTC strings. Normalize to epoch ms in Pinia stores.
- Client Display: Format using
Intl.DateTimeFormatwith explicittimeZonein a composable (useDateTimeFormatter). Never rely on the browser's locale alone. - SSR Safety: Use
useAsyncDatawithdayjs.utc()for datetime parsing in server-side code. Never callnew Date()insetup()without UTC normalization. - State: Timestamps in Pinia stores must be epoch ms (number). Convert to localized strings only in template computed properties.
Testing Strategies
- Framework:
Vitest+Vue Test Utils. - Component Testing: Mount components and test DOM output/emitted events.
- E2E Testing: Use
PlaywrightorCypress.