Search code examples
c#browserblazorserver-sidebrowser-detection

What is the best way to handle different browser features within a Blazor Server-Side Application?


EDIT: I initially only wanted to know how to detect different browsers in Blazor Server-Side and change the behaviour of my app accordingly. The answer to that can be found here: How to use the HttpContext object in server-side Blazor to retrieve information about the user, user agent I then learned about feature detection from @CodeCaster and changed this question, since this seems to be a better solution for my problem.

Here is my initial post:

I want to load different contents on the starting page depending on what the browser that is used on the client side supports. (or alternatively have the client load a certain starting page depending on its browser) Something like this: Browser Detection

What is the best way to detect the browser the client is using and where would be the best location in a Blazor Server-Side App to do so?


Solution

  • What is the best way to detect the browser

    You don't, that practice from the two past decades needs to go. Use feature detection instead.

    See:

    Key point (taken from the last link):

    The problem with this approach is twofold. First, it bundles multiple assumptions about the features the browser supports in one check. A single wrong assumption can break the site. So as a developer you have to keep track of exactly which features each version of a specific browser supports.

    The second issue is that this browser check doesn’t take browser versions into consideration and therefore isn’t future-proof. Even if it works with today’s version of a browser, the next release might not require—or worse, might remove support altogether for—a workaround that the browser detection was used to add to the site.