Meta og 标签无法使用 Next.js甚至 facebook 调试器



我正在使用React+NextJS(均为最新版本((不使用redux等(

我需要动态更改元标签的内容

当我渲染页面并通过chrome Dev工具观察时,Meta标签成功创建但当我在Slack、FB等网站上提供url时,它不起作用。。此外,Facebook OpenGraph调试器无法检查我的日志标签

需要一些帮助

这是我的代码

[id].jsx>

const Main = () => {
....
return (
<>
<MetaSEO
title={response.data.seo ? response.data.seo['page-title'] : SEOSheet.seo.title}
keywords={response.data.seo ? response.data.seo.keywords : SEOSheet.seo.keywords}
description={response.data.seo ? response.data.seo.description : SEOSheet.seo.description}
ogType={SEOSheet.seo['sns-type']}
ogTitle={response.data.seo ? response.data.seo['sns-title'] : SEOSheet.seo['sns-title']}
ogDescription={response.data.seo ? response.data.seo['sns-description'] : SEOSheet.seo['sns-description']}
ogImage={SEOSheet.seo['sns-image']}
ogUrl={SEOSheet.seo['sns-url']}
indexing="all"
/>
.......
</>
);
}

MetaSEO.jsx>

import Head from 'next/head';
const MetaSEO = ({
title, keywords, description, ogTitle, ogDescription, ogImage, ogUrl, indexing
}) => (
<Head>
<title>{title}</title>
<meta name="keywords" content={keywords} />
<meta name="description" content={description} />
<meta property="og:type" content="website" />
<meta property="og:title" content={ogTitle} />
<meta property="og:description" content={ogDescription} />
<meta property="og:image" content={ogImage} />
<meta property="og:url" content={ogUrl} />
<meta name="robots" content={indexing} />
</Head>
);
export default MetaSEO;

努力解决我做了->-使用NextSEO-在所有页面添加了元标记-已使用getInitialProps

您需要使用getServerSidePropsgetStaticProps获取数据,然后通过props将其传递给组件,以便正确呈现元标记,您可以使用View Source而不是使用Dev Tools进行验证。

---已解决---有一些代码阻止组件呈现(如"return false"(

最新更新