Skip to content

Commit c8eef66

Browse files
committed
fix(sitemap): fix image link inner links
1 parent 495f81f commit c8eef66

File tree

5 files changed

+65
-36
lines changed

5 files changed

+65
-36
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# SuzuBlog Changelog
22

3+
## 1.8.2 (2025-04-21)
4+
5+
### Patch Changes
6+
7+
- Fix sitemap for in site image links
8+
39
## 1.8.1 (2025-04-20)
410

511
### Patch Changes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "suzu-blog",
3-
"version": "1.8.1",
3+
"version": "1.8.2",
44
"private": true,
55
"packageManager": "[email protected]",
66
"author": {

src/app/sitemap.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,59 @@ import type { MetadataRoute } from 'next'
22

33
import { getConfig } from '@/services/config'
44
import { getAllPosts } from '@/services/content'
5+
import { generateImageUrl } from '@/services/utils'
56

67
async 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

5360
export default sitemap
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const generateImageUrl = (
2+
siteUrl: string,
3+
image?: string,
4+
): string[] | undefined => {
5+
if (image === undefined || image === null || image.trim() === '') {
6+
return undefined
7+
}
8+
if (image.startsWith('http')) {
9+
return [image]
10+
}
11+
const normalizedPath = image.startsWith('/') ? image : `/images/${image}`
12+
return [`${siteUrl}${normalizedPath}`]
13+
}
14+
15+
export default generateImageUrl

src/services/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export {
22
default as generateHierarchicalSlug,
33
slugPrefix,
44
} from './generateHierarchicalSlug'
5+
export { default as generateImageUrl } from './generateImageUrl'
56
export { sanitizeQuery, validateParameters } from './searchUtils'

0 commit comments

Comments
 (0)