Pārlūkot izejas kodu

fix: restore article detail ssr context

main
leiyun pirms 1 mēnesi
vecāks
revīzija
f1816c090a
1 mainītis faili ar 32 papildinājumiem un 30 dzēšanām
  1. +32
    -30
      app/composables/useArticle.js

+ 32
- 30
app/composables/useArticle.js Parādīt failu

@@ -116,41 +116,43 @@ export async function useArticleDetail(colId) {
const detail = computed(() => data.value?.detail || {})
const canonical = computed(() => `https://tool.aionline.cc${articleUrl(detail.value)}`)

useSeoMeta({
title: () => legacyPageTitle(detail.value.title),
description: () => detail.value.info || LEGACY_DEFAULT_DESCRIPTION,
keywords: () => detail.value.title || LEGACY_DEFAULT_KEYWORDS,
ogTitle: () => detail.value.title,
ogDescription: () => detail.value.info || detail.value.title,
ogType: 'article',
ogImage: () => detail.value.cover || '',
articlePublishedTime: () => formatTime(detail.value.publish_time, 'YYYY-MM-DDTHH:mm:ssZ'),
})

useHead({
link: [{ rel: 'canonical', href: canonical }],
// 结构化数据,利于搜索结果富摘要
script: [{
type: 'application/ld+json',
innerHTML: computed(() => JSON.stringify({
'@context': 'https://schema.org',
'@type': 'Article',
headline: detail.value.title,
description: detail.value.info || '',
image: detail.value.cover ? [detail.value.cover] : undefined,
datePublished: formatTime(detail.value.publish_time, 'YYYY-MM-DD'),
author: { '@type': 'Organization', name: 'AI在线' },
publisher: { '@type': 'Organization', name: 'AI在线' },
mainEntityOfPage: { '@type': 'WebPage', '@id': canonical.value },
})),
}],
})

// 文章不存在 → 404,避免无效页面被搜索引擎收录
if (error.value || !data.value?.detail?.id) {
throw createError({ statusCode: 404, statusMessage: '文章不存在', fatal: true })
}

await nuxtApp.runWithContext(() => {
useSeoMeta({
title: () => legacyPageTitle(detail.value.title),
description: () => detail.value.info || LEGACY_DEFAULT_DESCRIPTION,
keywords: () => detail.value.title || LEGACY_DEFAULT_KEYWORDS,
ogTitle: () => detail.value.title,
ogDescription: () => detail.value.info || detail.value.title,
ogType: 'article',
ogImage: () => detail.value.cover || '',
articlePublishedTime: () => formatTime(detail.value.publish_time, 'YYYY-MM-DDTHH:mm:ssZ'),
})

useHead({
link: [{ rel: 'canonical', href: canonical }],
// 结构化数据,利于搜索结果富摘要
script: [{
type: 'application/ld+json',
innerHTML: computed(() => JSON.stringify({
'@context': 'https://schema.org',
'@type': 'Article',
headline: detail.value.title,
description: detail.value.info || '',
image: detail.value.cover ? [detail.value.cover] : undefined,
datePublished: formatTime(detail.value.publish_time, 'YYYY-MM-DD'),
author: { '@type': 'Organization', name: 'AI在线' },
publisher: { '@type': 'Organization', name: 'AI在线' },
mainEntityOfPage: { '@type': 'WebPage', '@id': canonical.value },
})),
}],
})
})

// 访问的栏目路径与文章实际栏目不一致 → 301 到规范地址(避免重复内容)
if (colId && Number(detail.value.col_id) !== Number(colId)) {
await nuxtApp.runWithContext(() =>


Notiek ielāde…
Atcelt
Saglabāt