-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Influencing HTML Head from a Blazor component #10450
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
Hopefully it will work during prerendering in Blazor client side app. It is needed for SEO and social sites integration. |
Very lacking this functionality today |
Sometimes it's even needed to adjust the window.application = {
toggleAsideOnMobile() {
const className = 'kt-aside--on';
const body = document.getElementsByTagName('body')[0];
const aside = document.getElementById('kt_aside');
body.classList.toggle(className);
aside.classList.toggle(className);
return true;
},
toggleTopbarOnMobile() {
const className = 'kt-header__topbar--mobile-on';
const body = document.getElementsByTagName('body')[0];
body.classList.toggle(className);
return true;
}
}; |
@vertonghenb unless it has to be the Blazor doesn't have built-in tooling for that, but you can use something like CssBuilder. |
@chucker Yes, I know but it has to be the |
I stumbled upon this today, what I really need is the ability to write in the browser history, and I guess it simply takes to add some parameters to what already exist, the layout of the page can inspect the route and generate SEO metadata during load... It still is in a MVC controller after all... I suspect SEO engines just do a lot of requests without using JavaScript so... I don't see how that is useful, here a workaround for those who want per request page title or meta
|
@vertonghenb I don't see why anything need to be modified to achieve what you ask :
Then
|
@TrabacchinLuigi I don’t know that was possible, awesome! Thanks for sharing! |
@TrabacchinLuigi Hmm, I'm not sure if this would work, please correct me if I'm wrong. Since components can't contain |
That's a good argument! |
@dotkiwi @vertonghenb as far as i know script tags can be placed in head tag, inside body tag or after it, it just depends on when you want that script to be available and start executing (also how bad is that script, the trend of putting scripts at the end of the page started because of people writing bad libs not tied to events, and by doing so the scripts worked only if they where "rendered by the browser" after the rest of the dom), in this case it should not matter, the page is very lightweight and the majority of the content is downloaded later by Blazor ... |
Is there anyway now to add script and css references from a Razor component now? I have several Razor components with their own JavaScript files and css files. If i use these components i'll have to link to their JavaScript and css files in "index.html" manually. Is there some way this can be done from the Razor component? |
Another use case to document that the lack of this feature is impacting right now is web analytics solutions. Today, in systems like Application Insights, page views are tracked via the JavaScript SDK which can be configured to auto-track route changes and report those as page views (no problem there). The issue is the document title is used as the page view name, so looking at page views in your analytics dashboard just shows all views being for the application home page (as the title never changes). |
This is a very reasonable request. How can I add style links inside a component? FAQ page needs custom style for example. The Head tag is in _Host.cshtml and as far as I understand there is no way right now? |
This feature will be very useful in supporting RTL and LTR layout switching. |
@GioviQ I've tried this component. But this only works in the Browser and we can find that the title changes in the browser's tab. But the title WILL NOT UPDATE if someone tries to share a link through some social apps, because the title is updated using the Web Socket but the social apps only fetch the title once and will not update further. |
@walterlv I think you're right, so what is the solution? |
need a way for server to render prerender component base on first time request url. this will help seo to fetch necessary info, meanwhile allow user to serve the seamlessly |
To add to the example by @vertonghenb and when using MapFallbackToPage, the _Host.cshtml file is evaluated, then if present, the cshtml layout file is evaluated. In addition, a dependency injected service can be accessed by the blazor ServerPrerendered, and the layout page. So to influence the HTML Head when using ServerPrerendered, you make sure you are constructing the HTML Head in a cshtml layout file. You then use a transient dependency injected service to carry the value from Blazor to the layout.
|
In Blazor apps the root of the HTML document is typically controlled by the layout component. If other components need to influence the contents of the HTML Head tag (e.g. to add or change Meta or Title tags) there isn't really an established patterns to do that today.
The text was updated successfully, but these errors were encountered: