2026-10-17 17:20:59 +03:30
|
|
|
import axios from 'axios'
|
|
|
|
|
import { SitemapStream, streamToPromise } from 'sitemap'
|
|
|
|
|
import { Readable } from 'stream'
|
|
|
|
|
import { AppUrl } from '../../lib/api'
|
|
|
|
|
import { SITE_URL, sitemapLinks } from '../../lib/seo'
|
|
|
|
|
|
|
|
|
|
export default async function sitemap(req, res) {
|
|
|
|
|
let news = []
|
|
|
|
|
let newsAvailable = false
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await axios.get(AppUrl('news'), { timeout: 5000 })
|
|
|
|
|
const posts = Array.isArray(data) ? data : data && data.data
|
|
|
|
|
if (!Array.isArray(posts)) throw new Error('Unexpected news response')
|
|
|
|
|
news = posts
|
|
|
|
|
newsAvailable = true
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// Keep core pages discoverable during a news API outage; retry soon.
|
|
|
|
|
console.warn('Sitemap: news API unavailable:', error.message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stream = new SitemapStream({ hostname: SITE_URL })
|
|
|
|
|
const xml = await streamToPromise(Readable.from(sitemapLinks(news).map(url => ({ url }))).pipe(stream))
|
|
|
|
|
res.setHeader('Content-Type', 'application/xml; charset=utf-8')
|
|
|
|
|
res.setHeader('Cache-Control', newsAvailable ? 'public, s-maxage=3600, stale-while-revalidate=86400' : 'public, s-maxage=60')
|
|
|
|
|
res.status(200).send(xml.toString())
|
|
|
|
|
}
|