Missing Stylesheet after navigating in .NET 8
UweKeim opened this issue · comments
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:
![image](https://private-user-images.githubusercontent.com/415439/289372293-2334ced3-3a3c-4eb2-b86a-fb5cc0d56d28.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyMjkzLTIzMzRjZWQzLTNhM2MtNGViMi1iODZhLWZiNWNjMGQ1NmQyOC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lMDQ0ZWE2YTAzY2ViMGRiYmE3ZjNkYTgwNzEyMWIyNTI0Yzk1MmU5YmRjMmRiY2E4ZTU2MDZjMmUyZDcyYzM4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.wPO0UbnZXdr0MlXvpuUrX7dRkLyPrWqdwx74EDA41iM)
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:
![image](https://private-user-images.githubusercontent.com/415439/289372394-7467cea7-9a1a-4c47-a06e-5c24f73d4c06.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyMzk0LTc0NjdjZWE3LTlhMWEtNGM0Ny1hMDZlLTVjMjRmNzNkNGMwNi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT04NTcwNWUyMDdlMjVlN2ZlNDVkNDlkNjE0NDQyZDE4NjgxMDNmMDg4MzY2OGI4ZmY1M2M5MDI4OTc0YzY0NTBkJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.-Yx38-5ueNoUjw4PV-_Am6NnvoxytiRGdmB2lf09ZI0)
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:
![image](https://private-user-images.githubusercontent.com/415439/289372555-da331ed3-e76c-401e-baca-8b208cbfa15e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyNTU1LWRhMzMxZWQzLWU3NmMtNDAxZS1iYWNhLThiMjA4Y2JmYTE1ZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mNGU1YTNjMGY4OWIwMzc1Y2Q0YjQwMjA0NTBkOWI5MWRiOWI0OWEzNmI2ODgyMmIzZTk5ZTE4OTNmODBiYmM5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.l6vdUqxle1RwJfVsCTR7W71xeG7-bQHppwYfo9t4XaU)
When I click the button, it shows this alert:
![image](https://private-user-images.githubusercontent.com/415439/289372618-c7ca9e65-a8cd-48fa-8590-8968de32d51b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyNjE4LWM3Y2E5ZTY1LWE4Y2QtNDhmYS04NTkwLTg5NjhkZTMyZDUxYi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xZmFhNjc2OThiZjQ4MGFmMDRlYTVhZTdmZmU4NjgxNDZlZmE5Y2YyNzQ2MGNlNzg4ZDY2M2YzODVlZDhkNGJiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.apVfs7vZZwWm8JbPOUFCIq-WUa-0y_GrY7MHu235Krg)
I can do this multiple times successfully.
Next, I navigate to another page:
![image](https://private-user-images.githubusercontent.com/415439/289372644-4e70f372-ee34-4c23-a444-804158819b57.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyNjQ0LTRlNzBmMzcyLWVlMzQtNGMyMy1hNDQ0LTgwNDE1ODgxOWI1Ny5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wNzU4MTk0ZjFhYzI3MWU2YWIzZWM4NGIxY2M5OTQwYzY2MzUzZmY0N2Q2MzIzODRmMDdlMjVmZDFlMjk0NjhlJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.La57MOX3MhbuUWiTbl6aq6_ylBgkpUt1mOqPyhPET78)
And then I navigate back to the home page:
![image](https://private-user-images.githubusercontent.com/415439/289372666-34c575a2-b882-4d25-a5eb-8c17916ff745.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyNjY2LTM0YzU3NWEyLWI4ODItNGQyNS1hNWViLThjMTc5MTZmZjc0NS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yYjVlMGQ2NTE4OTM0NDQ1NTIyZmVhOTNhYjY1NzY3ZWNkYjRlM2YwMDI3NDJiN2Y3NjU1ZWYwMDcxYTBhYzliJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.2seaha3GVp6DgAuFHoAVA8TUgkbdpDaxT8JknlXkDKY)
When I then click the button, the alert appears, but it seems to have no styling:
![image](https://private-user-images.githubusercontent.com/415439/289372686-df7ea15c-a774-46a3-b72d-c828ce0c3ca0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkwNjQ1NTcsIm5iZiI6MTcxOTA2NDI1NywicGF0aCI6Ii80MTU0MzkvMjg5MzcyNjg2LWRmN2VhMTVjLWE3NzQtNDZhMy1iNzJkLWM4MjhjZTBjM2NhMC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyMlQxMzUwNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xZmViOTQxN2FhN2U5N2NiM2I1NDJhZWY3MzU0OTk4YzQ2YWEyZmE0Y2QyMDM0NDlmM2NmMzc2NDlkMDAzOTk3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.V4Py-naSSzWzMkJN6cm_NaOfmh7Eyz-FUYE8WIGPSGU)
It is shown that the bottom left of the page.
It seems to work correctly (i.e. I can click the OK 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)
Any workaround? Same problem here...
It seems, that all my favorite Blazor library developers have shifted priorities away from .NET 8 Blazor 😭.
I have the same problem.
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.
In case anyone wonders: The NuGet package of PageScript is named "BlazorPageScript
".
And you have to add this to "_Imports.razor":
...
@using BlazorPageScript
...
@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?
@UweKeim in your case use HeadOutlet like this: <HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)" />
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.
@UweKeim Change it to: <PageScript Src="./_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js" />
./ at the start
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.
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.
@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
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 :)!
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 :)!
<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