Architectural Invariant Design: Nuxt 4 Directory Restructuring, Nitro Context & SSR State Isolation
Nuxt 4 Directory Restructuring, Nitro Context & SSR State Isolation defines an industrial-grade behavioral standard for frontend engineering. Enforces Nuxt 4 directory architecture (isolating application code inside `app/` and shared cross-runtime logic inside `shared/`), strict Nitro server-route context management, and per-request SSR state isolation. Prohibits global module-level mutable singletons and cross-boundary runtime contamination between Nitro server routes and Vue client code. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Global Module-Level State Bleed and Invalid Directory Cross-Imports Across Nuxt 4 Runtimes | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS place client-side and Vue application components, composables, and pages within the root `app/` directory (or configure compatibilityVersion
Engineers MUST ALWAYS place client-side and Vue application components, composables, and pages within the root `app/` directory (or configure compatibilityVersion: 4).
Engineers MUST ALWAYS place universal utility functions, types, and DTO schemas inside `shared/` without importing runtime-specific dependencies (such as `h3`, `vue`, or DOM APIs).
Engineers MUST ALWAYS place universal utility functions, types, and DTO schemas inside `shared/` without importing runtime-specific dependencies (such as `h3`, `vue`, or DOM APIs).
Engineers MUST NOT declare global mutable variables or stateful module-level singletons in `server/` or `app/` as they cause memory leaks and cross-request SSR data bleeding.
Engineers MUST NOT declare global mutable variables or stateful module-level singletons in `server/` or `app/` as they cause memory leaks and cross-request SSR data bleeding.
Engineers MUST ALWAYS access and store per-request Nitro context exclusively through `event.context` using typed augmentations in `server/plugins` or middleware.
Engineers MUST ALWAYS access and store per-request Nitro context exclusively through `event.context` using typed augmentations in `server/plugins` or middleware.
Engineers MUST ALWAYS wrap server-hydrated reactive state in `useState(key, init)` or dynamic per-request Pinia stores rather than standalone Vue `ref()` or `reactive()` outside setup lifecycle.
Engineers MUST ALWAYS wrap server-hydrated reactive state in `useState(key, init)` or dynamic per-request Pinia stores rather than standalone Vue `ref()` or `reactive()` outside setup lifecycle.
Components NEVER directly import modules from `server/` routes or Nitro utilities
Components NEVER directly import modules from `server/` routes or Nitro utilities; communication must solely occur via type-safe `$fetch` or `useFetch` with Nitro runtime endpoints.
Nitro route handlers MUST ALWAYS return serializable plain JSON objects and use `defineEventHandler` or `defineLazyEventHandler` to maintain runtime boundaries.
Nitro route handlers MUST ALWAYS return serializable plain JSON objects and use `defineEventHandler` or `defineLazyEventHandler` to maintain runtime boundaries.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Nuxt 4 Directory Restructuring, Nitro Context & SSR State Isolation critical for AI coding assistants?
Enforces Nuxt 4 directory architecture (isolating application code inside `app/` and shared cross-runtime logic inside `shared/`), strict Nitro server-route context management, and per-request SSR state isolation. Prohibits global module-level mutable singletons and cross-boundary runtime contamination between Nitro server routes and Vue client code. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Global Module-Level State Bleed and Invalid Directory Cross-Imports Across Nuxt 4 Runtimes) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.