Skip to content

fix: v3 Library with Blazor Web App .NET 8 #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timheuer opened this issue Aug 18, 2023 · 15 comments
Closed

fix: v3 Library with Blazor Web App .NET 8 #623

timheuer opened this issue Aug 18, 2023 · 15 comments

Comments

@timheuer
Copy link
Member

🐛 Bug Report

When using the library with the new Blazor Web App (.NET8 Preview 7) template, I'm not getting any of the styles loading like the Header, etc. The content is rendering, no errors in outputs/console log/etc. just not getting any UI that is expected.

💻 Repro or Code Sample

(install .NET 8 Preview 7)

  1. File...New Blazor Web App
  2. Add 3.0.0-RC1 package and configure project
  3. Add element to MainLayout
<FluentHeader>
    My Dashboard
</FluentHeader>

🤔 Expected Behavior

Header renders like documentation indicates
image

😯 Current Behavior

Header content renders, but nothing else
image

@vnbaaij
Copy link
Collaborator

vnbaaij commented Aug 18, 2023

There seems to be an issue with loading the needed script (_content/Microsoft.Fast.Components.FluentUI/js/web-components.min.js) from the static assets. Need to dig into that.
In the meantime, as a workaround you can add a script tag to the App.razor file which points to a hosted CDN version of the script, i.e.:

<script type="module" src="https://cdn.jsdelivr.net/npm/@@fluentui/web-components@@2.5.16/dist/web-components.min.js"></script>

That should get you in a working state again.

@danroth27
Copy link
Member

There seems to be an issue with loading the needed script (_content/Microsoft.Fast.Components.FluentUI/js/web-components.min.js) from the static assets. Need to dig into that.

@vnbaaij How does this script normally get loaded? Are you using a JavaScript initializer?

@timheuer
Copy link
Member Author

That should get you in a working state again.

Thanks @vnbaaij -- got the UI working...interactivity is not (sorting does nothing on clicking on the grid now)

@vnbaaij
Copy link
Collaborator

vnbaaij commented Aug 18, 2023

@vnbaaij How does this script normally get loaded? Are you using a JavaScript initializer?

Yes, the lib.module.js file uses beforeStart initializer to load the we components script

@vnbaaij
Copy link
Collaborator

vnbaaij commented Aug 18, 2023

Thanks @vnbaaij -- got the UI working...interactivity is not (sorting does nothing on clicking on the grid now)

The behavior has changed with . NET 8. I'm in contact with Mackinnon to see what would be a solution for this

@vnbaaij
Copy link
Collaborator

vnbaaij commented Aug 19, 2023

@timheuer this how it works with .NET 8 at the moment (JS initializers behavior is not yet finalized, see also dotnet/aspnetcore#50187)

With Blazor's server-side rendering model, JS initializers don't get called until interactive components get loaded on the page for the first time. That is, when a WebAssembly component gets added to the page, the beforeStart callback is executed and the WebAssembly runtime loads and starts, then the afterStarted callback executes. Callbacks will be re-executed if a Blazor Server component then gets added to the page.

By using the new template (which generates a SSR site) the module script (that loads the web components script and hooks up the custom events)) does not get called. Adding the web components script tag, as suggested by me before, only solves part of the problem.

I solved it in my copy of a site generated with the template as follows:
1 - Change Program.cs to add .AddServerComponents(), so from

// Add services to the container.
builder.Services.AddRazorComponents();

to

// Add services to the container.
builder.Services.AddRazorComponents().AddServerComponents();

2 - Add @attribute [RenderModeServer] to the Index page directives to force it to be a Server component so library script (.lib.module.js) gets called

I guess adding .AddWebAssemblyComponents(); to Program.cs and @attribute [RenderModeWebAssembly] to Index.razor would have the same effect.

@tlmii
Copy link
Member

tlmii commented Sep 11, 2023

@vnbaaij Just curious if there's been any progress on this issue. Looking at the related issue from aspnetcore, it doesn't look like there's been much movement.

