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

RegisterCustomElement stopped working in Blazor 8 #53920

Open
1 task done
rudibelt opened this issue Feb 9, 2024 · 6 comments
Open
1 task done

RegisterCustomElement stopped working in Blazor 8 #53920

rudibelt opened this issue Feb 9, 2024 · 6 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-custom-elements

Comments

@rudibelt
Copy link

rudibelt commented Feb 9, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I upgraded the solution as found in https://github.com/khalidabuhakmeh/BlazorCustomElementsSample to .NET 8. Now the customelement no longer loads. If I use the old blazor.webassembly.js the customelement loads.

Expected Behavior

I expect that the customelement is loaded.
see branch feature/net8 in repo https://github.com/rudibelt/BlazorCustomElementsSample

Steps To Reproduce

run the code in featre/net8. customelement is no longer loaded

Exceptions (if any)

No response

.NET Version

8.0.100

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Feb 9, 2024
@javiercn
Copy link
Member

@rudibelt thanks for contacting us.

Your sample doesn't setup Blazor Web correctly. As a starting point we suggest you start with a blank Blazor Web template with interactivity to begin with and add the code to register the custom elements.

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Feb 10, 2024
@rudibelt
Copy link
Author

Hi, I create a new Blazor Web App with WebAssembly Interactive rende mode. Added a component Counter and use it in an index.html for the custom CustomElement scenario. Please see https://github.com/rudibelt/BlazorAppCustomElementsNet8 for this sample and the steps I toke in the commits. The sample works when using blazor.webassembly.js but not for blazor.web.js

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Feb 11, 2024
@mkArtakMSFT mkArtakMSFT added investigate feature-blazor-custom-elements and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Feb 14, 2024
@mkArtakMSFT mkArtakMSFT added this to the 8.0.x milestone Feb 14, 2024
@mkArtakMSFT mkArtakMSFT added this to 8.0.x in Servicing Feb 14, 2024
@mkArtakMSFT mkArtakMSFT modified the milestones: 8.0.x, 9.0-preview7 Mar 25, 2024
@mkArtakMSFT mkArtakMSFT removed this from 8.0.x in Servicing Mar 25, 2024
@sebitsi
Copy link

sebitsi commented Mar 28, 2024

@mkArtakMSFT

We also encountered the same problem.
It would be greatly appreciated if the problem could be analyzed earlier and not until the 7th preview.
The use of custom web components is very useful in the transition of old applications to Blazor.
This error blocks us from performing the transition. Waiting for the final version of .NET 9 or the preview is undesirable.

@KirillFridman
Copy link

Hi, I faced the same issue migrating from 7 to 8. The hint I found out which may be useful: to make Blazor Custom elements to work one should force Blazor to setup Web Socket connection with server part (in 7 it was always the case). To do this, one can either set @rendermode InteractiveServer for the blazor page container for custom elements, or somehow force magic script _framework/blazor.web.js to do this within html page

@mkArtakMSFT mkArtakMSFT modified the milestones: 9.0-preview7, 9.0-rc1 Jul 3, 2024
@mkArtakMSFT mkArtakMSFT added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels Jul 3, 2024
@xcaptain
Copy link

Same problem here, I followed https://learn.microsoft.com/en-us/aspnet/core/blazor/components/js-spa-frameworks?view=aspnetcore-8.0

RegisterForJavaScript is working, but RegisterCustomElement not working. Custom Element is a very useful feature, I want to generate a blazor wasm component to use in wordpress, react, anguare apps.

@MackinnonBuck
Copy link
Member

The reason this doesn't always work is because blazor.web.js doesn't start an interactive runtime until an interactive component gets rendered from the server. And since it's the interactive runtime that registers custom elements in the first place, this creates a bit of a chicken and egg problem (we don't know if a custom element represents a Blazor component until we initialize the interactive runtime that registers the custom element).

We should update our docs to not suggest using a "Blazor Web"-style app for scenarios utilizing custom elements. Instead, we should show how to set up custom element scenarios utilizing blazor.server.js or blazor.webassembly.js. The primary use for Blazor custom elements is when incorporating Blazor into an existing JavaScript app, which probably means it's not already a Blazor Web app in the first place.

However, we should definitely consider adding full support in the future for custom elements and JS root components when using blazor.web.js. This would allow mixing different render modes in the same app and lazily initializing circuits or the .NET WebAssembly runtime. We could add new APIs to overcome the existing limitations preventing this from working today:

  • A declarative model for defining custom elements so that we can discover them at build time (before initializing the WebAssembly runtime, for example)
  • A way to specify a render mode when adding a root component via JS or when using a custom element

@guardrex, could we update these docs to not reference "Blazor Web App" scenarios? It's best that developers stick to blazor.server.js and blazor.webassembly.js when integrating with an existing JavaScript app (until we add better support for blazor.web.js in the future).

@MackinnonBuck MackinnonBuck added Docs This issue tracks updating documentation and removed Docs This issue tracks updating documentation labels Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-custom-elements
Projects
None yet
Development

No branches or pull requests

7 participants