-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Html meta tags, page title #16018
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
Comments
Good question. I was thinking about this problem in my own application and so far I have no solution. Think about server side rendering in the future. Correct meta tags are essential for SEO, for integration with Facebook/Twitter, etc. I hope that we will be able to create multilingual Blazor applications in the future. To do it correctly we have to handle lang attribute in html element. We have to be able to modify main index.html file from Blazor application and selected solution have to be compatible with server side rendering. |
Yes exactly. My vision/hope is that Blazor will be "the new way" of doing web pages. Like replacing the "old way" of doing server-rendered pages. If we(you) could find a way how to handle stuff outside the app element, like Meta tags, title etc. Then the game is afoot :-) |
I have found yet another example why we need a way to modify HTML page elements outside of our Blazor application tag. We can have an application with multiple pages and someone can add a link to Favourites in the web browser. Link should have proper title - otherwise links to different pages are indistinguishable. |
For most nontrivial Blazor apps, you'll want some notion of a data store that issues notifications when its state changes. There's an example of this in the FlightFinder sample: https://github.com/aspnet/samples/tree/master/samples/aspnetcore/blazor/FlightFinder With this kind of architecture, you can have "page title" as one of the data items tracked in the store, and can subscribe to that in your layout to change the |
@SteveSandersonMS How do you change the content of the <title> element located in in index.html? Using JS-Interop (document / page.title, I suppose?) |
@DNF-SaS Yes |
@SteveSandersonMS Thank you for suggestion. Of course we can use data store and modify what we want in JavaScript (title, metatags, etc.). But in the future we will have server side rendering. It would be nice to find a solution which will work on the client and on the server. In ordinary ASP.NET Core application I have a base ViewModel for all my pages. I use this base class in _Layout.cshtml like this: @model ViewModelBase
<!DOCTYPE html>
<html lang="@Model.Language" prefix="og: http://ogp.me/ns#">
<head>
<title>@Model.Title</title>
@if (!string.IsNullOrEmpty(Model.OgVideo))
{
<meta property="og:video" content="@Model.OgVideo" />
} As you can see I assume that some model properties (Language, Title) are always available and I render them unconditionally. On the other hand not every page has video which can be shared on social sites and og:video metatag is rendered conditionally. It would be nice to have similar experience on the client. Maybe it is possible to add some event to the router (for example OnHeadRender). In that event we should be able to render at least the opening I understand that it is not a high priority task at this stage of Blazor development, but I think it should be tracked somewhere. |
If would be great if you had a namespace (or helpers) to manage header's metadata (such as title, meta keywords), and also a set of helpers to manage Cookies, and also a wrapper to the windows javascript namepace ! |
@Andrew-MSFT SAP always face the issue meta tags and title with the SEO :( haiz.. so.. Blazor has nothing diff about that, right? |
@topnguyen Correct, the story is pretty much the same. |
I have similar problem for seo purposes, |
I used a bunch of ugly JS-Interop and pre-rendering pages as static HTML to get crawlers working around; but it's definitely not ideal. It would be really great to see a "MetaHelper" built into Blazor akin to UriHelper where I can just call |
Is there an official recommended way to solve this yet? Home.cshtml (which renders the home component) This way you create a hybrid approach, in which everything in the page is interactive (without postbacks), except links to other pages which would trigger the browser redirect to another .cshtml razor page, each one with its custom head tags. Is there a known better solution? Also even this solution doesn't work since Regards! |
Currently the simplest and cleanest solution is to use JS interop to modify the document state. We are planning to improve this, though we don't have a final design or ETA yet. |
JS Interop? What? No! You don't even need anything sophisticated to do this. Basically, create an html component, a body component, and a head component; render the html component in Index.cshtml; put a reference to the Head Component inside a Metatag Container; set the reference to "this" from the Head component upon init, and also pass this container into the Body component and whatever is inside of the body. From there you can update the Head component from within the body, which will update the SEO tags as expected as long as you invoke StateHasChanged on the Head blazor component. This works with pre-rendering, allowing you to be appropriately crawled. Here's what my Index.cshtml looks like
Then I have two classes called "html" and "metatags".
A "body" Component.
And a "head" component.
|
That's a nice way to do it where Blazor has full control over most of the document, but it could pose a mild load time issue in certain cases and still requires interop implicitly. |
@Honkmother does this work for client side blazor or only server side. And if it does work for client side blazor, can you provide a git repo showing your configuration? |
@TheFanatr
Yes it requires some minimal interop between the components; but doesn't need any Javascript.
I think this is close to as good as we can get in terms of patterns that already exists within Blazor. I wouldn't mind seeing default HTML, Body, and Head component classes with Dictionary structures, and having those references passed down a cascade or something.
re: load time: not as far as I can tell.
@smartprogrammer93
It should work for both, but I would have to double check because for all I know <app>...</app> could be a hard coded hook into the DOM; but I am almost certain if you rename App.razor to Html.razor, it will allow you to use the HTML element instead.
Client side might be a little different because you need the script tag in the document for Blazor to even boot up (where as serverside renders the components for you). I am 99% certain you can just throw the script tag inside of the loading placeholder.
After I'm done working today I will set up a repo with an example that works with preview4/5 for both the client and server.
|
@Honkmother I really like the sample code you provided, and i've tried to make attempts at replicating it based on instructions you've provided. I cant tell if preview version i'm using is an issue, or if i'm not following your steps properly. Is it possible for you to offer some more clarity to the sample you've given? Maybe a direct link to download base/template files that i may eventually modify and build on. I'm not sure what your using tag points to on I'm sorry, i just have so much confusion and there's just not enough information and resources available for blazor to properly make sense of anything. Users are writing articles on blazor, but no one is really saying where they get their data from. So their articles don't really touch on any of the concerns i share or look forward to learning more about. I didnt even realize you can only edit the html code for layout only once in I just need a little bit of direction, if you could offer any that'll help clear a bit of the fog (smoke and mirrors) |
Hello people
You guys are doing a wonderful job with Blazor :-)
I have a question regarding Html meta tags. The app itself will be placed in the app element on the page. But how about stuff in the header? Like meta tags and so on.
Let say I would like to change/update meta tag data depending on what page (in the app). Or just update the page title.
How to do this? By Javascript interop?
The text was updated successfully, but these errors were encountered: