Skip to content

Commit

Permalink
#557761: fixed sitemap handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sc-ruslanmatkovskyi committed Nov 3, 2022
1 parent 0416c4a commit a90a376
Showing 1 changed file with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,41 @@ const sitemapApi = async (
siteName: config.jssAppName,
});

const sitemapPath = await sitemapXmlService.getSitemap(id as string);
if (id) {
const sitemapPath = await sitemapXmlService.getSitemap(id as string);

if (sitemapPath) {
const isAbsoluteUrl = sitemapPath.match(ABSOLUTE_URL_REGEXP);
const sitemapUrl = isAbsoluteUrl ? sitemapPath : `${config.sitecoreApiHost}${sitemapPath}`;
res.setHeader('Content-Type', 'text/xml;charset=utf-8');
if (sitemapPath) {
const isAbsoluteUrl = sitemapPath.match(ABSOLUTE_URL_REGEXP);
const sitemapUrl = isAbsoluteUrl ? sitemapPath : `${config.sitecoreApiHost}${sitemapPath}`;
res.setHeader('Content-Type', 'text/xml;charset=utf-8');

return new AxiosDataFetcher()
.get(sitemapUrl, {
responseType: 'stream',
})
.then((response: AxiosResponse) => {
response.data.pipe(res);
})
.catch(() => res.redirect('/404'));
}
return new AxiosDataFetcher()
.get(sitemapUrl, {
responseType: 'stream',
})
.then((response: AxiosResponse) => {
response.data.pipe(res);
})
.catch(() => res.redirect('/404'));
}
} else {
const sitemaps = await sitemapXmlService.fetchSitemaps();

const SitemapLinks = sitemaps
.map(
(item) =>
`<sitemap>
<loc>${item}</loc>
</sitemap>
`
)
.join('');

res.redirect('/404');
res.setHeader('Content-Type', 'text/xml;charset=utf-8');
return res.send(`
<sitemapindex xmlns="http://sitemaps.org/schemas/sitemap/0.9">${SitemapLinks}</sitemapindex>
`);
}
};

export default sitemapApi;

0 comments on commit a90a376

Please sign in to comment.