Hi,
I am using gatsby-plugin-advanced-sitemap to generate an advanced sitemap.
What I would like to achieve is implement logic for generating the sitemaps using the data fetched from Graphql queries and passed to a function that in the end should result with this:
{
**pages**: [{url: '/test/', lastmod: 'YYYY-MM-DD', ...not how to pass all required fields to satisfy the expected result bellow}],
partnerships: [{url: /partnerhips/partner, lastmod: 'YYYY-MM-DD}]
}
So this will generate categorised sitemaps
In the end I would like to have these fields for each sitemap entity
<url>
<loc>https://${host}${route}</loc>
<lastmod>${date}</lastmod>
<link
rel="alternate"
hreflang="mk"
href="https://${host}${route}"
/>
<link
rel="alternate"
hreflang="en"
href="https://${host}/en${route}"
/>
</url>
<url>
<loc>https://${host}/en${route}</loc>
<lastmod>${date}</lastmod>
<link
rel="alternate"
hreflang="mk"
href="https://${host}${route}"
/>
<link
rel="alternate"
hreflang="en"
href="https://${host}/en${route}"
/>
</url>
This is how the plugin is set in gatsby-config.js:
const { GetSiteMap } = require("./src/lib/services/sitemap.service");
{
resolve: "gatsby-plugin-advanced-sitemap",
options: {
query: `{
allSanityPages {
edges {
node {
_updatedAt
slugs {
slug_ch {
current
}
slug_en {
current
}
slug_gr {
current
}
slug_se {
current
}
}
}
}
}
allSanityDomains {
edges {
node {
_updatedAt
}
}
}
allSanityPartnerships {
edges {
node {
_updatedAt
slugs {
slug_ch {
current
}
slug_en {
current
}
slug_gr {
current
}
slug_se {
current
}
}
localeAvailability {
ch
gr
se
}
}
}
}
}`,
serializer: (data) => GetSiteMap(data, process.env.GATSBY_LANG),
},
},
and GetSiteMap function (initial version just to try) :
function GetSiteMap(data, lang) {
console.log(Object.keys(data));
let sitemap = {};
const domains = MapEdgesToNodes(data.allSanityDomains);
const pages = MapEdgesToNodes(data.allSanityPages);
const partnerships = MapEdgesToNodes(data.allSanityPartnerships);
const partnersPage = GetCurrentPage("partners", pages);
if (
partnerships &&
partnerships.length > 0 &&
partnersPage &&
partnersPage.slugs &&
partnersPage.slugs[`slug_${lang}`] &&
partnersPage.slugs[`slug_${lang}`].current
) {
console.log("CREATING SITEMAP");
sitemap["partnerships"] = partnerships.map((p) => {
return {
node: {
url:
partnersPage.slugs[`slug_${lang}`].current +
"/" +
p.slugs[`slug_${lang}`].current +
"/",
},
};
});
}
return sitemap;
}
Anyways I always get:
Cannot read property 'replace' of undefined
233 |
234 | var sourceType = node.__typename ? "all" + node.__typename : source;
> 235 | var slug = sourceType === "allMarkdownRemark" || sourceType === "allMdx" || node !== null && node !== void 0 && (_node$fields = node.fields) !== null && _node$fields !== void 0 &&
_node$fields.slug ? node.fields.slug.replace(/^\/|\/$/, "") : node.slug.replace(/^\/|\/$/, "");
It is really important to achieve categorised sitemap and alternative links (for other locales) for each entity