Reference scripts in Blazor applications

27 Nov 202618 minutes to read

This page explains script isolation and how to reference Blazor scripts from the CDN, static web assets, and the Custom Resource Generator (CRG).

JavaScript interop files are required for features that cannot be implemented natively in Blazor.

CDN reference

You can refer the Blazor scripts using the CDN resources.

  • For .NET 8, .NET 9, .NET 10, and .NET 11 Blazor Web App (any render mode: Server, WebAssembly, or Auto), add scripts in ~/Components/App.razor.
  • For a Blazor WebAssembly (standalone) App, add scripts in ~/wwwroot/index.html.

Blazor components are available on the CDN per version. Ensure that the version in the URLs matches the NuGet package version used in the application.

Component CDN Script Reference

All Blazor UI Components

  • CSHTML
  • https://cdn.syncfusion.com/blazor/35.2.12/syncfusion-blazor.min.js
    <head>
        ...
        <script src="https://cdn.syncfusion.com/blazor/35.2.12/syncfusion-blazor.min.js" type="text/javascript"></script>
    </head>

    Additionally, Blazor components provide the latest scripts on the CDN without versioning. You can use this in a development environment if you want to always use the latest version of the scripts. It is not recommended to use this in a production environment.

    Component CDN Script Reference
    All Blazor UI Components https://cdn.syncfusion.com/blazor/syncfusion-blazor.min.js

    Static web assets

    You can refer to the Blazor scripts from NuGet packages by using static web assets.

    Enable static web assets usage

    To use static web assets, call UseStaticFiles in the app’s ~/Program.cs file.

    For a Blazor Web App (render mode: Auto) and a Blazor WebAssembly App, call UseStaticFiles in the Server project.

    Reference scripts from static web assets

    • Combined scripts are available in the Syncfusion.Blazor.Core package. To refer the script from static web assets, use the code below.

        <head>
            ...
            <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
        </head>

    Prevent stale cached assets using asset fingerprinting

    Browsers cache static web assets such as _content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js to improve load times. After the Blazor NuGet packages are upgraded, a browser can continue to use a previously cached script while the server runs newer Blazor assemblies. This version mismatch can cause runtime errors. Asset fingerprinting prevents this issue by generating a unique URL whenever the file content changes, so browsers automatically download the latest assets after an upgrade.

    ASP.NET Core asset fingerprinting is available in .NET 9 and later through MapStaticAssets. In Blazor Web App applications, fingerprinted asset URLs can be resolved through the @Assets[] resource collection. Static web assets from Razor class libraries, such as _content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js, are included in the fingerprinting process.

    1. Call MapStaticAssets() in the ~/Program.cs file. For a Blazor Web App, call it in the Server project.
    var app = builder.Build();
    
    app.UseHttpsRedirection();
    app.UseAntiforgery();
    
    app.MapStaticAssets();
    app.MapRazorComponents<App>()
        .AddInteractiveServerRenderMode();
    
    app.Run();
    1. Reference the theme and script assets through the @Assets resource collection. Add the theme stylesheet reference as described in the Static Web Assets section and place it at the end of the <head> section in the ~/Components/App.razor file.
    <link href="@Assets["_content/Syncfusion.Blazor.Themes/fluent2.css"]" rel="stylesheet" />

    Add the script reference at the end of the <body> section in the same file.

    <script src="@Assets["_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js"]" type="text/javascript"></script>

    The @Assets[] resource collection requires .NET 9 or later. The following table lists where to apply fingerprinted asset references for each application type.

    Application type Call MapStaticAssets() in Add the references in Reference syntax
    Blazor Web App (Server, WebAssembly, or Auto render modes) ~/Program.cs of the Server project ~/Components/App.razor of the Server project @Assets["_content/..."]
    Blazor WebAssembly standalone App Not applicable ~/wwwroot/index.html The @Assets resource collection is not available. Use the alternative cache-busting approaches.

    The rendered URL contains a fingerprint computed from the file content, so the URL changes whenever the file changes after a package upgrade. Fingerprinted assets are served with the immutable cache directive, which allows browsers to cache them safely while still receiving updated files when the content changes.

    MapStaticAssets() serves only the assets listed in the static assets manifest that is generated during build and publish. To serve files that are not part of the manifest, call UseStaticFiles() along with MapStaticAssets(). For more information, refer to Static files in ASP.NET Core.

    Runtime behavior of fingerprinted Blazor assets

    • The syncfusion-blazor.min.js bundle does not load additional Blazor JavaScript files relative to its own URL. Serving the script through a fingerprinted URL therefore does not affect component initialization or rendering.
    • Blazor theme files, such as fluent2.css and bootstrap5.css, are self-contained and do not depend on external fonts or images referenced through relative URLs for normal rendering. Serving the stylesheet through a fingerprinted URL therefore does not affect theme rendering. If a customized stylesheet references additional assets through relative URLs, reference those assets using @Assets[] or absolute paths.

    Alternative cache-busting approaches

    The @Assets[] resource collection is available in Blazor Web App applications that target .NET 9 or later. For applications that cannot use MapStaticAssets(), such as applications targeting earlier .NET versions or standalone Blazor WebAssembly applications, use one of the following approaches.

    Version query string

    Append a version query string to the script and stylesheet references, and update the value after each Blazor package upgrade. In a standalone Blazor WebAssembly application, add the references to the ~/wwwroot/index.html file, as shown in the following code snippet.

    <link href="_content/Syncfusion.Blazor.Themes/fluent2.css?v=35.2.12" rel="stylesheet" />
    
    <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js?v=35.2.12" type="text/javascript"></script>

    In standalone Blazor WebAssembly applications, reference Blazor assets using paths that begin with _content/. The ~/ prefix is not supported in wwwroot/index.html because the file is not processed by Razor.

    The asp-append-version tag helper

    In applications that render .cshtml files, such as a Blazor Server application that uses the ~/Pages/_Host.cshtml host page, use the asp-append-version tag helper. It uses IFileVersionProvider to append a content-based hash to the URL, ensuring that browsers request the updated file whenever the file content changes.

    <script src="~/_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" asp-append-version="true"></script>

    The tag helper is processed on the server. Therefore, it cannot be used in static files such as the index.html file of a standalone Blazor WebAssembly application, or in .razor files.

    Individual component script references

    Blazor components provide component-wise scripts that can be referenced externally in the application. If you are using only a few components, you can directly reference the required component scripts from the CDN or static web assets without using the CRG, instead of referencing the single script that includes all components.

    You can add a component script reference in one of the following ways based on usage:

    Usage Script reference

    Refer from static web assets

  • CSHTML
  • <head>
        ...
        <!--<script src="_content/<Package name>/scripts/<Component script name>" type="text/javascript"></script>-->
        <script src="_content/Syncfusion.Blazor.Inputs/scripts/sf-textbox.min.js" type="text/javascript"></script>
    </head>

    Refer from CDN

  • CSHTML
  • <head>
        ...
        <!--<script src="https://cdn.syncfusion.com/blazor/<Version>/<Component script name>" type="text/javascript"></script>-->
        <script src="https://cdn.syncfusion.com/blazor/35.2.12/sf-textbox.min.js" type="text/javascript"></script>
    </head>

    The following table lists the components and their script file names.

    Component Script name
    TextBox sf-textbox.min.js
    TextArea sf-textarea.min.js
    Toggle Switch Button sf-switch.min.js
    Numeric TextBox sf-numerictextbox.min.js
    Input Mask sf-maskedtextbox.min.js
    OTP Input sf-otp-input.min.js
    File Upload sf-uploader.min.js
    Range Slider sf-slider.min.js
    Calendar sf-calendar.min.js
    DatePicker sf-datepicker.min.js
    DateTime Picker sf-datepicker.min.js
    DateRangePicker sf-daterangepicker.min.js
    TimePicker sf-timepicker.min.js
    AutoComplete sf-dropdownlist.min.js
    ComboBox sf-dropdownlist.min.js
    Dropdown List sf-dropdownlist.min.js
    Multicolumn ComboBox sf-multicolumncombobox.min.js
    MultiSelect Dropdown sf-multiselect.min.js
    Mention sf-mention.min.js
    Dropdown Menu sf-drop-down-button.min.js
    Split Button sf-drop-down-button.min.js
    Progress Button sf-spinner.min.js
    ListBox sf-listbox.min.js
    Color Picker sf-colorpicker.min.js
    Signature sf-signature.min.js
    Rating sf-rating.min.js
    Speech to Text sf-speechtotext.min.js
    Context Menu sf-contextmenu.min.js
    Menu Bar sf-menu.min.js
    Breadcrumb sf-breadcrumb.min.js
    Carousel sf-carousel.min.js
    Dropdown Tree sf-dropdowntree.min.js
    Stepper sf-stepper.min.js
    Query Builder sf-querybuilder.min.js
    Grid sf-grid.min.js
    Accordion sf-accordion.min.js
    Tabs sf-tab.min.js
    Toolbar sf-toolbar.min.js
    Scheduler sf-schedule.min.js
    Barcode Generator sf-barcode.min.js
    Maps sf-maps.min.js
    Circular Gauge sf-circulargauge.min.js
    Linear Gauge sf-lineargauge.min.js
    HeatMap sf-heatmap.min.js
    Chart sf-chart.min.js
    3D Chart sf-chart3d.min.js
    Chart Wizard sf-chart-wizard.min.js
    CheckBox sf-checkbox.min.js
    Accumulation Chart sf-accumulation-chart.min.js
    Stock Chart sf-stock-chart.min.js
    Bullet Chart sf-bullet-chart.min.js
    Sparkline sf-sparkline.min.js
    Sunburst Chart sf-sunburst-chart.min.js
    TreeMap sf-treemap.min.js
    ProgressBar sf-progressbar.min.js
    Smith Chart sf-smith-chart.min.js
    Range Selector sf-range-navigator.min.js
    Sankey sf-sankey.min.js
    File Manager sf-filemanager.min.js
    Tooltip sf-tooltip.min.js
    ListView sf-listview.min.js
    Dashboard Layout sf-dashboard-layout.min.js
    Sidebar sf-sidebar.min.js
    TreeView sf-treeview.min.js
    Pivot Table sf-pivotview.min.js
    TreeGrid sf-treegrid.min.js
    Spinner sf-spinner.min.js
    Splitter sf-splitter.min.js
    Floating Action Button sf-floating-action-button.min.js
    Speed Dial sf-speeddial.min.js
    Toast sf-toast.min.js
    Dialog sf-dialog.min.js
    In-Place Editor sf-inplaceeditor.min.js
    Kanban sf-kanban.min.js
    Image Editor sf-image-editor.min.js
    Pager sf-pager.min.js
    Diagram sf-diagramcomponent.min.js
    Gantt Chart sf-gantt.min.js
    Ribbon sf-ribbon.min.js
    Rich Text Editor sf-richtexteditor.min.js
    Block Editor sf-blockeditor.min.js
    AI AssistView sf-ai-assistview.min.js
    Chat UI sf-chat-ui.min.js

    Custom Resource Generator

    Blazor provides an option to generate component interop scripts by using the Custom Resource Generator (CRG) tool. Learn how to generate component-wise scripts using CRG.

    See also