From 915c1b0a3aee8b37df26e6648969a9bcec72f2cc Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Mon, 6 Jun 2022 20:41:37 -0500 Subject: [PATCH] Save sitemap as xml during build --- build-lists.js | 8 +++++--- helpers/api/sitemap/build.js | 25 +++++++++++++++++++++++++ helpers/constants.js | 4 ++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 helpers/api/sitemap/build.js diff --git a/build-lists.js b/build-lists.js index a090163..aa1a67a 100644 --- a/build-lists.js +++ b/build-lists.js @@ -13,6 +13,7 @@ import buildGamesList from '~/helpers/build-game-list.js' import buildHomebrewList from '~/helpers/build-homebrew-list.js' import buildVideoList from '~/helpers/build-video-list.js' import buildDeviceList from '~/helpers/build-device-list.js' +import { saveSitemap } from '~/helpers/api/sitemap/build.js' import { deviceSupportsApp } from '~/helpers/devices.js' import getListSummaryNumbers from '~/helpers/get-list-summary-numbers.js' @@ -697,13 +698,14 @@ class BuildLists { }) } ) - // Add paginated category lists to sitemap - // sitemapEndpoints.push( ...this.getKindListForCategories() ) - // Save sitemap endpoints console.log('Building Sitemap JSON') await this.saveToJson( sitemapEndpoints, './static/sitemap-endpoints.json') + // Save XML Sitemap + console.log('Building XML Sitemap') + await saveSitemap( sitemapEndpoints.map( ({ route }) => route ) ) + // Save stork toml index console.log('Building Stork toml index') await writeStorkToml( sitemapEndpoints ) diff --git a/helpers/api/sitemap/build.js b/helpers/api/sitemap/build.js new file mode 100644 index 0000000..578a71e --- /dev/null +++ b/helpers/api/sitemap/build.js @@ -0,0 +1,25 @@ +// import fs from 'fs-extra' +import { simpleSitemapAndIndex } from 'sitemap' + + +import { + sitemapLocation +} from '~/helpers/constants.js' + +export async function saveSitemap ( sitemapUrls ) { + + await simpleSitemapAndIndex({ + hostname: process.env.PUBLIC_URL, + destinationDir: sitemapLocation, + gzip: false, + // [{ url: '/page-1/', changefreq: 'daily'}, ...], + sourceData: sitemapUrls.map( url => { + return { + url, + // Google doesn't care about changefreq and priority - https://www.seroundtable.com/google-priority-change-frequency-xml-sitemap-20273.html + // changefreq: 'daily' + } + }), + }) + +} diff --git a/helpers/constants.js b/helpers/constants.js index 1bc0880..f6d7411 100644 --- a/helpers/constants.js +++ b/helpers/constants.js @@ -1,2 +1,6 @@ export const filterSeparator = '_' + +export const sitemapLocation = './static/' + +export const sitemapIndexFileName = 'sitemap-index.xml' \ No newline at end of file