Rule 6 of 8 · Chapter II — State & Rendering
Async lifecycle methods must tolerate running twice.
Why this rule exists
With prerendering enabled, OnInitializedAsync runs on the server and again on the client. Side effects written for exactly-once execution — telemetry, counters, POSTs — happen twice, and the duplicates show up in production analytics, not in dev.
In practice
Keep lifecycle methods idempotent, or use persistent component state to hand the prerendered data across. Anything with side effects belongs behind an after-first-render guard.
When it doesn't apply
Apps with prerendering fully disabled — but say so where the assumption lives, because someone will re-enable it.
Related rules in this book
Sources
- Microsoft Learn — Prerender ASP.NET Core Razor components — Why lifecycle methods run twice