javascript - Cannot create sitemaps on nextjs with next-sitemap - Stack Overflow

I am trying to create dynamic sitemaps for my next-js site with next-sitemap but I'm running into

I am trying to create dynamic sitemaps for my next-js site with next-sitemap but I'm running into an issue.

I have followed the docs and created the following

//pages/server-sitemap-index.xml/index.js
import axios from "axios";
import { getServerSideSitemapIndex } from "next-sitemap";

export const getServerSideProps = async (ctx) => {
  const res = await axios(`${process.env.API_ROOT}/books`);
  const { data } = res.data;
  const fields = data.map((item) => ({
    loc: `${process.env.NEXTAUTH_URL}/${item.slug}`,
    lastmod: item.updatedAt,
    priority: 0.7,
    changefreq: "daily",
  }));

  console.log({ fields });
  return getServerSideSitemapIndex(ctx, fields);
};

export default function SitemapIndex() {}

The console.log in the above code produces(as expected)

{
  fields: [
    {
      loc: 'http://localhost:3000/The-river-between-3pgkgy',
      lastmod: '2022-03-16T16:02:06.000Z',
      priority: 0.7,
      changefreq: 'daily'
    },
    {
      loc: 'http://localhost:3000/The-river-between-0tqwkgy',
      lastmod: '2022-03-16T16:02:06.000Z',
      priority: 0.7,
      changefreq: 'daily'
    },
    {
      loc: 'http://localhost:3000/The-river-between-tqwkgy',
      lastmod: '2022-03-16T16:02:06.000Z',
      priority: 0.7,
      changefreq: 'daily'
     }
  ]
}

However, http://localhost:3000/server-sitemap-index.xml produces:

<sitemapindex xmlns=".9">
<sitemap>
<loc>[object Object]</loc>
</sitemap>
<sitemap>
<loc>[object Object]</loc>
</sitemap>
<sitemap>
<loc>[object Object]</loc>
</sitemap>
</sitemapindex>

The xml is not populated properly. I am using javascript, not typescript like the examples in the docs. Could this really be the issue?

Ps: All the other sitemaps(not dynamic) e out okay.

I am trying to create dynamic sitemaps for my next-js site with next-sitemap but I'm running into an issue.

I have followed the docs and created the following

//pages/server-sitemap-index.xml/index.js
import axios from "axios";
import { getServerSideSitemapIndex } from "next-sitemap";

export const getServerSideProps = async (ctx) => {
  const res = await axios(`${process.env.API_ROOT}/books`);
  const { data } = res.data;
  const fields = data.map((item) => ({
    loc: `${process.env.NEXTAUTH_URL}/${item.slug}`,
    lastmod: item.updatedAt,
    priority: 0.7,
    changefreq: "daily",
  }));

  console.log({ fields });
  return getServerSideSitemapIndex(ctx, fields);
};

export default function SitemapIndex() {}

The console.log in the above code produces(as expected)

{
  fields: [
    {
      loc: 'http://localhost:3000/The-river-between-3pgkgy',
      lastmod: '2022-03-16T16:02:06.000Z',
      priority: 0.7,
      changefreq: 'daily'
    },
    {
      loc: 'http://localhost:3000/The-river-between-0tqwkgy',
      lastmod: '2022-03-16T16:02:06.000Z',
      priority: 0.7,
      changefreq: 'daily'
    },
    {
      loc: 'http://localhost:3000/The-river-between-tqwkgy',
      lastmod: '2022-03-16T16:02:06.000Z',
      priority: 0.7,
      changefreq: 'daily'
     }
  ]
}

However, http://localhost:3000/server-sitemap-index.xml produces:

<sitemapindex xmlns="http://www.sitemaps/schemas/sitemap/0.9">
<sitemap>
<loc>[object Object]</loc>
</sitemap>
<sitemap>
<loc>[object Object]</loc>
</sitemap>
<sitemap>
<loc>[object Object]</loc>
</sitemap>
</sitemapindex>

The xml is not populated properly. I am using javascript, not typescript like the examples in the docs. Could this really be the issue?

Ps: All the other sitemaps(not dynamic) e out okay.

Share Improve this question asked Mar 16, 2022 at 19:38 YouzefYouzef 8582 gold badges11 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Okay, so I found the problem.

I needed to import the correct module, next-sitemap has two APIs.

import { getServerSideSitemapIndex ,getServerSideSitemap } from "next-sitemap";

You need getServerSideSitemap.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231622a4617701.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信