为什么blazor HeadOutlet是在App之后渲染的

问题描述 投票:0回答:2

我在服务器端预渲染的 net6.0 应用程序上使用 HeadOutlet 来设置一些标头标签,例如元描述,但服务器首先渲染应用程序,然后渲染标头,这使得搜索引擎忽略它。

@page "/"
@namespace Example.Pages
@using Microsoft.AspNetCore.Components.Web
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = null;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
    <base href="~/" />

    <link href="css/site.css" rel="stylesheet" />
    <link rel="preconnect" href="https://fonts.gstatic.com">
</head>
<body>
    <component type="typeof(App)" render-mode="ServerPrerendered" />

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="/" class="reload">Reload</a>
        <a href="#" class="dismiss">🗙</a>
    </div>

    <script src="_framework/blazor.server.js"></script>
    <script src="~/outsideHandleContainerJsInterop.js"></script>
</body>
</html>
@page "/test"

<HeadContent>
     <meta name="description" content="Hello World">
</HeadContent>

在浏览器中查看页面将按预期在 head 中渲染元标记,但在 insomnia/postman 中执行 get 请求会返回初始标头和 blazor 预渲染标记注释

<!--Blazor:{"sequence":0,"type":"server","prerenderId":"b0376004567c4aaf9c07defc4341e21e","descriptor":"<long string here>"}--><!--Blazor:{"prerenderId":"b0376004567c4aaf9c07defc4341e21e"}-->

这是一个错误还是我遗漏了什么?我需要在页面的其余部分之前或与页面的其余部分一起渲染头部,以便搜索引擎可以拾取它。

.net blazor server-side-rendering meta-tags head
2个回答
3
投票

通过将 html/head/body 从 _Host.cshtml 移动到 _Layout.html 解决了这个问题。本文描述的更多信息:https://github.com/dotnet/aspnetcore/issues/37293


0
投票

我没有 _Layout 文件。我没有移动,而是在 _Host 文件中完全限定了 HeadOutlet 的类并使其正常工作。

<component type="typeof(Microsoft.AspNetCore.Components.Web.HeadOutlet)" render-mode="ServerPrerendered" />
© www.soinside.com 2019 - 2024. All rights reserved.