Blazor <head> modification using a section-based approach#34218
Conversation
Still lots of work to do, including full prerendering support for Blazor Server.
|
@pranavkm We've identified a possibly cleaner API for this, so I'm investigating a new approach. |
pranavkm
left a comment
There was a problem hiding this comment.
This looks pretty nice.
SteveSandersonMS
left a comment
There was a problem hiding this comment.
Amazing work, @MackinnonBuck! This is a great feature. I know it's involved a lot of design but I think the end result is really superb and solves a lot of problems at once.
I posted a few other comments and suggestions but I think you should feel free to merge once they are handled to your satisfaction.
Replaced with support for '::after' pseudo-selector.
…34518) ## Description This PR introduces a way for developers to modify the HTML `<head>` contents from Blazor. ## Customer Impact With this feature, customers will be able to easily * Modify the document title (tab name). * Add link previews for links to their web app. * Optimize their website for SEO. * Dynamically change style sheets (e.g. switch between light and dark modes). Other SPA frameworks have third-party libraries enabling `<head>` modification, so this change makes Blazor an even more viable alternative. Addresses #25705
|
Hi @MackinnonBuck, is this feature (i.e.; components and capabilities) supposed to be available in .NET 6.0.100-preview.6.21355.2? I cannot seem to get it to work in my Blazor WebAssembly app. |
|
Hi @IEvangelist. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context. |
Blazor modification via new Sections API
Enables rendering to the HTML component through a new API.
PR Description
Previously, we have explored components that mutate the HTML
<head>via JavaScript interop. This created numerous challenges, including handling prerendering and dealing with asynchrony. This PR introduces mechanism for defining a<HeadOutlet>that can be rendered to by a<HeadContent>.Overview
A
<HeadOutlet>is a component whose child content is determined by a<HeadContent>component. We could dynamically render to the<head>from aCountercomponent as follows:To make
HeadRootrender to the head, we can specify it as a root component that renders to the<head>, similar to how root components are generally rendered to to the<body>. Here's how this can be done in Blazor WebAssembly:When the
::afterpseudo-selector is specified, the contents of the root component are appended to the existing head contents instead of replacing them. This allows developers to keep static head content inindex.htmlwithout having to repeat it in Blazor components.In Blazor Server, we can take a similar approach by adding a 2nd component tag helper in the
<head>in_Host.cshtml.Addresses #25705