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

Identifying whether the page is server or client for RenderMode.Auto #50859

Closed
1 task done
gragra33 opened this issue Sep 21, 2023 · 6 comments
Closed
1 task done

Identifying whether the page is server or client for RenderMode.Auto #50859

gragra33 opened this issue Sep 21, 2023 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@gragra33
Copy link

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.

Whilst writing components to support all render modes is preferred, there may be cases where it is not always possible.

Describe the solution you'd like

For these cases, having a property or method with the running state for the component on the ComponentBase class would make this possible.

Additional context

A current workaround is to check the IJSRuntime interface for if it inherits IJSInProcessRuntime. Here is my current workaround: Blazor.WhereAmIRunning. The sample project visually demonstrates how it works.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Sep 21, 2023
@javiercn
Copy link
Member

@gragra33 thanks for contacting us.

You can use OperatingSystem.IsBrowser() to this effect.

@gragra33
Copy link
Author

@javiercn well, shoot ... yep, that works well. When was that added?

@javiercn
Copy link
Member

@gragra33 It's been there for a while.

We do plan to make this a more obvious concept in the API, but we haven't made it a priority since there are easy ways to figure this out.

@gragra33 gragra33 changed the title Identifying wether the page is server or client for RenderMode.Auto Identifying whether the page is server or client for RenderMode.Auto Sep 21, 2023
@guardrex
Copy link
Contributor

guardrex commented Sep 22, 2023

It's covered in our JSImport/Export JS interop article ...

https://learn.microsoft.com/aspnet/core/blazor/javascript-interoperability/import-export-interop

... but it should also appear in our JS interop overview. I'll add it there to improve its visibility.

Looks like it came in at 6.0 on #26140.

@marinasundstrom
Copy link

This is how I solved it:

I have an abstract RenderingContext class with two deriving implementations:

For Server: https://github.com/marinasundstrom/Blazor8Test/blob/main/src/Server/ServerRenderingContext.cs
For Client: https://github.com/marinasundstrom/Blazor8Test/blob/main/src/Client/ClientRenderingContext.cs

These are, of course, registered depending on context.

You can inject RenderingContext into your component, and it will resolve to the correct implementation depending on context. When the component is running on the server, the Server-specific implementation has a property that return a value indicating whether it is pre-rendering.

You can then just check the IsPrerendering property in your component.

@javiercn
Copy link
Member

Dupe of #49401

@ghost ghost locked as resolved and limited conversation to collaborators Oct 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

4 participants