Architectural Invariant Design: Nuxt 4 Directory Restructuring, Nitro Isolation, and SSR State Hydration Guardrails
Nuxt 4 Directory Restructuring, Nitro Isolation, and SSR State Hydration Guardrails defines an industrial-grade behavioral standard for frontend engineering. Enforces strict boundary separation between Nuxt 4 app/ frontend execution context, Nitro v2 server runtime, and the shared/ directory. Eliminates cross-request state pollution, server-side prototype leaks, and client bundle contamination by mandating per-request state instantiation and runtime-agnostic shared utilities. 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 Mutable State in Nitro Handlers and Direct Server Import into Nuxt 4 App Code | 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 locate client-facing components, composables, pages, and plugins strictly inside the `app/` directory pursuant to Nuxt 4 restructuring defaults.
Engineers MUST ALWAYS locate client-facing components, composables, pages, and plugins strictly inside the `app/` directory pursuant to Nuxt 4 restructuring defaults.
Engineers MUST ALWAYS isolate SSR state using `useState()` or Pinia stores bound to `useNuxtApp()`
Engineers MUST ALWAYS isolate SSR state using `useState()` or Pinia stores bound to `useNuxtApp()`; module-level `ref()`, `reactive()`, or plain JavaScript objects outside request scope are STRICTLY FORBIDDEN.
Engineers MUST NEVER import Nitro-only dependencies (e.g., `h3`, `#imports` containing server utilities, database connectors) into `app/` or `shared/`.
Engineers MUST NEVER import Nitro-only dependencies (e.g., `h3`, `#imports` containing server utilities, database connectors) into `app/` or `shared/`.
Engineers MUST ensure code located in `shared/` is strictly pure and runtime-agnostic
Engineers MUST ensure code located in `shared/` is strictly pure and runtime-agnostic; it MUST NOT reference Node.js built-ins (`fs`, `path`), browser globals (`window`, `localStorage`), or Nitro-specific context.
Nitro server event handlers MUST instantiate state per request or attach scoped context to `event.context`
Nitro server event handlers MUST instantiate state per request or attach scoped context to `event.context`; they MUST NEVER write mutable data to module-scoped singleton variables.
Engineers MUST ALWAYS use `defineEventHandler` or `defineCachedEventHandler` with deterministic cache keys and avoid leaking sensitive backend headers or un-sanitized server state into client hydration payloads.
Engineers MUST ALWAYS use `defineEventHandler` or `defineCachedEventHandler` with deterministic cache keys and avoid leaking sensitive backend headers or un-sanitized server state into client hydration payloads.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Nuxt 4 Directory Restructuring, Nitro Isolation, and SSR State Hydration Guardrails critical for AI coding assistants?
Enforces strict boundary separation between Nuxt 4 app/ frontend execution context, Nitro v2 server runtime, and the shared/ directory. Eliminates cross-request state pollution, server-side prototype leaks, and client bundle contamination by mandating per-request state instantiation and runtime-agnostic shared utilities. 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 Mutable State in Nitro Handlers and Direct Server Import into Nuxt 4 App Code) 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.