fix: seo friendly
This commit is contained in:
+26
-25
@@ -1,25 +1,26 @@
|
||||
const { SitemapStream, streamToPromise } = require("sitemap");
|
||||
const { Readable } = require("stream");
|
||||
|
||||
export default async (req, res) => {
|
||||
// An array with your links
|
||||
const links = [
|
||||
{ url: "/page/news/36/", priority: 0.3 },
|
||||
{ url: "/page/about", priority: 0.3 },
|
||||
{ url: "/recruitment", priority: 0.3 },
|
||||
{ url: "/contact-us", priority: 0.3 }
|
||||
];
|
||||
|
||||
// Create a stream to write to
|
||||
const stream = new SitemapStream({ hostname: `https://${req.headers.host}` });
|
||||
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "application/xml",
|
||||
});
|
||||
|
||||
const xmlString = await streamToPromise(
|
||||
Readable.from(links).pipe(stream)
|
||||
).then((data) => data.toString());
|
||||
|
||||
res.end(xmlString);
|
||||
};
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user