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

Ability to check for JavaScript runtime support #49225

Closed
1 task done
yugabe opened this issue Jul 5, 2023 · 6 comments
Closed
1 task done

Ability to check for JavaScript runtime support #49225

yugabe opened this issue Jul 5, 2023 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one Pillar: Complete Blazor Web Priority:1 Work that is critical for the release, but we could probably ship without

Comments

@yugabe
Copy link

yugabe commented Jul 5, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

In .NET 8, an internal type named UnsupportedJavaScriptRuntime was moved from the namespace Microsoft.AspNetCore.Mvc.ViewFeatures to Microsoft.AspNetCore.Components.Endpoints, and an exception message text was changed. The latter led to a downstream bug surfacing in MudBlazor.

Library authors, but not only them, should be able to decide at runtime if a given IJSRuntime instance is able to run interop requests or not. Currently, the only recommended way to safely run JavaScript code in both Blazor Server and WebAssembly is to run it in the component's overridden OnAfterRender(bool firstRender) method. This is in many cases undesirable (service calls, timers, library authoring etc.).

It is very difficult and error-prone to decide if JavaScript should be called in some cases. Especially during prerendering, JavaScript operations are not available. For this reason, it should be possible to decide if JavaScript operations are supported in a given context.

Describe the solution you'd like

The IJSRuntime interface could be extended with a bool IsSupported property, and all implementations could simply return a constant true, except for the UnsupportedJavaScriptRuntime, which would return false.

Additional context

To check if user code should call JavaScript at all, users have a few options:

  • Check if IJSRuntime.GetType().Name == "UnsupportedJavaScriptRuntime". This is extremely error prone, given the type is internal and could change at any time, or even if there are other runtimes that could implement IJSRuntime.
  • Simply try calling into JavaScript and catch the resulting InvalidOperationException. However, it is not definite an exception of this type would only surface from the expected call and nothing else downstream. A custom Exception type would probably solve this issue as well (but a simple flag check should be more explicit, cleaner and more performant/efficient than an exception-driven solution). One could also check for the exception's Message, but that is exactly what made the above bug appear in MudBlazor as well.
  • Only call JavaScript in OnAfterRenderAsync. This only works in component code, thus services or library authors have a hard time.
  • Check if running in a browser: checking for System.OperatingSystem.IsBrowser() (or a compile time equivalent) is not correct because Blazor Server has its own implementation as well. This won't produce false positives, but will produce false negatives. Nor is it enough to check if currently running in Blazor Server, because Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor will add a scoped RemoteJSRuntime (internal as well) on top of UnsupportedJSRuntime (to shadow it in DI).
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jul 5, 2023
@mkArtakMSFT mkArtakMSFT added the enhancement This issue represents an ask for new feature or an enhancement to an existing one label Jul 5, 2023
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Jul 5, 2023
@ghost
Copy link

ghost commented Jul 5, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@ScarletKuro
Copy link

ScarletKuro commented Jul 18, 2023

For this reason, it should be possible to decide if JavaScript operations are supported in a given context (especially during prerendering).

Similar issue #49497 but offers alternative design by adding lifecycle method instead of some property, but with help of it you would be able to understand when you are attached to the browser as well.

@ghost
Copy link

ghost commented Jan 3, 2024

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@mkArtakMSFT mkArtakMSFT added the Priority:1 Work that is critical for the release, but we could probably ship without label Jan 16, 2024
@javiercn
Copy link
Member

This is likely covered by #49401

@yugabe
Copy link
Author

yugabe commented Jan 25, 2024

@javiercn yes, maybe. But maybe it's worth considering to allow the IJSRuntime and related symbols to be agnostic from rendering concepts? Web workers, alternative (to Blazor) frameworks, rendering-agnostic features etc.

@mkArtakMSFT
Copy link
Member

Closing this as we think the API we've added as part of #49401 is good enough. If not, we'll find out in the future and react accordingly.

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 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 enhancement This issue represents an ask for new feature or an enhancement to an existing one Pillar: Complete Blazor Web Priority:1 Work that is critical for the release, but we could probably ship without
Projects
None yet
Development

No branches or pull requests

5 participants