为什么Blazor的HeadOutlet在App之后渲染?

9
我正在一个服务器端预渲染的 net6.0 应用中使用 HeadOutlet 来设置一些头部标签,例如 meta 描述,但是服务器会先呈现应用,然后再呈现头部,这会导致搜索引擎忽略它。
@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>

在浏览器中查看页面将按预期在头部渲染元标签,但在Insomnia/Postman中进行get请求会返回初始标头和一个Blazor预呈现标记评论。
<!--Blazor:{"sequence":0,"type":"server","prerenderId":"b0376004567c4aaf9c07defc4341e21e","descriptor":"<long string here>"}--><!--Blazor:{"prerenderId":"b0376004567c4aaf9c07defc4341e21e"}-->

这是一个错误还是我遗漏了什么?我需要在页面的其余部分之前或与其同时呈现头部,以便搜索引擎可以捕捉到它。

2个回答

3

1
来自链接的评论:“创建自己的应用程序的人不会想到这一点,也无法推理出更自然的应用程序结构为什么不起作用。”- 哈哈。幸好有SO。+1 - Yogi
我没有_Layout.html,我有MainLayout.razor,但是将建议放在那里只会破坏我的布局。 - Klom Dark

0
我没有_Layout文件。与其移动,我在_Host文件中完全限定了HeadOutlet的类,并使其工作。
<component type="typeof(Microsoft.AspNetCore.Components.Web.HeadOutlet)" render-mode="ServerPrerendered" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接