简体   繁体   English

如何在 NextJS 中使用 getServerSideProps() 改进 SEO?

[英]How to improve SEO with getServerSideProps() in NextJS?

I have a website that will show dynamic data (Articles), ie data that changes hourly.我有一个网站会显示动态数据(文章),即每小时变化的数据。

I therefore have opted to use getServerSideProps() from NextJS.因此,我选择使用 NextJS 的getServerSideProps()

export async function getServerSideProps() {
    const url = process.env.NEXT_PUBLIC_API_URL

    const articles = await fetch(url + 'article').then(res => res.json()).catch(err => console.error(err))

    console.log("articles", articles);

    return {
        props: {
            articles,
        }
    }
}

I could not use getStaticPaths() because the data changes so frequently, an hour after building the site and all the static paths would point to out-of-date articles, and all the new articles would not have any paths pointing to them.我无法使用getStaticPaths()因为数据变化如此频繁,在构建站点一小时后,所有 static 路径将指向过期文章,并且所有新文章都没有任何指向它们的路径。 I also looked at ISR however this also wouldn't work as it relies on knowing the paths in advance.我还查看了ISR ,但这也行不通,因为它依赖于提前知道路径。

My issue arises that because getServerSideProps() is completely dynamic, search engines do not see any of the dynamic pages (which is pretty much the entire site), and therefore I do not rank well.我的问题出现了,因为getServerSideProps()是完全动态的,搜索引擎看不到任何动态页面(几乎是整个网站),因此我排名不高。

Is there a way to use getServerSideProps() with some kind of caching, that will allow search engines to see the pages?有没有办法使用带有某种缓存的getServerSideProps() ,这将允许搜索引擎查看页面? If not, is there an alternative framework I can use that allows dynamic pages while retaining some SEO performance?如果没有,是否有我可以使用的替代框架允许动态页面同时保留一些 SEO 性能?

First of all, reduce the number of dynamic pages.首先,减少动态页面的数量。 too many dynamic pages will put your app at risk of cloaking and google hates it.太多的动态页面会让你的应用面临被cloaking的风险,谷歌讨厌它。

Generate sitemap.生成站点地图。 sitemap will improve the SEO but it is very hard for getServerSideProps because those functions are not available during build time.站点地图将改善 SEO,但getServerSideProps很难,因为这些功能在构建期间不可用。 Use this npm package: next-sitemap .使用这个 npm package: next-sitemap it helps for dynamic pages too.它也有助于动态页面。

Server side index-sitemaps (getServerSideSitemapIndex) Here's a sample script to generate index-sitemap on server side.Create pages/server-sitemap-index.xml/index.tsx page and add the following content.服务器端索引站点地图 (getServerSideSitemapIndex) 这是在服务器端生成索引站点地图的示例脚本。创建 pages/server-sitemap-index.xml/index.tsx 页面并添加以下内容。

// pages/server-sitemap-index.xml/index.tsx
import { getServerSideSitemapIndex } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
  // Method to source urls from cms
  // const urls = await fetch('https//example.com/api')

  return getServerSideSitemapIndex(ctx, [
    'https://example.com/path-1.xml',
    'https://example.com/path-2.xml',
  ])
}

// Default export to prevent next.js errors
export default function SitemapIndex() {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM