@@ -30,6 +30,7 @@ import {
30
30
} from '../core/types.js' ;
31
31
import type { ElementGraph } from '../node/element-graph.js' ;
32
32
import { parseTagNames } from '../utils/elements.js' ;
33
+ import { toHrefLang } from '../utils/i18n.js' ;
33
34
import { AssetMap } from './asset-map/asset-map.js' ;
34
35
import { htmlMinify } from './html-minify.js' ;
35
36
import { htmlPretty } from './html-pretty.js' ;
@@ -386,29 +387,38 @@ export class Renderer {
386
387
387
388
async getSitemap ( ) : Promise < Sitemap > {
388
389
const sitemap : Sitemap = { } ;
389
- const sitemapItemAlts : Record < string , Record < string , string > > = { } ;
390
+ const sitemapItemAlts : Record <
391
+ string ,
392
+ Record < string , { hrefLang : string ; urlPath : string } >
393
+ > = { } ;
390
394
const trailingSlash = this . rootConfig . server ?. trailingSlash || false ;
391
395
392
396
await this . router . walk ( async ( urlPath : string , route : Route ) => {
393
397
const routePaths = await this . router . getAllPathsForRoute ( urlPath , route ) ;
394
398
routePaths . forEach ( ( routePath ) => {
395
399
const routeLocale = route . isDefaultLocale ? 'x-default' : route . locale ;
400
+ const hrefLang = route . isDefaultLocale
401
+ ? 'x-default'
402
+ : toHrefLang ( route . locale ) ;
396
403
const defaultUrlPath = normalizeUrlPath (
397
404
replaceParams ( route . routePath , routePath . params ) ,
398
405
{ trailingSlash : trailingSlash }
399
406
) ;
400
407
if ( ! sitemapItemAlts [ defaultUrlPath ] ) {
401
408
sitemapItemAlts [ defaultUrlPath ] = { } ;
402
409
}
403
- sitemapItemAlts [ defaultUrlPath ] [ routeLocale ] = normalizeUrlPath (
404
- replaceParams ( urlPath , routePath . params ) ,
405
- { trailingSlash : trailingSlash }
406
- ) ;
410
+ sitemapItemAlts [ defaultUrlPath ] [ routeLocale ] = {
411
+ hrefLang : hrefLang ,
412
+ urlPath : normalizeUrlPath ( replaceParams ( urlPath , routePath . params ) , {
413
+ trailingSlash : trailingSlash ,
414
+ } ) ,
415
+ } ;
407
416
const sitemapItem : SitemapItem = {
408
417
urlPath : routePath . urlPath ,
409
418
route,
410
419
params : routePath . params ,
411
420
locale : routeLocale ,
421
+ hrefLang : hrefLang ,
412
422
alts : sitemapItemAlts [ defaultUrlPath ] ,
413
423
} ;
414
424
sitemap [ routePath . urlPath ] = sitemapItem ;
@@ -423,7 +433,8 @@ export class Renderer {
423
433
. forEach ( ( urlPath : string ) => {
424
434
// console.log(urlPath);
425
435
const sitemapItem = sitemap [ urlPath ] ;
426
- const orderedAlts : Record < string , string > = { } ;
436
+ const orderedAlts : Record < string , { hrefLang : string ; urlPath : string } > =
437
+ { } ;
427
438
Object . keys ( sitemapItem . alts )
428
439
. sort ( sortLocales )
429
440
. forEach ( ( locale ) => {
0 commit comments