feat: 修改为服务端渲染、解决跳转问题

This commit is contained in:
Tim
2025-08-08 13:22:42 +08:00
parent 6554e66a4e
commit 65ae660486
4 changed files with 36 additions and 28 deletions

View File

@@ -76,7 +76,9 @@ export default {
const router = useRouter()
const goToHome = () => {
router.push('/')
router.push('/').then(() => {
window.location.reload()
})
}
const search = () => {
showSearch.value = true

View File

@@ -245,13 +245,17 @@ export default {
gotoCategory(c) {
const value = encodeURIComponent(c.id ?? c.name)
this.$router
.push({ path: '/', query: { category: value } })
.push({ path: '/', query: { category: value } }).then(() => {
window.location.reload()
})
this.handleItemClick()
},
gotoTag(t) {
const value = encodeURIComponent(t.id ?? t.name)
this.$router
.push({ path: '/', query: { tags: value } })
.push({ path: '/', query: { tags: value } }).then(() => {
window.location.reload()
})
this.handleItemClick()
}
}

View File

@@ -133,10 +133,6 @@ export default {
},
async setup() {
const route = useRoute()
/**
* -------- 1. PARAMS & REFS --------
*/
const selectedCategory = ref('')
if (route.query.category) {
const c = decodeURIComponent(route.query.category)
@@ -169,16 +165,6 @@ export default {
const pageSize = 10
const allLoaded = ref(false)
/**
* -------- 2. INIT FETCH FOR SSR --------
* 服务端渲染阶段也需要获取首页内容和选项。
*/
await loadOptions()
await fetchContent()
/**
* -------- 3. FETCH OPTION HELPERS --------
*/
const loadOptions = async () => {
if (selectedCategory.value && !isNaN(selectedCategory.value)) {
try {
@@ -242,9 +228,6 @@ export default {
return url
}
/**
* -------- 4. FETCH CORE --------
*/
const fetchPosts = async (reset = false) => {
if (reset) {
page.value = 0
@@ -296,7 +279,12 @@ export default {
if (isLoadingPosts.value || allLoaded.value) return
try {
isLoadingPosts.value = true
const res = await fetch(buildRankUrl())
const token = getToken()
const res = await fetch(buildRankUrl(), {
headers: {
Authorization: token ? `Bearer ${token}` : ''
}
})
isLoadingPosts.value = false
if (!res.ok) return
const data = await res.json()
@@ -333,7 +321,12 @@ export default {
if (isLoadingPosts.value || allLoaded.value) return
try {
isLoadingPosts.value = true
const res = await fetch(buildReplyUrl())
const token = getToken()
const res = await fetch(buildReplyUrl(), {
headers: {
Authorization: token ? `Bearer ${token}` : ''
}
})
isLoadingPosts.value = false
if (!res.ok) return
const data = await res.json()
@@ -363,11 +356,11 @@ export default {
const fetchContent = async (reset = false) => {
if (selectedTopic.value === '排行榜') {
fetchRanking(reset)
await fetchRanking(reset)
} else if (selectedTopic.value === '最新回复') {
fetchLatestReply(reset)
await fetchLatestReply(reset)
} else {
fetchPosts(reset)
await fetchPosts(reset)
}
}
@@ -383,6 +376,9 @@ export default {
const sanitizeDescription = text => stripMarkdown(text)
await loadOptions()
await fetchContent()
return {
topics,
selectedTopic,

View File

@@ -86,9 +86,15 @@ export function handleMarkdownClick(e) {
export function stripMarkdown(text) {
const html = md.render(text || '')
const el = document.createElement('div')
el.innerHTML = html
return el.textContent || el.innerText || ''
// SSR 环境下没有 document
if (typeof window === 'undefined') {
// 用正则去除 HTML 标签
return html.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim()
} else {
const el = document.createElement('div')
el.innerHTML = html
return el.textContent || el.innerText || ''
}
}
export function stripMarkdownLength(text, length) {