【问题标题】:Gatsby react-helmet not server-side renderingGatsby react-helmet 不是服务器端渲染
【发布时间】:2019-03-06 08:32:14
【问题描述】:

我正在尝试在服务器端加载我的 react-helmet 标记,以便在构建时将这些标记注入静态 html 文件。这将允许 Facebook 等直接获取源文件并使用适当的元标记。

用这个配置我的应用程序后,我在服务器端静态渲染输出中看到的只是:

<title data-react-helmet="true"></title>

设置:

gatsby-config.js

module.exports = {
  plugins: ['gatsby-plugin-react-helmet']
}

app.tsx

import Helmet from 'react-helmet';

const Head: React.FunctionComponent<Props> = () => (
  <Helmet title="the title is big" >
    <meta name="description" content="the content" />
  </Helmet >
);

...

gatsby-ssr.js

const {Helmet} = require('react-helmet');

exports.onRenderBody = () => {
  console.log(Helmet.renderStatic())
}

**Output**
<title data-react-helmet="true"></title>

有什么想法吗?

【问题讨论】:

  • 盖茨比的头盔插件已经将头盔插入gatsby-ssr.js,因此您不需要这样做,如果您将Head 组件包含到您的页面/模板中,它应该可以工作。你如何使用你的Head 组件?
  • @DerekNguyen 这是真的,但输出仍然相同 - 没有生成标签。理论上,通过onRenderBody 记录元项目应该仍然有效吗? &lt;Head /&gt; 作为组件加载到页面模板中。
  • 你试过Helmet.renderStatic()吗?这就是gatsby plugin use
  • @DerekNguyen 是的对不起,这里写的方法不正确。
  • 啊,那是生产版本。我一直在运行gatsby develop

标签: reactjs gatsby server-side-rendering react-helmet


【解决方案1】:

您不需要拥有自己的gatsby-ssr.js 文件。通过使用gatsby-plugin-react-helmet,您就可以开始了。您的 Head 组件应该可以正常工作。

您如何看待输出?在浏览器中使用“查看源代码”?您需要查看 /public 文件夹中的 .html 文件。

【讨论】:

  • 正确,但理论上我应该仍然可以在这里记录头盔实例。无论如何,我正在以与您提到的相同的方式查看输出。不幸的是,我所有的 public/index.html 包含的是一个空的 &lt;title data-react-helmet="true"&gt;&lt;/title&gt;
  • 我也遇到了同样的问题,@DylanPeti -- 你有想过吗?
  • 这里一样,我有类似的设置和同样的问题
  • @cjones26 天哪,这是很久以前的事了,我不记得我是否正确解决了它。如果您阅读上面的 cmets,我记得在生产模式而不是开发模式下编译代码,我认为这实际上生成了正确的标签。
  • @DylanPeti -- 我能够弄清楚 -- 我没有将代码放在我的布局中并且在不同的页面上,所以它最终只是一个愚蠢的错误。至少感谢您的回答!
猜你喜欢
  • 2019-06-03
  • 2020-07-04
  • 1970-01-01
  • 2019-08-07
  • 2020-06-29
  • 2019-10-05
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
相关资源
最近更新 更多