Strapi/NextJS动态路线站点地图

发布于 2025-01-19 15:58:42 字数 358 浏览 2 评论 0原文

我正在使用 boazpoolman 的 Strapi-sitemap-plugin 为我的 NextJs 应用程序开发站点地图。问题是我有一个嵌套的动态路由,如下所示:

/articles/[categories]/[article]

该插件允许自定义网址,如下所示: url 模式image

我不确定如何将此信息传递给插件。在黑暗中相当大的镜头,但我想避免为 NextJs 应用程序生成站点地图所涉及的大量开发。

I'm using the strapi-sitemap-plugin by boazpoolman to develop a sitemap for my NextJs application. The issue is I have a nested dynamic route like so:

/articles/[categories]/[article]

The plugin allows custom urls like so:
url pattern image

I'm unsure how to pass this info to the plugin. Pretty big shot in the dark but I'd like to avoid a lot of the development involved with generating a sitemap for a NextJs app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2025-01-26 15:58:42

我建议您使用“ iamvishnusankar”的NextSitemap发电机。
github:“ https://github.com/iamvishnusankar/next-sitemap”
安装软件包“ NPM I NextSitemap”或“ Yarn Add NextSiteMap”。

在您的env文件中,创建一个site_url属性并将值设置为您的公共域。如果您要测试本地,请在“ .env.local”文件中创建此属性。还将这些值添加到您部署设置中的环境变量。

SITE_URL=https://my-url.com/

然后在您的根文件夹中创建一个“ next-sitemap.config.js”文件,然后将此代码粘贴在其中。如果您有一个大型网站设置“站点化:7000”,则可以将其遗漏。还将更改频率设置为“每日”。

/** @type {import('next-sitemap').IConfig} */
module.exports = {  
  siteUrl: process.env.SITE_URL || 'https://my-url.com/',
  generateRobotsTxt: true,
  changefreq: 'daily',
  priority: 0.7,
  sitemapSize: 7000,

  transform: async (config, path) => {
    return {
      loc: path, 
      changefreq: config.changefreq,
      priority: config.priority,
      lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
      alternateRefs: config.alternateRefs ?? [],
    }
  },

  additionalPaths: async (config) => [
    await config.transform(config, '/additional-page'),
  ],
  robotsTxtOptions: {
    policies: [
      {
        userAgent: '*',
        allow: '/',
      },
      {
        userAgent: 'test-bot',
        allow: ['/path', '/path-2'],
      },
      {
        userAgent: 'black-listed-bot',
        disallow: ['/sub-path-1', '/path-2'],
      },
    ],
    additionalSitemaps: [
      'https://doc.my-url.com/', //Add other releases or sub domains here
    ],
  },
}

然后在您的公共文件夹中创建一个“ robot.txt”文件,并在您看到的情况下设置值。这是一个示例。

# *
User-agent: *
Allow: /

# test-bot
User-agent: test-bot
Allow: /path
Allow: /path-2

# black-listed-bot
User-agent: black-listed-bot
Disallow: /sub-path-1
Disallow: /path-2

# Host
Host: https://my-url.com

# Sitemaps
Sitemap: https://my-url.com/sitemap.xml
Sitemap: https://docs.my-url.com/sitemap.xml

最后,运行“ npm run build”或“ yarn run run build”。这将在您的公共文件夹中生成和SiteMap-0.xml和/或sitemal.xml。

I recommend you use the next-sitemap generator by "iamvishnusankar".
GitHub: "https://github.com/iamvishnusankar/next-sitemap"
Install the package "npm i next-sitemap" or "yarn add next-sitemap".

In your ENV file create a SITE_URL property and set the value as your public domain. Create this property in your ".env.local" file if you're testing local. Also add these values to you Environment Variables in you deployment settings.

SITE_URL=https://my-url.com/

Then create a "next-sitemap.config.js" file in your root folder and paste this code in it. If you have a large website set the "sitemapSize: 7000", if not you can leave it out. Also set the change frequency to 'daily'.

/** @type {import('next-sitemap').IConfig} */
module.exports = {  
  siteUrl: process.env.SITE_URL || 'https://my-url.com/',
  generateRobotsTxt: true,
  changefreq: 'daily',
  priority: 0.7,
  sitemapSize: 7000,

  transform: async (config, path) => {
    return {
      loc: path, 
      changefreq: config.changefreq,
      priority: config.priority,
      lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
      alternateRefs: config.alternateRefs ?? [],
    }
  },

  additionalPaths: async (config) => [
    await config.transform(config, '/additional-page'),
  ],
  robotsTxtOptions: {
    policies: [
      {
        userAgent: '*',
        allow: '/',
      },
      {
        userAgent: 'test-bot',
        allow: ['/path', '/path-2'],
      },
      {
        userAgent: 'black-listed-bot',
        disallow: ['/sub-path-1', '/path-2'],
      },
    ],
    additionalSitemaps: [
      'https://doc.my-url.com/', //Add other releases or sub domains here
    ],
  },
}

Then create a "robot.txt' file in your public folder and set the values as you see fit. Here is an example.

# *
User-agent: *
Allow: /

# test-bot
User-agent: test-bot
Allow: /path
Allow: /path-2

# black-listed-bot
User-agent: black-listed-bot
Disallow: /sub-path-1
Disallow: /path-2

# Host
Host: https://my-url.com

# Sitemaps
Sitemap: https://my-url.com/sitemap.xml
Sitemap: https://docs.my-url.com/sitemap.xml

Finally, run "npm run build' or "yarn run build". This will generate and sitemap-0.xml and/or a sitemal.xml in your public folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文