# C# Blazor Apply these rules when building or changing Blazor components and apps. Performance and cross-platform reuse are the primary design goals. Check the target framework first: render modes, state persistence, and antiforgery behavior differ between .NET 8, 9, 10, and 11. Load the UI/UX rules for interface design and the shared web security rule for browser concerns. ## Performance * Measure before optimizing: profile rendering, download size, and server memory in `Release` builds against a baseline. * Choose the render mode for its cost: static server rendering for content, interactive server where latency is low and connections are few, WebAssembly where offline use or server load matters, and Auto to combine fast first load with client execution. Each mode moves cost between download size, server memory, and network latency. * Keep WebAssembly payloads small: trim, lazy load rarely used assemblies with `BlazorWebAssemblyLazyLoad`, and serve compressed, fingerprinted assets. Enable AOT (`RunAOTCompilation`, with `WasmStripILAfterAOT`) only for CPU-heavy apps, because it speeds execution but enlarges the download. * Expect prerendered components to initialize twice. Persist prerendered state with `PersistentComponentState`, or `[PersistentState]` on .NET 10+, instead of repeating service and database calls. * Use streaming rendering (`[StreamRendering]`) to show content while slow data loads on static server rendering. * Render less: virtualize large lists with `Virtualize`, give list items a stable `@key`, avoid unnecessary `StateHasChanged` calls, override `ShouldRender` for components with costly subtrees, and avoid creating thousands of small component instances in a loop when plain markup would do. * Batch JavaScript interop. Avoid many fine-grained calls in loops, and pass small, serializable data. * On interactive server rendering, keep circuit state small, release large objects promptly, and avoid per-user caches that grow without bounds. ## Cross-platform * Put shared UI in a Razor class library that works in every host: Blazor Web App, WebAssembly, and Blazor Hybrid in .NET MAUI (`BlazorWebView`). Keep host-specific code out of it. * Access platform features (device info, storage, files, notifications) through interfaces in the shared library, implemented per host and registered with dependency injection. * Write components that work in every render mode they may run in: no browser-only APIs before the first render, no assumption of an HTTP context, and JavaScript interop only in `OnAfterRenderAsync(firstRender)`. * Treat all WebAssembly and Hybrid client code and data as public. Never ship secrets or rely on client-side authorization; `AuthorizeView` and `[Authorize]` only hide UI. Enforce authorization in the server APIs and data access the client calls. * Keep per-user state out of singleton services. On interactive server rendering, state lives in the user's circuit and is lost on disconnect; persist what must survive a reconnect or reload. * Keep `app.UseAntiforgery()` in the pipeline. `EditForm` adds the token automatically; add `` to plain HTML forms. Validate forms with a validator component and again on the server. * Dispose `IJSObjectReference` and `DotNetObjectReference` instances in `DisposeAsync`, catch `JSDisconnectedException` there for server circuits, and implement `IDisposable` or `IAsyncDisposable` to unsubscribe events and stop timers. * Never render untrusted content with `MarkupString`; let Razor encode it. Wrap risky regions in `ErrorBoundary` and keep detailed errors out of production. * Test every host and render mode you ship, including Hybrid on each MAUI target platform.