Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SiteMapManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class SiteMapManager {
this.index = options.index || this.createIndexGenerator(sitemapTypes);
// create the default pages one for all fallback sitemap URLs
this.pages = options.pages || this.createSiteMapGenerator(options, `pages`);
this.summary = options.summary || this.createSiteMapGenerator(options, `summary`);
}

createIndexGenerator(sitemapTypes) {
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class SiteMapManager {
// This is the equivalent of adding the URLs on bootstrap by listening to the events
// like we do in Ghost core
addUrls(type, {url, node}) {
this['summary'].addUrl(url, node);
return this[type].addUrl(url, node);
}
}

7 changes: 5 additions & 2 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ const defaultOptions = {
sitemap: `pages`
}
},
output: `/sitemap.xml`,
output: `/sitemap-index.xml`,
summaryOutput: `/sitemap.xml`,
exclude: [
`/dev-404-page`,
`/404`,
`/404.html`,
`/offline-plugin-app-shell-fallback`
],
createLinkInHead: true
createLinkInHead: true,
slugsMapping: {},
additionPages: []
};

export default defaultOptions;
26 changes: 20 additions & 6 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const runQuery = (handler, {query, mapping, exclude}) => handler(query).then((r)
for (let source in r.data) {
// Check for custom serializer
if (typeof mapping?.[source]?.serializer === `function`) {
if (r.data[source] && Array.isArray(r.data[source].edges)) {
if (r.data[source] && Array.isArray(r.data[source].edges)) {
const serializedEdges = mapping[source].serializer(r.data[source].edges);

if (!Array.isArray(serializedEdges)) {
Expand All @@ -175,10 +175,10 @@ const runQuery = (handler, {query, mapping, exclude}) => handler(query).then((r)

// Removing excluded paths
if (r.data?.[source]?.edges && r.data[source].edges.length) {
r.data[source].edges = r.data[source].edges.filter(({node}) => !exclude.some((excludedRoute) => {
r.data[source].edges = r.data[source].edges.filter(({node}) => !exclude.some((excludedRoute) => {
const sourceType = node.__typename ? `all${node.__typename}` : source;
const slug = (sourceType === `allMarkdownRemark` || sourceType === `allMdx`) || (node?.fields?.slug) ? node.fields.slug.replace(/^\/|\/$/, ``) : node.slug.replace(/^\/|\/$/, ``);

excludedRoute = typeof excludedRoute === `object` ? excludedRoute : excludedRoute.replace(/^\/|\/$/, ``);

// test if the passed regular expression is valid
Expand All @@ -205,7 +205,7 @@ const runQuery = (handler, {query, mapping, exclude}) => handler(query).then((r)
return r.data;
});

const serialize = ({...sources} = {}, {site, allSitePage}, {mapping, addUncaughtPages}) => {
const serialize = ({...sources} = {}, {site, allSitePage}, {mapping, addUncaughtPages, slugsMapping}) => {
const nodes = [];
const sourceObject = {};

Expand All @@ -230,9 +230,9 @@ const serialize = ({...sources} = {}, {site, allSitePage}, {mapping, addUncaught
// to reflect this. This prevents mapping issues, when we later update
// the path with the Gatsby generated one in `getNodePath`
if (mapping[type].path) {
node.path = path.resolve(mapping[type].path, node.slug);
node.path = path.resolve(mapping[type].path, node.slug) + `/`
} else {
node.path = node.slug;
node.path = node.slug in slugsMapping ? `/${slugsMapping[node.slug]}/` : `/${node.slug}/`;
}

if (typeof mapping[type].prefix === `string` && mapping[type].prefix !== ``){
Expand Down Expand Up @@ -277,6 +277,7 @@ exports.onPostBuild = async ({graphql, pathPrefix}, pluginOptions) => {
const options = pluginOptions.addUncaughtPages ? _.merge(defaultOptions, pluginOptions) : Object.assign({}, defaultOptions, pluginOptions);

const indexSitemapFile = path.join(PUBLICPATH, pathPrefix, options.output);
const summarySitemapFile = path.join(PUBLICPATH, pathPrefix, options.summaryOutput);
const resourcesSitemapFile = path.join(PUBLICPATH, pathPrefix, RESOURCESFILE);

delete options.plugins;
Expand Down Expand Up @@ -312,6 +313,10 @@ exports.onPostBuild = async ({graphql, pathPrefix}, pluginOptions) => {
}
});

for (const additionPage of options.additionPages) {
manager.addUrls(`pages`, { url: siteURL + additionPage.path , node: { slug: additionPage.slug, id: additionPage.id } });
}

// The siteUrl is only available after we have the returned query results
options.siteUrl = siteURL;
options.pathPrefix = pathPrefix;
Expand Down Expand Up @@ -345,6 +350,15 @@ exports.onPostBuild = async ({graphql, pathPrefix}, pluginOptions) => {
console.error(err);
}

const summarySiteMap = manager.getSiteMapXml('summary', options);

// Save the generated xml files in the public folder
try {
await utils.outputFile(summarySitemapFile, summarySiteMap);
} catch (err) {
console.error(err);
}

for (let sitemap of resourcesSiteMapsArray) {
const filePath = resourcesSitemapFile.replace(/:resource/, sitemap.type);

Expand Down