Skip to content
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

MAUI Project can not import web-components script automatically #404

Closed
RiuHDuo opened this issue May 23, 2023 · 14 comments
Closed

MAUI Project can not import web-components script automatically #404

RiuHDuo opened this issue May 23, 2023 · 14 comments

Comments

@RiuHDuo
Copy link

RiuHDuo commented May 23, 2023

I created a MAUI-Blazor project with VS 2022.
and installed Microsoft.Fast.Components.FluentUI 2.3.2
image

Add @using Microsoft.Fast.Components.FluentUI in _Imports.razor.
image

Add tag in index.html
image

Followed documents modified Promgram.cs
image

Add sample code in index.razor

<FluentCard Style="padding: 1.5rem; width: 400px; height: 250px; ">
  <h2>Hello World!</h2>
  <FluentButton Appearance="@Appearance.Accent">Click Me</FluentButton>
</FluentCard>

App can not work well.
image

If add web-component script in index.html.
image

it will work well.
image

@RiuHDuo RiuHDuo changed the title MAUI Project can not import web-components script.automatically MAUI Project can not import web-components script automatically May 23, 2023
@LuohuaRain
Copy link
Contributor

LuohuaRain commented May 31, 2023

In v2.3.4, this issue can still be reproduced. And same with (WPF) Blazor app

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });

        builder.Services.AddMauiBlazorWebView();

        builder.Services.AddFluentUIComponents(options =>
        {
            options.HostingModel = BlazorHostingModel.Hybrid;
            options.IconConfiguration = new(true)
            {
                Sizes = new[] { IconSize.Size20, IconSize.Size24, IconSize.Size32 },
                Variants = new[] { IconVariant.Filled, IconVariant.Regular }
            };
            options.EmojiConfiguration = new(true)
            {
                Groups = new[] { EmojiGroup.Activities, EmojiGroup.Flags },
                Styles = new[] { EmojiStyle.Color }
            };
        });

        builder.Services.AddScoped<IStaticAssetService, FileBasedStaticAssetService>();

#if DEBUG
        builder.Services.AddBlazorWebViewDeveloperTools();
        builder.Logging.AddDebug();
#endif


        return builder.Build();
    }
}

@andreisaperski
Copy link
Contributor

Looks like the root cause for this issue is the same as for #327.

Microsoft.Fast.Components.FluentUI package has an initializer Microsoft.Fast.Components.FluentUI.lib.module.js which loads web-components.js, but this initializer is ignored by Blazor Hybrid.

Build generates [AppName].modules.json file (bin[Configuration][FW][Platform]\AppX\wwwroot[AppName].modules.json) and populates it with found initilizers, but blazor.webview.js doesn't use this file.
It loads initializers from '_framework/blazor.modules.json' instead. blazor.modules.json contains an empty array, it's embeded as resource in Microsoft.AspNetCore.Components.WebView and provided to blazor.webview.js by WebViewManager as is - array of initializers is always empty.

As sort of PoC, replaced '_framework/blazor.modules.json' with 'MauiApp1.modules.json', rebuilt Microsoft.AspNetCore.Components.WebView and copied new version into bin folder:

image

Microsoft.Fast.Components.FluentUI.lib.module.js was loaded and controls initialized properly

@vnbaaij
Copy link
Collaborator

vnbaaij commented May 31, 2023

@andreisaperski Can you test if custom web component events (like accordion change event) work with this setup as well?

@andreisaperski
Copy link
Contributor

Copied content of AccordionDefault.razor into Index.razor of MAUI App, initial state:

image

After click on 'Panel two':
image

Handler for @onaccordionchange was called and Accordion raised OnAccordionItemChange

@vnbaaij
Copy link
Collaborator

vnbaaij commented May 31, 2023

So if people want to use the library in MAUI, we just need to tell them to compile their own WebView and everything works! 🤣🤣

@andreisaperski
Copy link
Contributor

There's another option )
image

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jun 2, 2023

@andreisaperski please chime in on the issue in the aspnetcore repo I linked to above

@andreisaperski
Copy link
Contributor

@vnbaaij not sure if i can add anything there, the comment you linked there, reflects all i have

@andreisaperski
Copy link
Contributor

There's a simpler workaround: intercept '_framework/blazor.modules.json' and provide proper JS initializers file (created by build). You can drop this initializersLoader.windows.js into wwwroot folder of app and add script tag for it to index.html right before '_framework/blazor.webview.js' tag:

<script app-name="FluentUI.Demo.Hybrid.MAUI" src="./_content/FluentUI.Demo.Hybrid.Shared/js/initializersLoader.windows.js"></script>
<script src="_framework/blazor.webview.js"></script>

'app-name' attribute needs to match app's assembly name - initializersLoader uses 'app-name' to resolve name of the file with initializers.
initializersLoader replaces 'fetch' function with another one which provides the right file instead of empty blazor.modules.json. 'fetch' is restored to its original state once '_framework/blazor.modules.json' request is intercepted.

When original issue is resolved, all that need to be done is to remove the tag and the script.

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jun 9, 2023

Adding these instructions to the Blazor Hybrid section in the readme!!

@vnbaaij
Copy link
Collaborator

vnbaaij commented Jun 10, 2023

Closing this as we now have a workaround. See https://github.com/microsoft/fluentui-blazor#tempory-workaround-for-maui-issues for the details

@Kalyxt
Copy link

Kalyxt commented Sep 11, 2024

There's a simpler workaround: intercept '_framework/blazor.modules.json' and provide proper JS initializers file (created by build). You can drop this initializersLoader.windows.js into wwwroot folder of app and add script tag for it to index.html right before '_framework/blazor.webview.js' tag:

<script app-name="FluentUI.Demo.Hybrid.MAUI" src="./_content/FluentUI.Demo.Hybrid.Shared/js/initializersLoader.windows.js"></script>
<script src="_framework/blazor.webview.js"></script>

'app-name' attribute needs to match app's assembly name - initializersLoader uses 'app-name' to resolve name of the file with initializers. initializersLoader replaces 'fetch' function with another one which provides the right file instead of empty blazor.modules.json. 'fetch' is restored to its original state once '_framework/blazor.modules.json' request is intercepted.

When original issue is resolved, all that need to be done is to remove the tag and the script.

Doesn't work for me. I am running it on android emulator and all I get is [chromium] [INFO:CONSOLE(1)] "Uncaught (in promise) SyntaxError: Unexpected token ?", source: https://0.0.0.0/_content/Microsoft.FluentUI.AspNetCore.Components/Microsoft.FluentUI.AspNetCore.Components.lib.module.js (1) . App is then frozen at startup. I am using one of Geralds projects in MAUI blazor hybrid workshop.

edgeinspector
monkeyfinder

@vnbaaij
Copy link
Collaborator

vnbaaij commented Sep 11, 2024

@Kalyxt is this on .NET 8 or 9?

@Kalyxt
Copy link

Kalyxt commented Sep 12, 2024

@vnbaaij .NET 8

<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.9.3" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="8.0.82" />

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
@RiuHDuo @vnbaaij @Kalyxt @andreisaperski @LuohuaRain and others