7

I have Blazor Server Side app, where I have added JavaScript and CSS Libraries for Metronic Bootstrap Template.

The template is fully loaded when I run the app, but the JavaScript is not responding.

For example when I click on Side Menu (Arrow Icon) or Expand the Lists, or click on User Profile Drop down, all those sections are not working.

But when I comment out line, Blazor Server Library (blazor.server.js) template starts working, but other Blazor Components don't work properly (e.g. Counter).

In _Host.cshtml file Head Section added following CSS References:

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700" />    
<link href="assets/plugins/custom/fullcalendar/fullcalendar.bundle.css" rel="stylesheet" type="text/css" />
<link href="assets/plugins/global/plugins.bundle.css" rel="stylesheet" type="text/css" />
<link href="assets/css/style.bundle.css" rel="stylesheet" type="text/css" />

In Body Section added following JavaScript References.

<script src="_framework/blazor.server.js"></script>
<script src="./assets/plugins/global/plugins.bundle.js"></script>
<script src="./assets/js/scripts.bundle.js"></script>
<script src="./assets/plugins/custom/fullcalendar/fullcalendar.bundle.js"></script>
<script src="./assets/js/custom/widgets.js"></script>
<script src="./assets/js/custom/apps/chat/chat.js"></script>
<script src="./assets/js/custom/modals/create-app.js"></script>
<script src="./assets/js/custom/modals/upgrade-plan.js"></script>

I also tried the following methods, but nothing is working. Looks like Blazor Server App is Rendering before these JavaScript files are loaded into DOM. How Can I load these Scripts and work them properly. Any help will be much appriciated.

<script src="_framework/blazor.server.js" autostart="false"></script>
<script>
    Blazor.start().then(function () {
        var customScript = document.createElement('script');
        customScript.setAttribute('src', scriptURLs);
        document.head.appendChild(customScript);
    });

    const scriptURLs = [
        "./assets/plugins/global/plugins.bundle.js",
        "./assets/js/scripts.bundle.js",
        "./assets/plugins/custom/fullcalendar/fullcalendar.bundle.js",
        "./assets/js/custom/widgets.js",
        "./assets/js/custom/apps/chat/chat.js",
        "./assets/js/custom/modals/create-app.js",
        "./assets/js/custom/modals/upgrade-plan.js",
        // ...
    ];
</script>

In my MainLayout.razor

@inherits LayoutComponentBase
@inject IJSRuntime JSRuntime

<div class="d-flex flex-column flex-root">
    <div class="page d-flex flex-row flex-column-fluid">
        <MainSidebar />
        <div class="wrapper d-flex flex-column flex-row-fluid" id="kt_wrapper">
            <MainHeader />
            @Body
            <MainFooter />
        </div>
    </div>
</div>
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Usman Jalil
  • 121
  • 4
  • 10
  • Template Link https://preview.keenthemes.com/metronic8/demo1/index.html – Usman Jalil Aug 11 '21 at 16:31
  • can u check the console of your browser, and see what is the error showing there – HAJJAJ Aug 12 '21 at 05:51
  • I remember a similar issue. Moving the line all the way to the top of all my scripts inside the tags resolved the issue for me. Otherwise, you can maybe try to call your customScript creation function inside the component's OnAfterRenderAsync() method, and see if that helps. – Daniël J.M. Hoffman Aug 12 '21 at 14:29
  • MSDocs also states that your script paths should be put after the blazor script reference: https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-5.0#load-a-script-from-an-external-js-file-js-1 – Daniël J.M. Hoffman Aug 12 '21 at 14:35
  • I did all those things nothing is working. In Developer Tools, Console window I only see this error. Unchecked runtime.lastError: The message port closed before a response was received. – Usman Jalil Aug 13 '21 at 03:17
  • Have you tried to use the CDN node to load, will it work? – Tupac Aug 19 '21 at 09:44

3 Answers3

1

Just add the following:

