Skip to content

Commit

Permalink
Merge pull request #1254 from Sitecore/bugfix/556667-sitemap-fix
Browse files Browse the repository at this point in the history
556667: fixed sitemap results and fixed sitemap service
(cherry picked from commit e82c43f)
  • Loading branch information
ambrauer committed Dec 6, 2022
1 parent 0c0f400 commit 9d84ec1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Expand Up @@ -19,28 +19,24 @@ const sitemapApi = async (
siteName: config.jssAppName,
});

// if url has sitemap-{n}.xml type
if (id) {
const sitemapPath = await sitemapXmlService.getSitemap(id as string);
// if url has sitemap-{n}.xml type. The id - can be null if it's sitemap.xml request
const sitemapPath = await sitemapXmlService.getSitemap(id as string);

// if sitemap is match otherwise redirect to 404 page
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 sitemap is match otherwise redirect to 404 page
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');

// need to prepare stream from sitemap url
return new AxiosDataFetcher()
.get(sitemapUrl, {
responseType: 'stream',
})
.then((response: AxiosResponse) => {
response.data.pipe(res);
})
.catch(() => res.redirect('/404'));
}

return res.redirect('/404');
// need to prepare stream from sitemap url
return new AxiosDataFetcher()
.get(sitemapUrl, {
responseType: 'stream',
})
.then((response: AxiosResponse) => {
response.data.pipe(res);
})
.catch(() => res.redirect('/404'));
}

// this approache if user go to /sitemap.xml - under it generate xml page with list of sitemaps
Expand All @@ -63,7 +59,7 @@ const sitemapApi = async (
res.setHeader('Content-Type', 'text/xml;charset=utf-8');

return res.send(`
<sitemapindex xmlns="http://sitemaps.org/schemas/sitemap/0.9">${SitemapLinks}</sitemapindex>
<sitemapindex xmlns="http://sitemaps.org/schemas/sitemap/0.9" encoding="UTF-8">${SitemapLinks}</sitemapindex>
`);
};

Expand Down
2 changes: 0 additions & 2 deletions packages/sitecore-jss/src/site/graphql-sitemap-service.ts
Expand Up @@ -83,8 +83,6 @@ export class GraphQLSitemapXmlService {
async getSitemap(id: string): Promise<string | undefined> {
const searchSitemap = `${PREFIX_NAME_SITEMAP}${id}.xml`;
const sitemaps = await this.fetchSitemaps();
// added this item - there is default sitemap when will be available everytime
sitemaps.push('sitemap.xml');

return sitemaps.find((sitemap: string) => sitemap.includes(searchSitemap));
}
Expand Down

0 comments on commit 9d84ec1

Please sign in to comment.