Skip to content

Commit 32b8aa4

Browse files
authored
Merge pull request #58 from netlify-labs/bug/fix-error-handling
Fix error handling during XML parsing
2 parents d313fa9 + b250954 commit 32b8aa4

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

make_sitemap.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ const DEFAULT_PRIORITY = 0.8
7070
// 600 sec cache period
7171
const DEFAULT_CACHE_TIME = 600000
7272

73+
// Creates a sitemap object given the input configuration with URLs and generates XML
74+
const createSitemapInfo = async function ({ homepage, urls, failBuild }) {
75+
try {
76+
const hostname = ensureTrailingSlash(homepage)
77+
const sitemap = createSitemap({ hostname, cacheTime: DEFAULT_CACHE_TIME, urls })
78+
await util.promisify(sitemap.toXML.bind(sitemap))()
79+
const xml = sitemap.toString()
80+
return { sitemap, xml }
81+
} catch (error) {
82+
return failBuild('Could not generate XML sitemap', { error })
83+
}
84+
}
85+
7386
module.exports = async function makeSitemap(opts = {}) {
7487
const {
7588
distPath,
@@ -87,16 +100,8 @@ module.exports = async function makeSitemap(opts = {}) {
87100
const paths = await getPaths({ distPath, exclude, cwd })
88101
const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd })
89102

90-
// Creates a sitemap object given the input configuration with URLs
91-
const sitemap = createSitemap({ hostname: ensureTrailingSlash(homepage), cacheTime: DEFAULT_CACHE_TIME, urls })
92-
// Generates XML
93-
try {
94-
await util.promisify(sitemap.toXML.bind(sitemap))()
95-
} catch (error) {
96-
failBuild('Could not generate XML sitemap', { error })
97-
}
98-
// Gives you a string containing the XML data
99-
const xml = sitemap.toString()
103+
const { sitemap, xml } = await createSitemapInfo({ homepage, urls, failBuild })
104+
100105
// write sitemap to file
101106
const sitemapFileName = fileName || 'sitemap.xml'
102107
const sitemapFile = path.resolve(distPath, sitemapFileName)

0 commit comments

Comments
 (0)