Skip to content

Commit cdc6859

Browse files
authored
fix(sitemap): fix sitemap for in site image links (#161)
2 parents b6fb1a3 + c8eef66 commit cdc6859

File tree

6 files changed

+73
-38
lines changed

6 files changed

+73
-38
lines changed

CHANGELOG.md

+6
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

+1-1
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

+42-35
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

src/components/article/TOC.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
import { useTOCLogic } from '@/hooks'
4-
import { useIsTop } from '@zl-asica/react'
4+
import { useIsBottom, useIsTop } from '@zl-asica/react'
55
import { List } from 'lucide-react'
66

77
import TOCLink from './TOCLink'
@@ -20,7 +20,13 @@ const TOC = ({
2020
showThumbnail = true,
2121
}: TOCProps) => {
2222
const { activeSlug, isOpen, toggleOpen, handleLinkClick, tocReference } = useTOCLogic()
23-
const isVisible = !useIsTop(showThumbnail ? 150 : 50)
23+
const isTop = !useIsTop(showThumbnail ? 150 : 50)
24+
const isBottom = !useIsBottom(50)
25+
const isVisible = isTop && isBottom
26+
27+
if (items.length === 0) {
28+
return null
29+
}
2430

2531
return (
2632
<div
+15
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

+1
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)