@@ -2,52 +2,59 @@ import type { MetadataRoute } from 'next'
2
2
3
3
import { getConfig } from '@/services/config'
4
4
import { getAllPosts } from '@/services/content'
5
+ import { generateImageUrl } from '@/services/utils'
5
6
6
7
async function sitemap ( ) : Promise < MetadataRoute . Sitemap > {
7
8
const config = getConfig ( )
8
9
const siteUrl = config . siteUrl
9
10
const updateDate = new Date ( )
10
11
11
- // Load posts data from JSON file
12
+ // Load posts data
12
13
const posts = await getAllPosts ( )
13
14
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,
36
20
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` , {
38
42
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 } ] : [ ] ) ,
50
51
]
52
+
53
+ const staticSitemap = staticPages . map ( ( { path, priority } ) =>
54
+ makeSitemapItem ( `${ siteUrl } ${ path } ` , { priority } ) ,
55
+ )
56
+
57
+ return [ homepage , aboutPage , ...staticSitemap , ...postUrls ]
51
58
}
52
59
53
60
export default sitemap
0 commit comments