Files
trumpsignal-monorepo/apps/web/pages/api/sitemap.ts
T

97 lines
3.2 KiB
TypeScript

import type { MetadataRoute } from 'next'
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL
// In production, warn loudly but don't crash — the sitemap will use the
// fallback URL. Set NEXT_PUBLIC_SITE_URL in your deployment env to get the
// correct absolute URLs in sitemap.xml.
if (!siteUrl && process.env.NODE_ENV === 'production') {
console.warn(
'[sitemap] NEXT_PUBLIC_SITE_URL is not set — sitemap will use https://trumpsignal.com as fallback. ' +
'Set this env var in your deployment to avoid incorrect sitemap URLs.',
)
}
const base = (siteUrl || 'https://trumpsignal.com').replace(/\/$/, '')
const routes: Array<{
path: string
priority: number
freq: MetadataRoute.Sitemap[number]['changeFrequency']
}> = [
{ path: '', priority: 1.0, freq: 'hourly' },
{ path: '/trump', priority: 0.9, freq: 'hourly' },
{ path: '/macro', priority: 0.9, freq: 'daily' },
{ path: '/kol', priority: 0.9, freq: 'daily' },
{ path: '/trades', priority: 0.8, freq: 'daily' },
{ path: '/analytics', priority: 0.7, freq: 'daily' },
{ path: '/methodology', priority: 0.8, freq: 'monthly' },
{ path: '/glossary', priority: 0.8, freq: 'monthly' },
{ path: '/case-studies', priority: 0.8, freq: 'monthly' },
// NOTE: /settings is intentionally NOT listed — it's user-personalized and
// robots.ts disallows it. Listing it here while disallowing in robots is a
// self-contradicting signal to crawlers. Keep private pages out of both.
{ path: '/privacy', priority: 0.3, freq: 'yearly' },
{ path: '/terms', priority: 0.3, freq: 'yearly' },
{ path: '/contact', priority: 0.4, freq: 'monthly' },
]
// i18n shelved — only emit English routes. Add 'zh' back when content is
// actually translated (otherwise Google flags duplicate content under hreflang).
const locales = ['en']
function sitemap(): MetadataRoute.Sitemap {
const now = new Date()
const entries: MetadataRoute.Sitemap = []
// Root landing (not locale-scoped)
entries.push({
url: base,
lastModified: now,
changeFrequency: 'hourly',
priority: 1.0,
})
for (const locale of locales) {
for (const r of routes) {
entries.push({
url: `${base}/${locale}${r.path}`,
lastModified: now,
changeFrequency: r.freq,
priority: r.priority,
})
}
}
return entries
}
export default function handler(
_req: import('next').NextApiRequest,
res: import('next').NextApiResponse,
) {
res.setHeader('Content-Type', 'application/xml')
res.send(
'<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' +
sitemap()
.map(
(x) =>
'<url><loc>' +
String(x.url).replaceAll('&', '&amp;').replaceAll('<', '&lt;') +
'</loc>' +
(x.lastModified
? '<lastmod>' +
(x.lastModified instanceof Date
? x.lastModified.toISOString()
: String(x.lastModified)) +
'</lastmod>'
: '') +
(x.changeFrequency ? '<changefreq>' + x.changeFrequency + '</changefreq>' : '') +
(x.priority != null ? '<priority>' + x.priority + '</priority>' : '') +
'</url>',
)
.join('') +
'</urlset>',
)
}