Skip to content

Blazor: Add a possiblity to pass data from page component to main index.cshtml during prerendering #9128

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

Closed
Andrzej-W opened this issue Apr 5, 2019 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one

Comments

@Andrzej-W
Copy link

Problem to solve

We want to use Blazor app as normal web app which has to be SEO friendly. We also want to integrate this app with social sites. To achieve this we have to dynamically build <head> section of html page. We have to have different title, description and metatags for each page.

Describe the solution you'd like

We have prerendering already and main index.html file is now index.cshtml (at least in Blazor server side). One thing is missing. I would like to have some data structure (I want to define it myself) which is accessible to this main index.cshtml file and to all page files. You should make some magic and run prerendering logic first and then render the main index.cshtml file. With this functionality in place each page will be able to assign required information to fields in the data structure and then main html output will be correctly generated by index.cshtml.

This is critical if we want to have proper SEO and integration with social sites.

@mkArtakMSFT mkArtakMSFT added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Apr 5, 2019
@mkArtakMSFT mkArtakMSFT added this to the 3.0.0-preview6 milestone Apr 8, 2019
@SteveSandersonMS
Copy link
Member

In terms of the order of execution, it's problematic to do this by declaring this data inside your components. They don't start executing until the part of your page above them has already been output, which presumably is too late based on what you're trying to do (add extra tags to <head>?).

I would recommend you create your own separate way of establishing what the correct metadata is for each given URL, so this logic can be executed independently of rendering components. For example, you could implement some ASP.NET Core middleware that acquires whatever metadata you want based on the URL (even depending on DB queries, etc., if you want), and then stash that information in something like HttpContext. Then you can access the same information both during the execution of your Razor Pages and during the prerendering of your components.

@Andrzej-W
Copy link
Author

Thank you Steve for your suggestion. It is a little inconvenient to separate this logic from pages (especially when we think about performance and additional DB queries) but on the other hand it is good to set title, description and meta elements in one place. Now I have to wait for this: dotnet/AspNetCore.Docs#11366

@danroth27
Copy link
Member

Here's a free and useful tool which DevExpress has made to help solve this issue: https://community.devexpress.com/blogs/aspnet/archive/2019/07/17/devexpress-blazor-update-metadata-at-runtime-free-seo-tool.aspx

@Andrzej-W
Copy link
Author

I haven't tested this yet but it probably solves almost all problems except one: in multilingual site we have to change language in html element:

<html lang="en">
<html lang="pl">
...

Because I want to use url segments as "source of truth" for language/culture (for example: example.com/en-us/my-page) I hope I will be able to easily use this information in _Host.razor during prerendering. To implement this on the client (Web Assembly) I will try to use setAttribute in JavaScript.

@rynowak
Copy link
Member

rynowak commented Jul 29, 2019

<html lang="..."> will help the browser know the language of the response (just metadata) but it has no effect on anything happens in client or server-side Blazor. See: dotnet/AspNetCore.Docs#13436

@roduman
Copy link

roduman commented Aug 9, 2019

Do you plan to do this in Blazor itself? https://community.devexpress.com/blogs/aspnet/archive/2019/07/17/devexpress-blazor-update-metadata-at-runtime- свободный сео-tool.aspx

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

6 participants