Multi-tenant app Dynamic Sitemap Trouble
Answered
Western thatching ant posted this in #help-forum
Western thatching antOP
I'm using the nextJS App Router for this. It's a multi-tenant app the structure is app/[degreeDomain] and i've managed to find on the docs how to generate a static sitemap but the issue is that i cannot figure out a way to pass to this file the domainname. NextReq says undefined, i'm unable to effectively implement a cookie in my middleware. Here's what app/[dynamicDegree]/sitemap.ts looks like:
Since my pages are static for every domain, i could easily use this static template the only issue is i can't for the life of me figure out how this file can access the current domain. Can anyone help?
import { MetadataRoute } from 'next'
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://acme.com',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
},
{
url: 'https://acme.com/about',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
{
url: 'https://acme.com/blog',
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 0.5,
},
]
}Since my pages are static for every domain, i could easily use this static template the only issue is i can't for the life of me figure out how this file can access the current domain. Can anyone help?
Answered by Western thatching ant
Figured it out for anyone whoever has this issue too:
// app/[domain]/sitemap.xml/route.ts
import { type NextRequest } from "next/server";
export async function GET(req: NextRequest) {
const domain = req.headers.get('host'); // Extract the hostname (domain) from the request
// Define your static pages here
const staticPages = [
"",
"about"
// Add more pages as needed
];
// Generate sitemap entries
const sitemapEntries = staticPages
.map(
(pagePath) => `
<url>
<loc>https://${domain}/${pagePath}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<priority>1</priority>
<changefreq>monthly</changefreq>
</url>
`
)
.join("");
// Construct sitemap XML
const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemapEntries}
</urlset>`;
return new Response(sitemapXml, {
headers: {
"Content-Type": "application/xml",
},
});
}1 Reply
Western thatching antOP
Figured it out for anyone whoever has this issue too:
// app/[domain]/sitemap.xml/route.ts
import { type NextRequest } from "next/server";
export async function GET(req: NextRequest) {
const domain = req.headers.get('host'); // Extract the hostname (domain) from the request
// Define your static pages here
const staticPages = [
"",
"about"
// Add more pages as needed
];
// Generate sitemap entries
const sitemapEntries = staticPages
.map(
(pagePath) => `
<url>
<loc>https://${domain}/${pagePath}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<priority>1</priority>
<changefreq>monthly</changefreq>
</url>
`
)
.join("");
// Construct sitemap XML
const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemapEntries}
</urlset>`;
return new Response(sitemapXml, {
headers: {
"Content-Type": "application/xml",
},
});
}Answer