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: 5 comments 7 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 = 5I 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.
-
|
Timed revalidation is not appropriate solution for sitemaps due to how it works. Because it wait for next visit to rebuild page. What this means in practice step by step:
The only realistic solution seems to be dynamic generation until manual revalidation is fixed |
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.jsfile now.You can check out my demo setup here: https://gist.github.com/fabiancdng/ef318c99d8a9f5efc2c48f6a06108881
If someone is still running into stale sitemaps despite the exported
revalidateconstant, 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!