Architectural Invariant Design: Nuxt 4 Directory Separation, Nitro Context Isolation, and SSR Hydration Safeguards
Nuxt 4 Directory Separation, Nitro Context Isolation, and SSR Hydration Safeguards defines an industrial-grade behavioral standard for frontend engineering. Enforces strict architectural boundaries across Nuxt 4 directory layers (`app/`, `server/`, and `shared/`) while preventing multi-tenant cross-request state contamination in Nitro server engines. Eliminates hydration mismatches and secret leakage by restricting server-only modules and stateful closures during SSR execution. 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 | Module-Level Mutable State in Nitro Handlers and Cross-Boundary Node/Client Code 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 ALWAYS place client-side UI, components, composables, and pages within the `app/` directory pursuant to Nuxt 4 architecture.
Engineers MUST ALWAYS place client-side UI, components, composables, and pages within the `app/` directory pursuant to Nuxt 4 architecture.
Engineers MUST NEVER declare mutable variables (e.g., `let`, `new Map()`, or top-level singletons) at Nitro module scope in `server/`, as Nitro's long-running worker threads share module memory across concurrent user requests.
Engineers MUST NEVER declare mutable variables (e.g., `let`, `new Map()`, or top-level singletons) at Nitro module scope in `server/`, as Nitro's long-running worker threads share module memory across concurrent user requests.
Engineers MUST attach request-scoped state exclusively to Nitro's `event.context` using typed H3 event augmentations.
Engineers MUST attach request-scoped state exclusively to Nitro's `event.context` using typed H3 event augmentations.
Engineers MUST ensure code located in `shared/` is strictly isomorphic, side-effect-free, and contains only pure runtime validation schemas, pure transformation functions, and universal TypeScript contracts.
Engineers MUST ensure code located in `shared/` is strictly isomorphic, side-effect-free, and contains only pure runtime validation schemas, pure transformation functions, and universal TypeScript contracts.
Engineers MUST NEVER import database drivers, server SDKs, environment secrets, or Nitro-specific packages (`h3`, `nitropack`) into `app/` or `shared/`.
Engineers MUST NEVER import database drivers, server SDKs, environment secrets, or Nitro-specific packages (`h3`, `nitropack`) into `app/` or `shared/`.
Engineers MUST ALWAYS wrap shared Vue reactive state in `useState(key, init)` rather than module-scoped `ref()` or `reactive()` to prevent cross-request SSR hydration state leakage.
Engineers MUST ALWAYS wrap shared Vue reactive state in `useState(key, init)` rather than module-scoped `ref()` or `reactive()` to prevent cross-request SSR hydration state leakage.
Engineers MUST NEVER invoke browser globals (`window`, `document`, `localStorage`) in `server/`, `shared/`, or root setup functions of `app/` without an explicit `import.meta.client` guard.
Engineers MUST NEVER invoke browser globals (`window`, `document`, `localStorage`) in `server/`, `shared/`, or root setup functions of `app/` without an explicit `import.meta.client` guard.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Nuxt 4 Directory Separation, Nitro Context Isolation, and SSR Hydration Safeguards critical for AI coding assistants?
Enforces strict architectural boundaries across Nuxt 4 directory layers (`app/`, `server/`, and `shared/`) while preventing multi-tenant cross-request state contamination in Nitro server engines. Eliminates hydration mismatches and secret leakage by restricting server-only modules and stateful closures during SSR execution. 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 (Module-Level Mutable State in Nitro Handlers and Cross-Boundary Node/Client Code 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.