Git Product home page Git Product logo

Comments (17)

Pvxtotal avatar Pvxtotal commented on August 20, 2024 2

I found a workaround. This is because of the new Blazor SSR, it changes the way that JS is rendered.

You need to use PageScript: https://github.com/MackinnonBuck/blazor-page-script

Import the SweetAlert2 Script in the MainLayout like this: <PageScript Src="./_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js" />

Set a RenderMode for HeadOutlet like this: <HeadOutlet @rendermode="new InteractiveAutoRenderMode(prerender: false)" />

With this trick everything should be working just fine.

This trick solved issues with all JS-based libraries I've imported.

Source: https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/static-server-rendering?view=aspnetcore-8.0

from razor.sweetalert2.

UweKeim avatar UweKeim commented on August 20, 2024 2

It seems that I solved it by not placing the <PageScript... component into the <HeadContent> tags, but simply at the very end of the "MainLayout.razor" page:

<PageScript Src="/_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js" />

I still have to have this in the "App.razor":

<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)" />

But all after all, it seems to work.

from razor.sweetalert2.

Pvxtotal avatar Pvxtotal commented on August 20, 2024 2
    <Routes />
    <script src="_framework/blazor.web.js"></script>
    <script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"></script>

    <script>
        function injectScript(url) {
            const script = document.createElement('script');
            script.src = url;
            document.head.appendChild(script);
        }

        Blazor.addEventListener('enhancedload', () => {
            const oldScript = document.querySelector('script[src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"]');
            if (oldScript) {
                oldScript.remove();
            }

            injectScript('_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js');
        });

    </script>
i've found another workaround to make it work with Blazor SSR. Just remove and insert the script on each navigation. This applies to every JS interaction with Blazor SSR

from razor.sweetalert2.

Pvxtotal avatar Pvxtotal commented on August 20, 2024 1

Any workaround? Same problem here...

from razor.sweetalert2.

filipe-augusto avatar filipe-augusto commented on August 20, 2024 1

I have the same problem.

from razor.sweetalert2.

Pvxtotal avatar Pvxtotal commented on August 20, 2024 1

@UweKeim in your case use HeadOutlet like this: <HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)" />

from razor.sweetalert2.

Pvxtotal avatar Pvxtotal commented on August 20, 2024 1
@UweKeim  Change it to: <PageScript Src="./_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js" />

./ at the start

from razor.sweetalert2.

Basaingeal avatar Basaingeal commented on August 20, 2024 1

Sorry for my absence. I'll be looking into this, but .NET 8 runtime support has been added. That may aleviate this issue, but if there is a breaking change in .NET 8 I'll need to work out a fix.

from razor.sweetalert2.

muadyb avatar muadyb commented on August 20, 2024 1

Work for me adding only this line @rendermode="@RenderMode.InteractiveServer" on app.razor in HeadOutlet and Routes also works fine with @rendermode="@(new InteractiveServerRenderMode(false))"

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />
    <link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="app.css" />
    <link rel="stylesheet" href="Biblioteca.Server.styles.css" />
    <link rel="icon" type="image/png" href="favicon.png" />
    <HeadOutlet @rendermode="@RenderMode.InteractiveServer"/>
</head>

<body>    
    <Routes @rendermode="@RenderMode.InteractiveServer" />
    <script src="_framework/blazor.web.js"></script>
    <Script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"></Script>
</body>

from razor.sweetalert2.

UweKeim avatar UweKeim commented on August 20, 2024

It seems, that all my favorite Blazor library developers have shifted priorities away from .NET 8 Blazor 😭.

from razor.sweetalert2.

UweKeim avatar UweKeim commented on August 20, 2024

In case anyone wonders: The NuGet package of PageScript is named "BlazorPageScript".

And you have to add this to "_Imports.razor":

...
@using BlazorPageScript
...

from razor.sweetalert2.

UweKeim avatar UweKeim commented on August 20, 2024

@Pvxtotal When doing your steps, I get an error:

