SpawnDev has two libraries that let C# use browser APIs: SpawnDev.BlazorJS and SpawnDev.SpawnJS. If you are starting a project, or you already use one, you will want to know which to pick and what moving costs.
The short answer: start new projects on SpawnJS. If your app runs well on BlazorJS, there is no rush; it is still supported. When you do move, most of your code stays the same.
What each one is
SpawnDev.BlazorJS was first published in December 2022 and has over 222,000 NuGet downloads. It works through Blazor's own JavaScript interop.
SpawnDev.SpawnJS is the newer library. It talks to JavaScript through .NET's JSImport and JSExport and does not need Blazor at all.
| BlazorJS | SpawnJS | |
|---|---|---|
| Needs Blazor | Yes | No: also .NET WebAssembly apps, Web Workers, a console app under Node |
| How values cross to JavaScript | Through Blazor's interop, which converts them to JSON | Kept live in JavaScript; no JSON on the default path |
| Trimming | Its JSON interop is not trim-friendly in the same way | Ships trimming descriptors and generates code for your types |
| Status | Supported | Where new work happens |
The SpawnJS README reports a test app that went from about 30 MB to about 9 MB when trimmed. In the small example below, the published apps were 19.5 MB on SpawnJS and 22.5 MB on BlazorJS, counting the compressed copies of each file.
Beyond Blazor
Because SpawnJS does not depend on Blazor, the same C# can run where Blazor never does: a plain .NET WebAssembly app, a Web Worker, a browser extension, or a .NET WebAssembly console app running under Node.js with no browser at all.
That last one is not a curiosity. SpawnDev.ILGPU, our GPU library, runs its shared GPU test suite that way: a .NET WebAssembly console app under Node.js, using SpawnJS for every JavaScript call, with real WebGPU from the webgpu npm package, the Dawn project's own binding. On an RTX 4070, all 876 tests that apply there pass. The other 22 are skipped: 17 belong to other backends, and 5 check for a feature Node does not have, such as a canvas or a video frame, and say so.
The first run found something a browser-only test run never could: under Node, the library never loaded one of its WebGPU helper scripts, so dispatch batching quietly switched off. That is fixed in SpawnDev.ILGPU 5.4.52.
To try it yourself, Example 08 runs a GPU kernel from C# under Node and prints the GPU it found. It needs Node 22 or later and the .NET wasm-tools workload.
BlazorJS is built on Blazor's own interop, so it needs a Blazor app.
The same page, twice
Article 1 built a recipe page that keeps the screen awake and copies to the clipboard with SpawnJS. We wrote the same page with BlazorJS, and the same browser tests pass for both.
The page itself did not change except for one line, the runtime you inject:
@inject BlazorJSRuntime JS
Everything else, Navigator, WakeLock, Clipboard, the += and -= events, using, is the same in both libraries.
Moving from BlazorJS to SpawnJS
1. Swap the packages.
dotnet remove package SpawnDev.BlazorJS
dotnet add package SpawnDev.SpawnJS
dotnet add package SpawnDev.SpawnJS.Blazor
2. Change the start-up lines in Program.cs.
// BlazorJS
builder.Services.AddBlazorJSRuntime();
await builder.Build().BlazorJSRunAsync();
// SpawnJS
builder.Services.AddSpawnJSRuntime();
await builder.Build().SpawnJSRunAsync();
3. Change the namespaces in _Imports.razor and your C# files: SpawnDev.BlazorJS becomes SpawnDev.SpawnJS, and SpawnDev.BlazorJS.JSObjects becomes SpawnDev.SpawnJS.JSObjects.
4. Change the injected runtime: BlazorJSRuntime becomes SpawnJSRuntime.
That was the whole move for our example.
Where you will need to look closer
Your own wrappers. If you wrote classes for a JavaScript library, the base class and the reference type change: JSObject becomes SpawnJSObject, and the constructor takes a SpawnJSObjectReference instead of an IJSInProcessObjectReference. Both libraries' own Audio wrapper shows it:
// BlazorJS
public Audio(IJSInProcessObjectReference _ref) : base(_ref) { }
// SpawnJS
public Audio(SpawnJSObjectReference _ref) : base(_ref) { }
Calls by name. BlazorJS takes the arguments as objects and names only the return type. SpawnJS names every argument type, then the return type:
// BlazorJS
var encoded = JS.Call<string>("btoa", "Hello");
// SpawnJS
var encoded = JS.Call<string, string>("btoa", "Hello");
The compiler points at each of these, so the move is a list of build errors to work through, not a hunt.
Matching packages. Several libraries come in both families, such as SpawnDev.BlazorJS.WebWorkers and SpawnDev.SpawnJS.WebWorkers, and SpawnDev.BlazorJS.Cryptography and SpawnDev.SpawnJS.Cryptography. Move each to its SpawnJS twin at the same time.
Run it
Both versions of the page are in the example repo:
git clone https://github.com/LostBeard/SpawnDev.Examples
cd SpawnDev.Examples/BrowserApis.BlazorJS
dotnet run
The SpawnJS version is in BrowserApis, next to it.
Top comments (0)