Possible to revalidate sitemap.xml at runtime? #50419
-
Hey everyone 👋 I love the new metadata capabilities of Next.js using the app router! However, I've been wondering whether the sitemap (using the https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generate-a-sitemap As pages might be added at runtime in my application, that would be rather important... Does anyone have any ideas/experience regarding this? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Running into this same issue now. But you can get it another way using Route Handlers: app/sitemap.xml/route.ts export async function GET(): Promise<Response> {
const headers = new Headers();
headers.set("Content-Type", "application/xml");
return new Response(
`<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://acme.com</loc>
<lastmod>${new Date().toISOString()}</lastmod>
</url>
</urlset>
`,
{
headers,
}
);
}
export const revalidate = 5 I believed that because it was a normal route, the |
Beta Was this translation helpful? Give feedback.
-
Tried using |
Beta Was this translation helpful? Give feedback.
-
Next.js 14.2.3, just put in sitemap.ts:
and sitemap.xml is revalidated in production |
Beta Was this translation helpful? Give feedback.
-
Thanks to @pawelhoros's comment, I checked out the behavior of dynamic sitemaps in the newest Next.js version (14.2.3) and it seems like it is indeed possible to specify a revalidation interval in your If someone is still running into stale sitemaps despite the exported Closing this discussion now. |
Beta Was this translation helpful? Give feedback.
Thanks to @pawelhoros's comment, I checked out the behavior of dynamic sitemaps in the newest Next.js version (14.2.3) and it seems like it is indeed possible to specify a revalidation interval in your
sitemap.ts
/sitemap.js
file now.You can check out my demo setup here: https://github.com/fabiancdng/nextjs-sitemap-reval-test
If someone is still running into stale sitemaps despite the exported
revalidate
constant, I recommend upgrading to a newer Next.js version. If you don't want to (or can't) do that, I recommend the solution proposed by @viniciusbitt using route handlers.Closing this discussion now.
Thanks to everyone who gave their input on this!