From f1816c090a31944bbefa1622d7fff86692f75521 Mon Sep 17 00:00:00 2001 From: leiyun Date: Tue, 4 Aug 2026 23:59:51 +0800 Subject: [PATCH] fix: restore article detail ssr context --- app/composables/useArticle.js | 62 ++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/app/composables/useArticle.js b/app/composables/useArticle.js index 526c06f..b38ab3b 100644 --- a/app/composables/useArticle.js +++ b/app/composables/useArticle.js @@ -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(() =>