Architectural Invariant Design: Nuxt 4 Directory Boundaries and Nitro Cross-Request State Isolation
Nuxt 4 Directory Boundaries and Nitro Cross-Request State Isolation defines an industrial-grade behavioral standard for frontend engineering. Enforces architectural boundaries between Nuxt 4 app/, server/, and shared/ directories to eliminate cross-request memory contamination and hydration mismatches. Mandates pure runtime-agnostic code in shared/ and strictly isolates per-request state in Nitro handlers using H3 event context. 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 | Cross-Request State Contamination via Module-Level Nitro State and Invalid Shared Imports | 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 NEVER declare mutable module-level state (e.g., global `let`, `Map`, or singleton instances) in Nitro handlers, middleware, or utilities
Engineers MUST NEVER declare mutable module-level state (e.g., global `let`, `Map`, or singleton instances) in Nitro handlers, middleware, or utilities; state must be scoped to `event.context`.
Engineers MUST isolate shared code within `<rootDir>/shared/` to pure, runtime-agnostic utilities and types, never importing Node.js native APIs, Nitro runtime constructs, or Vue reactivity primitives (`ref`, `reactive`, `computed`).
Engineers MUST isolate shared code within `<rootDir>/shared/` to pure, runtime-agnostic utilities and types, never importing Node.js native APIs, Nitro runtime constructs, or Vue reactivity primitives (`ref`, `reactive`, `computed`).
Engineers MUST NEVER import directly from `server/` into `app/`, or from `app/` into `server/`
Engineers MUST NEVER import directly from `server/` into `app/`, or from `app/` into `server/`; all cross-environment primitives must be imported from `shared/`.
Engineers MUST strictly utilize Nuxt 4's `app/` folder convention for frontend-specific code (`app/components/`, `app/composables/`, `app/plugins/`, `app/middleware/`) to prevent build-time directory collisions.
Engineers MUST strictly utilize Nuxt 4's `app/` folder convention for frontend-specific code (`app/components/`, `app/composables/`, `app/plugins/`, `app/middleware/`) to prevent build-time directory collisions.
Engineers MUST preserve SSR-to-client hydration integrity by using `useState` for shared Vue reactive state rather than global module variables or bare `ref()` outside the Nuxt setup scope.
Engineers MUST preserve SSR-to-client hydration integrity by using `useState` for shared Vue reactive state rather than global module variables or bare `ref()` outside the Nuxt setup scope.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Nuxt 4 Directory Boundaries and Nitro Cross-Request State Isolation critical for AI coding assistants?
Enforces architectural boundaries between Nuxt 4 app/, server/, and shared/ directories to eliminate cross-request memory contamination and hydration mismatches. Mandates pure runtime-agnostic code in shared/ and strictly isolates per-request state in Nitro handlers using H3 event context. 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 (Cross-Request State Contamination via Module-Level Nitro State and Invalid Shared Imports) 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.