I have a Next JS application which was built using Next 12 and I am migrating it to Next 14. I have multiple sitemaps indexed in google earlier. Now I am trying to migrate them to App router logic. In my project I have a sitemap with folder path as pages/sitemaps/[categoryId]/category.xml.tsx
which was accessible in the path https://localhost:3000/sitemaps/124/category.xml
. I want to maintain the path of the sitemap in my app router as well. I created file app/sitemaps/[categoryId]/category/sitemap.ts
to keep the exact same path.
Here is my code:
//app/sitemaps/[categoryId]/category/sitemap.ts
import { MetadataRoute } from 'next';
export default async function sitemap({ params }: { params: { categoryId: string } }) : Promise<MetadataRoute.Sitemap> {
console.log("Params: ",params);
const categories:ICategory = await categories(parseInt(params.categoryId));
return categories.map(category => {
return( {
url: `${SiteURL}${getCategoryLink(category)}`,
lastModified: new Date(),
changeFrequency: 'daily',
priority: 1,
})
})
}
When I tried the link https://localhost:3000/sitemaps/124/category.xml
, it shows 404.
When I try the link http://localhost:3000/sitemaps/124/category/sitemap.xml
, it is showing error that “Params not found”. My dynamic route path is not accessible inside the files, my params value shows undefined
.
- Why does the dynamic param is not accessible inside the file?
- Does the end path have to be
[...]/sitemap.xml
always? Can’t it be any other name ?