It does appear that the above workarounds seem to work for me for individual pages. But it does NOT seem to work for me for things that are part of the MainLayout. E.g. if I have

MainLayout.razor:

<FluentMainLayout>
    <Header><h3>My Page</h3></Header>
    <Body>
        @Body
    </Body>
    <NavMenuContent>
        <FluentNavMenuLink Href="/" Text="Home" />
        <FluentNavMenuLink Href="/SomeRoute" Text="Some Route" />
    </NavMenuContent>
</FluentMainLayout>

And then on my page, I have a FluentDataGrid (details don't really matter, since it works), then:

  1. The datagrid renders and is fully interactive as far as I can tell (at minimum, sorting is working, which @timheuer noted didn't work for him until the workarounds)
  2. The nav menu renders (mostly) properly, but is not interactive at all. Collapsing the menu doesn't work, clicking the links doesnt work and it doesn't detect which page you're on.

I can't set @attribute [RenderModeServer] on the MainLayout.razor file itself (as done on the individual pages as part of the workaround), as that throws an exception.

Is there a different workaround for the layout file? Or do we need to wait for resolution of the underlying issue?

@vnbaaij
Copy link
Collaborator

vnbaaij commented Sep 11, 2023

Hi,

See also #706. We are blocked on this till .NET 8 RC2. The issue with MainLayout/@Attribute will be addressed then.

@mahald
Copy link

mahald commented Sep 16, 2023

Will we get the .net8 FluentUI version of the "Blazor-Web-App" Template fully functional and with support for SSR and a Mainlayout and NavMenu that supports SSR with RC2/RTM? Setting the MainLayout or NavMenu to @Attribute [RenderModeServer] kinda defeats the ability to choose rendermodes on a per page base. (With Add Dynamic WebAssembly component option) supported ?

@vnbaaij
Copy link
Collaborator

vnbaaij commented Sep 17, 2023

We will work on such a template, but it probably won't come before the next major release of the library (which will be .NET 8 and higher only).

As I understand it, you can't change rendermodes for child components (ie if parent gets rendermode server set, then child components get rendermode server as well) so if we would set @attribute[RenderModeServer] on MainLayout, that would indeed dictate that mode basically for the whole site. That is not the case with the NavMenu I think.

@mahald
Copy link

mahald commented Sep 17, 2023

Thank you for your encouraging feedback, @vnbaaij.

Although the NavMenu doesn't strictly impose its render mode on the 'pages', it implies that a SignalR connection would be necessary on every page where the NavMenu is featured. I am quite fond of the concept where this connection is initiated only if a particular page necessitates it, thereby avoiding unnecessary SignalR connections on WASM or SSR pages. Consequently, I am on the lookout for a NavMenu that is compatible with SSR (or WASM or static ?) mode.

@tlmii
Copy link
Member

tlmii commented Oct 12, 2023

Now that dotnet/aspnetcore#50187 has been fixed and RC2 released, is there work that needs to be done in the code here? Or should we be able to remove some of the workarounds now?

@Gareth064
Copy link

I'm running RC2 and I am getting no styles applied.
I am using FluentLayout, FluentHeader, FluentNav etc.... without setting a rendermode.

I want to use SSR for basic components like this that don't need interactivity.

@vnbaaij
Copy link
Collaborator

vnbaaij commented Nov 1, 2023

When using SSR, you will need to include the web components script. As there is no Blazor script being loaded/used, our script will also not get loaded.
Include the following in your App.razor:

<script src="_content/Microsoft.FluentUI.AspNetCore.Components/js/web-components-v2.5.16.min.js" type="module" async></script>

If you would later add interactivity, the Blazor script will kick in and try to load the web component script again but JavaScript will handle that gracefully by design.

p.s. the new V4 templates will take care of inserting the script based on the choices you make when creating an app from the template

@vnbaaij
Copy link
Collaborator

vnbaaij commented Nov 27, 2023

Closing this as there is no action from our side. There are templates from us that set everything up correctly (for v4). Rest is more a case of awareness and education.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants