Javascript 盖茨比反应头盔生成空<;标题>;关于SSR
我的盖茨比网站没有在SSR上生成正确的标题标签。当我建立网站时,我在生成的文件中得到的都是Javascript 盖茨比反应头盔生成空<;标题>;关于SSR,javascript,reactjs,gatsby,server-side-rendering,react-helmet,Javascript,Reactjs,Gatsby,Server Side Rendering,React Helmet,我的盖茨比网站没有在SSR上生成正确的标题标签。当我建立网站时,我在生成的文件中得到的都是。我非常感谢您帮助我发现问题并解决问题,因为经过长时间的调试,我不知道哪一个可能是问题 相关文件: package.json ... "dependencies": { "gatsby": "^2.19.45", "gatsby-image": "^2.2.44", "gatsby-plugin-manifest": "^2.2.48", "gatsby-plugin-react-helme
。我非常感谢您帮助我发现问题并解决问题,因为经过长时间的调试,我不知道哪一个可能是问题
相关文件:
package.json
...
"dependencies": {
"gatsby": "^2.19.45",
"gatsby-image": "^2.2.44",
"gatsby-plugin-manifest": "^2.2.48",
"gatsby-plugin-react-helmet": "^3.2.2",
"gatsby-plugin-sharp": "^2.4.13",
"gatsby-plugin-sitemap": "^2.3.1",
"gatsby-plugin-typescript": "^2.3.3",
"gatsby-source-filesystem": "^2.1.56",
"gatsby-source-graphql": "^2.2.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^6.0.0",
}
...
gatsby.config.js
plugins: [
`gatsby-plugin-react-helmet`,
...
]
import React from "react"
import { Helmet } from "react-helmet"
export const onRenderBody = (
{ setHeadComponents, setHtmlAttributes, setBodyAttributes },
pluginOptions
) => {
const helmet = Helmet.renderStatic()
setHtmlAttributes(helmet.htmlAttributes.toComponent())
setBodyAttributes(helmet.bodyAttributes.toComponent())
setHeadComponents([
helmet.title.toComponent(),
helmet.link.toComponent(),
helmet.meta.toComponent(),
helmet.noscript.toComponent(),
helmet.script.toComponent(),
helmet.style.toComponent()
])
}
(盖茨比插件离线已禁用)
Seo.tsx
import React from "react"
import { Helmet } from "react-helmet"
import { useStaticQuery, graphql } from "gatsby"
interface Props {
title: string
description?: string
image?: string
}
const SEO = ({ title, description, image }: Props) => {
const { site } = useStaticQuery(
graphql`
query {
site {
siteMetadata {
title
description
image
siteUrl
}
}
}
`
)
const metaDescription = description || site.siteMetadata.description
const shareImage = image || site.siteMetadata.image
const url = site.siteMetadata.siteUrl
return (
<Helmet defer={false}>
<title>{title}</title>
<meta name="description" content={metaDescription} />
<meta name="image" content={shareImage} />
<link rel="canonical" href={url} />
<meta property="og:url" content={url} />
<meta property="og:type" content="website" />
<meta property="og:title" content={title} />
<meta property="og:description" content={metaDescription} />
<meta property="og:image" content={shareImage} />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={metaDescription} />
<meta name="twitter:image" content={shareImage} />
</Helmet>
)
}
export default SEO
我仍然有空srr文件的问题
在任何给定页面上,我称之为SEO标签,例如:
<SEO title="Hello World" description="Foo Bar" />
你能试试像title={title}这样的东西吗?把它作为头盔的道具而不是像上面那样的标题标签传递进来
<Helmet
htmlAttributes={{
lang,
}}
title={title}
titleTemplate={`%s | ${site.siteMetadata.title}`}
meta={[
{
name: `description`,
content: metaDescription,
},
{
property: `og:title`,
content: title,
},
{
property: `og:description`,
content: metaDescription,
},
{
property: `og:type`,
content: `website`,
},
{
name: `twitter:card`,
content: `summary`,
},
{
name: `twitter:creator`,
content: site.siteMetadata.author,
},
{
name: `twitter:title`,
content: title,
},
{
name: `twitter:description`,
content: metaDescription,
},
].concat(meta)}
/>
盖茨比入门默认模板有一个很好的SEO组件,您也可以参考。找到了解决方案。
redux persist store
出现问题,导致SSR无法工作
完整的解决方案可以在这里的另一个问题中找到:我以前实际上是将此作为seo组件使用的,并且得到了相同的结果。SSR上相同的空白标题(没有其他标签)。