@@ -2,52 +2,59 @@ import type { MetadataRoute } from 'next'
22
33import { getConfig } from '@/services/config'
44import { getAllPosts } from '@/services/content'
5+ import { generateImageUrl } from '@/services/utils'
56
67async function sitemap ( ) : Promise < MetadataRoute . Sitemap > {
78 const config = getConfig ( )
89 const siteUrl = config . siteUrl
910 const updateDate = new Date ( )
1011
11- // Load posts data from JSON file
12+ // Load posts data
1213 const posts = await getAllPosts ( )
1314
14- // Generate sitemap entries for each post
15- const postUrls = posts . map ( post => ( {
16- url : `${ siteUrl } /${ post . slug } ` ,
17- lastModified : post . lastModified || updateDate ,
18- changeFrequency : 'weekly' as const ,
19- priority : 0.5 ,
20- images : post . frontmatter . showThumbnail && post . frontmatter . thumbnail !== undefined
21- ? [ post . frontmatter . thumbnail ]
22- : [ ] ,
23- } ) )
24-
25- // Pages settings
26- const showAnime = config . anilist_username === undefined || config . anilist_username !== null || config . anilist_username !== ''
27-
28- const pages = [ `${ siteUrl } /posts` , `${ siteUrl } /about` , `${ siteUrl } /friends` ]
29-
30- if ( showAnime ) {
31- pages . push ( `${ siteUrl } /about/anime` )
32- }
33-
34- const pagesSitemap = pages . map ( page => ( {
35- url : page ,
15+ const makeSitemapItem = (
16+ url : string ,
17+ options ?: Partial < MetadataRoute . Sitemap [ number ] > ,
18+ ) : MetadataRoute . Sitemap [ number ] => ( {
19+ url,
3620 lastModified : updateDate ,
37- changeFrequency : 'monthly' as const ,
21+ changeFrequency : 'monthly' ,
22+ priority : 0.8 ,
23+ ...options ,
24+ } )
25+
26+ // Generate sitemap entries for each post
27+ const postUrls = posts . map ( post =>
28+ makeSitemapItem ( `${ siteUrl } /${ post . slug } ` , {
29+ changeFrequency : 'weekly' ,
30+ priority : 0.5 ,
31+ images : post . frontmatter . showThumbnail
32+ ? generateImageUrl ( siteUrl , post . frontmatter . thumbnail )
33+ : undefined ,
34+ } ) ,
35+ )
36+ const homepage = makeSitemapItem ( siteUrl , {
37+ changeFrequency : 'yearly' ,
38+ priority : 1 ,
39+ } )
40+
41+ const aboutPage = makeSitemapItem ( `${ siteUrl } /about` , {
3842 priority : 0.8 ,
39- } ) )
40-
41- return [
42- {
43- url : siteUrl ,
44- lastModified : updateDate ,
45- changeFrequency : 'yearly' ,
46- priority : 1 ,
47- } ,
48- ...pagesSitemap , // Static page
49- ...postUrls , // Dynamic post URLs
43+ images : generateImageUrl ( siteUrl , config . avatar ) ,
44+ } )
45+
46+ const showAnime = Boolean ( config . anilist_username ?. trim ( ) )
47+ const staticPages = [
48+ { path : '/posts' , priority : 0.6 } ,
49+ { path : '/friends' , priority : 0.4 } ,
50+ ...( showAnime ? [ { path : '/about/anime' , priority : 0.5 } ] : [ ] ) ,
5051 ]
52+
53+ const staticSitemap = staticPages . map ( ( { path, priority } ) =>
54+ makeSitemapItem ( `${ siteUrl } ${ path } ` , { priority } ) ,
55+ )
56+
57+ return [ homepage , aboutPage , ...staticSitemap , ...postUrls ]
5158}
5259
5360export default sitemap
0 commit comments