Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

556667: fixed sitemap results and fixed sitemap service #1254

Merged
merged 1 commit into from Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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