Estoy tratando de crear mapas de sitio dinámicos para mi sitio next-js con el siguiente mapa del sitio, pero tengo un problema.
He seguido los documentos y he creado lo siguiente
//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() {} El console.log en el código anterior produce (como se esperaba)
{ 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' } ] } Sin embargo, http://localhost:3000/server-sitemap-index.xml produce:
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>[object Object]</loc> </sitemap> <sitemap> <loc>[object Object]</loc> </sitemap> <sitemap> <loc>[object Object]</loc> </sitemap> </sitemapindex>El xml no se rellena correctamente. Estoy usando javascript, no mecanografiado como los ejemplos en los documentos . ¿Podría ser este realmente el problema?
PD: Todos los demás mapas de sitio (no dinámicos) salen bien.
Bien, entonces encontré el problema.
Necesitaba importar el módulo correcto, next-sitemap tiene dos API.
import { getServerSideSitemapIndex ,getServerSideSitemap } from "next-sitemap"; Necesita getServerSideSitemap .