@@ -70,6 +70,19 @@ const DEFAULT_PRIORITY = 0.8
7070// 600 sec cache period
7171const 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+
7386module . 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