Posts

Stop Your Blazor Tabs from Fighting with the Web Locks API

Image
As .NET developers, our instinct when dealing with concurrency is to look at the backend: optimistic locking in Entity Framework, Redis distributed locks, or background queue orchestration. We rarely think about client-side concurrency until a user opens our application across three separate browser tabs. Suddenly, all three tabs trigger a local cache warmup, attempt to flush an IndexedDB offline queue simultaneously, and trigger duplicate background sync calls. Before building an ad-hoc coordination protocol over localStorage events or BroadcastChannel, look at what the browser already provides: the Web Locks API (navigator.locks). How Web Locks Work The Web Locks API acts as an origin-wide mutex across tabs, windows, and Web Workers. Instead of managing lock acquisition and manual teardown, you pass a callback to navigator.locks.request(). The browser grants the lock, executes your async callback, and releases the lock the moment the returned promise resolves or rejects. JavaScript a...

CancellationToken in Blazor: Stop Doing Work After the User Leaves

Image
A user opens a page in your Blazor app, and a database query begins. Before the query finishes, they click away to another menu item. In many Blazor applications, that original database query just keeps running. The component is no longer visible, the result is no longer needed, and the user is three pages away—yet your server is still allocating memory, executing SQL, consuming connection pool slots, and awaiting HTTP requests. When the operation finally completes, it delivers results to a component instance that has already been disposed. This is precisely what CancellationToken is designed to solve. However, implementing cancellation in Blazor requires a clear distinction between disposable UI work and durable business operations . The Problem in Plain Code Consider a standard Blazor component reading from an EF Core service: Razor CSHTML @page "/customers" @inject CustomerService CustomerService <h1>Customers</h1> @if (customers is null) { <p>Loa...