Razor.SweetAlert2: Missing Stylesheet after navigating in .NET 8
Issue
After navigating away and back to a page, a call to Swal.FireAsync() shows an alert that has CSS missing.
- Here is a video showing the behavior.
- Here is the project that I used for the video and below screenshots.
Steps
I’ve created a new Blazor Web App with these configuration settings:
I then added
<PackageReference Include="CurrieTechnologies.Razor.SweetAlert2" Version="5.5.0" />
to my project’s CSPROJ file, added
<script src="_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js"></script>
right to the end before the </body> tag:
and added this to my “Program.cs”:
builder.Services.AddSweetAlert2(options =>
{
options.Theme = SweetAlertTheme.Dark;
options.SetThemeForColorSchemePreference(ColorScheme.Light, SweetAlertTheme.Default);
options.SetThemeForColorSchemePreference(ColorScheme.Dark, SweetAlertTheme.Dark);
});
Next, I created a simple button in “Home.razor”:
<button @onclick="@(async ()=>await Swal.FireAsync("Test!"))">Test, klick!</button>
I’ve also changed @rendermode InteractiveServer on that page. The full source code of the page looks like this:
@page "/"
@rendermode InteractiveServer
@inject SweetAlertService Swal
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<button @onclick="@(async ()=>await Swal.FireAsync("Test!"))">Test, klick!</button>
This results in this:
When I click the button, it shows this alert:
I can do this multiple times successfully.
Next, I navigate to another page:
And then I navigate back to the home page:
When I then click the button, the alert appears, but it seems to have no styling:
It is shown that the bottom left of the page.
It seems to work correctly (i.e. I can click the <kbd>OK</kbd> button to close the alert), but it looks wrong.
Expected behavior
I would expect that Razor.SweetAlert2 works every time, not just when I do not navigate.
Desktop
- OS: Windows 11 German, 64 bit
- Browser: Google Chrome Version 120.0.6099.71 (Offizieller Build) (64-Bit)
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 2
- Comments: 18 (1 by maintainers)
Commits related to this issue
- adicionando o esquema do alert. https://github.com/Basaingeal/Razor.SweetAlert2/issues/2642 — committed to filipe-augusto/IBGE-Blazor by filipe-augusto 3 years ago
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))”
</html>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
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:I still have to have this in the “App.razor”:
But all after all, it seems to work.
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.
./ at the start
@UweKeim in your case use HeadOutlet like this: <HeadOutlet @rendermode=“new InteractiveServerRenderMode(prerender: false)” />
I have the same problem.
Any workaround? Same problem here…