-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Comments
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. |
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. |
Thanks for contacting us. We're moving this issue to the |
This is likely covered by #49401 |
@javiercn yes, maybe. But maybe it's worth considering to allow the |
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. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
In .NET 8, an internal type named
UnsupportedJavaScriptRuntime
was moved from the namespaceMicrosoft.AspNetCore.Mvc.ViewFeatures
toMicrosoft.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 overriddenOnAfterRender(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 abool IsSupported
property, and all implementations could simply return a constanttrue
, except for theUnsupportedJavaScriptRuntime
, which would returnfalse
.Additional context
To check if user code should call JavaScript at all, users have a few options:
IJSRuntime.GetType().Name == "UnsupportedJavaScriptRuntime"
. This is extremely error prone, given the type isinternal
and could change at any time, or even if there are other runtimes that could implementIJSRuntime
.InvalidOperationException
. However, it is not definite an exception of this type would only surface from the expected call and nothing else downstream. A customException
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'sMessage
, but that is exactly what made the above bug appear in MudBlazor as well.OnAfterRenderAsync
. This only works in component code, thus services or library authors have a hard time.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, becauseMicrosoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor
will add a scopedRemoteJSRuntime
(internal
as well) on top ofUnsupportedJSRuntime
(to shadow it in DI).The text was updated successfully, but these errors were encountered: