Next.js Discord

Discord Forum

return type wrong in MetadataRoute.Sitemap

Answered
Pembroke Welsh Corgi posted this in #help-forum
Open in Discord
Pembroke Welsh CorgiOP
I'm getting error:
Type '{ url: string; lastModified: Date; changeFrequency: string; priority: number; }[][]' is not assignable to type 'SitemapFile'.
I know I'm close, but unsure what I'm doing wrong here:
code in screenshot and as follows. sitemap.tsx

import { MetadataRoute } from 'next' import fs from 'fs'; import path from 'path'; export default function sitemap(): MetadataRoute.Sitemap { const postsDirectory = path.join(process.cwd(), 'blogposts'); const fileNames = fs.readdirSync(postsDirectory); const postNames = fileNames.map((fileName) => { // Remove .md from file name to get id const id = fileName.replace(/\.md$/, ''); return [${id}] }); const postsSiteMap = postNames.map((post) => { return [{ url:https://rwsnakecatcherbrisbane.com.au/${post}`,
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 0.5,
},]
})

return postsSiteMap;
}`
Answered by riský
lol this was fun to fix... i believe i added satisfies MetadataRoute.Sitemap[0] to the map
View full answer

2 Replies

Answer
@riský lol this was fun to fix... i believe i added `satisfies MetadataRoute.Sitemap[0]` to the map
Pembroke Welsh CorgiOP
Thanks, I was able to make that work as per your last bit. I'm not using a database, just markdown files for posts. So code ended up as:
import { MetadataRoute } from 'next' import fs from 'fs'; import path from 'path'; export default function sitemap(): MetadataRoute.Sitemap { const postsDirectory = path.join(process.cwd(), 'blogposts'); const fileNames = fs.readdirSync(postsDirectory); const postNames = fileNames.map((fileName) => { // Remove .md from file name to get id const id = fileName.replace(/\.md$/, ''); return [${id}] }); return [ { url: "https://example.com.au/", lastModified: new Date(), changeFrequency: 'monthly', priority: 1, }, ...postNames.map((p) => { return { url:https://example.com.au/posts/${p}`,
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 0.5,
} satisfies MetadataRoute.Sitemap[0]
})
]
}`