Substantive.Blazor.JSInterop 9.4.0-build20260114r2

Substantive.Blazor.JSInterop

Utilities for bridging .NET and browser APIs in Blazor applications. The library ships a set of opinionated services that simplify dynamically loading JavaScript, CSS, and other assets at runtime while keeping Blazor Server and Blazor WebAssembly hosting models in sync.

Why this library?

  • Consistent DOM loadingIDomFileInjector abstracts both JSHost (WASM) and IJSRuntime (Server) pipelines, so component code stays identical regardless of where it runs.
  • Security-first defaults – optional attributes (integrity, crossorigin, referrerpolicy, nonce, fetchpriority) map directly to the generated DOM nodes, reducing CSP and mixed-content regressions.
  • Tenant/feature isolation – scripts, styles, or media can be injected when a component activates and removed when it disposes, minimizing bundle size and side effects.
  • Deterministic cleanup – optional tag identifiers let you remove previously injected assets without tracking the original URIs, which is useful for multi-tenant layouts and dynamic feature toggles.
  • Typed interop surface – TypeScript helpers (wwwroot/src/DynamicFileLoader.ts) and the bundled JS deliver a narrowly scoped API focused on asset management.

Getting started

  1. Reference the project/package from your Blazor application.

  2. Register the injector for the current hosting model in Program.cs:

    if (OperatingSystem.IsBrowser())
    {
        builder.Services.AddSingleton<IDomFileInjector, DomFileInjectorJSHost>();
    }
    else
    {
        builder.Services.AddSingleton<IDomFileInjector, DomFileInjectorJSRuntime>();
    }
    
  3. Inject and use the service inside a component:

    @inject IDomFileInjector DomFileInjector
    @implements IAsyncDisposable
    
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await DomFileInjector.LoadJavaScriptFileAsync(
                scriptSrc: "https://cdn.example.com/widgets.js",
                scriptType: "module",
                integrity: "sha384-...",
                crossOrigin: "anonymous",
                referrerPolicy: "strict-origin-when-cross-origin",
                nonce: cspNonceFromServer,
                fetchPriority: "high",
                tag: "widgets-lib");
    
            await DomFileInjector.LoadCSSFileAsync(
                src: "https://cdn.example.com/widgets.css",
                media: "(min-width: 768px)",
                tag: "widgets-style");
        }
    }
    
    public async ValueTask DisposeAsync()
    {
        await DomFileInjector.UnloadJavaScriptFileByTagAsync("widgets-lib");
        await DomFileInjector.UnloadCSSFileByTagAsync("widgets-style");
    }
    

Features

Area Description
- JavaScript loader Inject/remove <script> tags with async/defer, SRI, module/nomodule, nonce, and fetchpriority support.
- CSS loader Inject/remove <link rel="stylesheet"> with integrity, crossorigin, media queries, nonce, and fetchpriority.
+ JavaScript loader Inject/remove <script> tags with async/defer, SRI, module/nomodule, nonce, fetchpriority, and optional tag identifiers for deterministic cleanup.
+ CSS loader Inject/remove <link rel="stylesheet"> with integrity, crossorigin, media queries, nonce, fetchpriority, and optional tags for group removal.
Asset loader Preload fonts/images/videos or generic assets with the same security attributes.
TypeScript tooling Source files live under wwwroot/src and are bundled to wwwroot/dist. Update TS, then rebuild to refresh the bundle.

Development workflow

  1. Edit TypeScript in wwwroot/src.

  2. Bundle using the repo’s configured bundler (Rspack). Running dotnet build regenerates wwwroot/dist/js/DynamicFileLoader.bundle.js via the existing targets.

  3. Run tests/build from the repo root:

    dotnet build
    

Tag-based unloading cheat sheet

Scenario Recommended call
Remove a single script by URL UnloadJavaScriptFileAsync("https://cdn.example.com/widgets.js")
Remove any script that was loaded with a specific identifier UnloadJavaScriptFileByTagAsync("widgets-lib")
Remove a single stylesheet by URL UnloadCSSFileAsync("https://cdn.example.com/widgets.css")
Remove any stylesheet loaded with a specific identifier UnloadCSSFileByTagAsync("widgets-style")

Showing the top 20 packages that depend on Substantive.Blazor.JSInterop.

Packages Downloads
Substantive.Blazor.Captcha
Blazor components for display and validate Captcha widgets.
4
Substantive.Blazor.Captcha
Blazor components for display and validate Captcha widgets.
5
Substantive.Blazor.Experimental
A collection of Blazor experimental components and services.
5
Substantive.Blazor.Markdown
Library for parsing and linting markdown content.
5
Substantive.Blazor.Markdown
Library for parsing and linting markdown content.
6
Substantive.Blazor.Markdown
Library for parsing and linting markdown content.
23
Substantive.Blazor.Markdown
Library for parsing and linting markdown content.
24
Substantive.Blazor.MediaPlayer
Blazor components for video and audio player.
4
Substantive.Blazor.MediaPlayer
Blazor components for video and audio player.
5
Substantive.Blazor.WebEmbedded
Blazor components for embedded a web page or content via iFrame.
5
Substantive.Blazor.WebStorages
Services for maniuplate Cookie, LocalStorage, and SessionStorage.
4
Substantive.Blazor.WebStorages
Services for maniuplate Cookie, LocalStorage, and SessionStorage.
12
Substantive.Blazor.WebStorages
Services for maniuplate Cookie, LocalStorage, and SessionStorage.
17
Substantive.Blazor.WebStorages
Services for maniuplate Cookie, LocalStorage, and SessionStorage.
24
Substantive.Metapackage.Blazor
A metapackage contains all Blazor essential building blocks. It packed standalone Blazor components and extensions that often use such as JSInterop, BrowserInterop, Navigation, Exception Handling, Localization, etc,.
10
Substantive.SubstantiveBlazor.Core
The Substantive Design System's core library. This package manages UI libraries that the Design System builds upon.
22
Substantive.SubstantiveBlazor.Core
The Substantive Design System's core library. This package manages UI libraries that the Design System builds upon.
24

Version Downloads Last updated
9.4.0-build20260115R1 17 01/15/2026
9.4.0-build20260114R5 2 01/14/2026
9.4.0-build20260114R4 2 01/14/2026
9.4.0-build20260114R3 2 01/14/2026
9.4.0-build20260114R2 2 01/14/2026
9.4.0-build20260114R1 5 01/14/2026
9.4.0-build20260113R1 5 01/13/2026
9.4.0-build20260109R1 3 01/09/2026
9.4.0-build20251226R1 12 12/26/2025
9.3.3-build20251226R1 1 12/26/2025
9.3.2-build20251225R1 3 12/25/2025
9.3.0-build20251223R1 2 12/23/2025
9.2.0-build20251222R2 24 12/22/2025
9.2.0-build20251222R1 24 12/22/2025
9.2.0-build20251221R2 2 12/21/2025
9.2.0-build20251221R1 2 12/21/2025
9.1.0-build20251218R1 24 12/18/2025
9.1.0-build20251217R1 24 12/17/2025
9.1.0-build20251215R1 25 12/15/2025
9.1.0-build20251212R1 4 12/12/2025
9.1.0-build20251211R3 3 12/11/2025
9.1.0-build20251211R2 3 12/11/2025
9.1.0-build20251211R1 4 12/11/2025
9.1.0-build20251207R1 3 12/07/2025
9.1.0-build20251204R3 3 12/04/2025
9.1.0-build20251204R1 1 12/04/2025
9.1.0-build20251203R2 2 12/03/2025
9.1.0-build20251203R1 3 12/03/2025