InvalidOperationException: A component of type 'Microsoft.AspNetCore.Components.Web.HeadOutlet' has render mode 'InteractiveAutoRenderMode', but the required endpoints are not mapped on the server. When calling 'MapRazorComponents', add a call to 'AddInteractiveWebAssemblyRenderMode'. For example, 'builder.MapRazorComponents<...>.AddInteractiveWebAssemblyRenderMode()'

Could you please share some more code of your solution?

from razor.sweetalert2.

UweKeim avatar UweKeim commented on August 20, 2024

Thanks, @Pvxtotal, I've already tried that, too, without success.

I've changed the <HeadOutlet /> in mit "App.razor" to:

<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)" />

I removed this from the "App.razor":

<script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"></script>

And then I've added this to "MainLayout.razor":

<HeadContent>
	<PageScript Src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js" />
</HeadContent>

When clicking the button to show a SweetAlert2 dialog, I get this error:

Error: Microsoft.JSInterop.JSException: Could not find 'CurrieTechnologies.Razor.SweetAlert2.Fire' ('CurrieTechnologies' was undefined).

With this call stack:

Error: Could not find 'CurrieTechnologies.Razor.SweetAlert2.Fire' ('CurrieTechnologies' was undefined).
    at https://localhost:7133/_framework/blazor.web.js:1:734
    at Array.forEach (<anonymous>)
    at l.findFunction (https://localhost:7133/_framework/blazor.web.js:1:702)
    at b (https://localhost:7133/_framework/blazor.web.js:1:5445)
    at https://localhost:7133/_framework/blazor.web.js:1:3238
    at new Promise (<anonymous>)
    at y.beginInvokeJSFromDotNet (https://localhost:7133/_framework/blazor.web.js:1:3201)
    at fn._invokeClientMethod (https://localhost:7133/_framework/blazor.web.js:1:62541)
    at fn._processIncomingData (https://localhost:7133/_framework/blazor.web.js:1:60016)
    at connection.onreceive (https://localhost:7133/_framework/blazor.web.js:1:53657)
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
   at CurrieTechnologies.Razor.SweetAlert2.SweetAlertService.FireAsync(String title, String message, SweetAlertIcon icon)
   at BlazorApp6.Components.Pages.Home.<BuildRenderTree>b__0_1() in C:\Users\ukeim\Documents\Visual Studio 2022\Projects\BlazorApp6\Components\Pages\Home.razor:line 11
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

Probably I'm missing something but can't figure out what.

from razor.sweetalert2.

UweKeim avatar UweKeim commented on August 20, 2024

Thanks again, @Pvxtotal, I've also tried this without success.

from razor.sweetalert2.

Pvxtotal avatar Pvxtotal commented on August 20, 2024

@Basaingeal This is related to how Blazor SSR load scripts when enhanced navigation is on. If you disable enhanced navigation everything works fine. I still don't know any workaround besides disabling enhanced navigation or turning the entire app into Interactive.

Source: dotnet/aspnetcore#52273

from razor.sweetalert2.

jdiazc2000 avatar jdiazc2000 commented on August 20, 2024

Hi muadyb, your solution worked fine for me, Thanks four you help :)!

from razor.sweetalert2.

jdiazc2000 avatar jdiazc2000 commented on August 20, 2024

Work for me adding only this line @rendermode="@RenderMode.InteractiveServer" on app.razor in HeadOutlet and Routes also works fine with @rendermode="@(new InteractiveServerRenderMode(false))"

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />
    <link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="app.css" />
    <link rel="stylesheet" href="Biblioteca.Server.styles.css" />
    <link rel="icon" type="image/png" href="favicon.png" />
    <HeadOutlet @rendermode="@RenderMode.InteractiveServer"/>
</head>

<body>    
    <Routes @rendermode="@RenderMode.InteractiveServer" />
    <script src="_framework/blazor.web.js"></script>
    <Script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"></Script>
</body>

Hi muadyb, your solution worked fine for me, Thanks four you help :)!

from razor.sweetalert2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.