@@ -5,60 +5,40 @@ import {
55 BASE_URL ,
66 EXTERNAL_LINKS_SITEMAP ,
77} from '#site/next.constants.mjs' ;
8+ import { BLOG_DYNAMIC_ROUTES } from '#site/next.dynamic.constants.mjs' ;
89import { dynamicRouter } from '#site/next.dynamic.mjs' ;
910import { availableLocaleCodes , defaultLocale } from '#site/next.locales.mjs' ;
1011
1112// This is the combination of the Application Base URL and Base PATH
1213const baseUrlAndPath = `${ BASE_URL } ${ BASE_PATH } ` ;
1314
15+ // All available alternate locales
16+ const nonDefaultLocales = availableLocaleCodes . filter (
17+ l => l !== defaultLocale . code
18+ ) ;
19+
20+ const getAlternatePath = ( r : string , locales : Array < string > ) =>
21+ Object . fromEntries ( locales . map ( l => [ l , `${ baseUrlAndPath } /${ l } /${ r } ` ] ) ) ;
22+
1423// This allows us to generate a `sitemap.xml` file dynamically based on the needs of the Node.js Website
1524const sitemap = async ( ) : Promise < MetadataRoute . Sitemap > => {
16- const routes = await dynamicRouter . getRoutesByLanguage ( defaultLocale . code ) ;
17- const paths = [ ] ;
25+ // Gets a list of all statically available routes
26+ const routes = await dynamicRouter . getAllRoutes ( ) ;
1827
1928 const currentDate = new Date ( ) . toISOString ( ) ;
2029
21- for ( const route of routes ) {
22- const availableLocalesForRoute = [ ] ;
23-
24- for ( const locale of availableLocaleCodes . filter (
25- locale => locale !== defaultLocale . code
26- ) ) {
27- const markdownFile = await dynamicRouter . getMarkdownFile ( locale , route ) ;
28- const isAvailable = markdownFile . filename !== '' ;
29- if ( isAvailable ) {
30- availableLocalesForRoute . push ( locale ) ;
31- }
32- }
33-
34- const alternatesPaths = availableLocalesForRoute . reduce (
35- ( acc , locale ) => ( {
36- ...acc ,
37- [ locale ] : `${ baseUrlAndPath } /${ locale } /${ route } ` ,
38- } ) ,
39- { }
40- ) ;
30+ const getSitemapEntry = ( r : string , locales : Array < string > = [ ] ) => ( {
31+ url : `${ baseUrlAndPath } /${ defaultLocale . code } /${ r } ` ,
32+ lastModified : currentDate ,
33+ changeFrequency : 'always' as const ,
34+ alternates : { languages : getAlternatePath ( r , locales ) } ,
35+ } ) ;
4136
42- paths . push ( {
43- url : `${ baseUrlAndPath } /${ defaultLocale . code } /${ route } ` ,
44- lastModified : currentDate ,
45- changeFrequency : 'always' as const ,
46- alternates : {
47- languages : {
48- ...alternatesPaths ,
49- } ,
50- } ,
51- } ) ;
52- }
37+ const staticPaths = routes . map ( r => getSitemapEntry ( r , nonDefaultLocales ) ) ;
38+ const blogPaths = BLOG_DYNAMIC_ROUTES . map ( r => getSitemapEntry ( r ) ) ;
39+ const externalPaths = EXTERNAL_LINKS_SITEMAP . map ( r => getSitemapEntry ( r ) ) ;
5340
54- return [
55- ...paths ,
56- ...EXTERNAL_LINKS_SITEMAP . map ( route => ( {
57- url : route ,
58- lastModified : currentDate ,
59- changeFrequency : 'always' as const ,
60- } ) ) ,
61- ] ;
41+ return [ ...staticPaths , ...blogPaths , ...externalPaths ] ;
6242} ;
6343
6444export default sitemap ;
0 commit comments