<script>
            function InitMetronicScript() {
                $.getScript('theme/assets/plugins/global/plugins.bundle.js');
                $.getScript('theme/assets/js/scripts.bundle.js');
                $.getScript('theme/assets/js/custom/widgets.js');
</script>
    

to your _Host.cshtml file

and call it with

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    await JsRuntime.InvokeVoidAsync("InitMetronicScript");
}

in your Layout.razor and it should work fine.

Max E
  • 39
  • 3
  • Hi, I've managed to get the template working but I have this error in browser console: Uncaught ReferenceError: KTUtil is not defined. This is using demo1 but I think the scripts.demo1.js file from the template isn't included in the build. Any ideas? – ledragon Jun 04 '22 at 08:46
1

You need to start Metronic's plugin after the page is rendered and you need to compile those scripts using webpack; if you don't compile using webpack you will have the following error:

Microsoft.JSInterop.JSException: Cannot set properties of undefined (setting 'moment') TypeError: Cannot set properties of undefined (setting 'moment')

At the end of MainLayout.razor file you need to add

@inject IJSRuntime JSRuntime

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await JSRuntime.InvokeAsync<IJSObjectReference>("import", "/assets/plugins/global/plugins.bundle.js");
            await JSRuntime.InvokeAsync<IJSObjectReference>("import", "/assets/js/scripts.bundle.js");         
        }
    }
}

And your _Host.cshtml you need to let the other Metronic's scripts, like in this snippet:

<body>

    <component type="typeof(App)" render-mode="ServerPrerendered" />

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss"></a>
    </div>
    <!--begin::Javascript-->
    <script>var hostUrl = "/assets/";</script>
    <!--begin::Page Vendors Javascript(used by this page)-->
    <script src="~/assets/plugins/custom/fullcalendar/fullcalendar.bundle.js"></script>
    <script src="~/assets/plugins/custom/datatables/datatables.bundle.js"></script>
    <!--end::Page Vendors Javascript-->
    <!--begin::Page Custom Javascript(used by this page)-->
    <script src="~/assets/js/widgets.bundle.js"></script>
    <script src="~/assets/js/custom/widgets.js"></script>
    <script src="~/assets/js/custom/apps/chat/chat.js"></script>
    <script src="~/assets/js/custom/utilities/modals/upgrade-plan.js"></script>
    <script src="~/assets/js/custom/utilities/modals/users-search.js"></script>
    <script src="~/assets/js/custom/utilities/modals/new-target.js"></script>
    <!--end::Page Custom Javascript-->

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss"></a>
    </div>
    <script src="_framework/blazor.server.js"></script>
    <!--end::Javascript-->
</body>

I got it working using metronic v8.0.38 html demo5

  • Hi, I've managed to get the template working but I have this error in browser console: Uncaught ReferenceError: KTUtil is not defined. This is using demo1 but I think the scripts.demo1.js file from the template isn't included in the build. Any ideas? – ledragon Jun 04 '22 at 06:48
  • Hello, can you please mention how to compile those scripts using the webpack ? – Abanoub rafaat Jul 19 '23 at 22:36
  • in the moment I'm writing the current version is 8.2.0 – Alessandro Bernardi Jul 21 '23 at 13:14
  • in the moment I'm writing the current version is 8.2.0 || I'm using win11 and I'm following this guideline https://preview.keenthemes.com/blazor/metronic/docs/getting-started || cd Starterkit/_keenthemes/tools || npm install || 1. modify tools/package.json and remove "type": "module" || 2. in webpack.config.js I commented the lines between 114 and 133 because of an error occuring in the following command || npm run build || hope this helps... 8.1.9 was working a little bit better than 8.2.0 || maybe I'm doing something wrong... – Alessandro Bernardi Jul 21 '23 at 13:24
0

Please note that Metronic have released a Blazor sample which does everything better than I described here: https://devs.keenthemes.com/metronic/blazor/demo1/download

Cheers,

Max

Max E
  • 39
  • 3