From f5cce648626a5ed811c11e21786ecbbb3eeb23fd Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 21 Dec 2024 00:18:49 +0800 Subject: [PATCH] :mag: Optimize for SEO of posts --- src/layouts/Layout.astro | 3 ++- src/pages/posts/[...slug].astro | 34 ++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 06961a7..6a5b34a 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -12,7 +12,8 @@ interface Props { } let { title, trailingTitle } = Astro.props -if(trailingTitle == null) trailingTitle = 'Solsynth LLC' +if (!trailingTitle) trailingTitle = 'Solsynth LLC' +console.log(title ? `${title} | ${trailingTitle}` : trailingTitle) const locale = Astro.currentLocale ?? 'en' --- diff --git a/src/pages/posts/[...slug].astro b/src/pages/posts/[...slug].astro index 2fff45a..8bcad9b 100644 --- a/src/pages/posts/[...slug].astro +++ b/src/pages/posts/[...slug].astro @@ -13,7 +13,7 @@ import { getAttachmentUrl, fetchAttachmentMeta } from '@/scripts/attachment' const { slug } = Astro.params const baseUrl = import.meta.env.PUBLIC_SOLAR_NETWORK_URL -const resp = await fetch(`${baseUrl}/cgi/co/posts/${slug}`) +const resp = await fetch(`${baseUrl}/cgi/co/posts/${slug?.replace('/', ':')}`) if (resp.status !== 200) { return new Response(null, { status: 404 }) @@ -27,16 +27,27 @@ const rawContent = await marked(data.body.content as string, { const content = sanitizeHtml(rawContent) const attachments = await fetchAttachmentMeta(data.body.attachments) + +const title = data.body?.title ? data.body.title : `Post #${data.id}` +const description = + data.body?.description ?? data.body?.content?.substring(0, 200) + '...' + +const url = + data.alias && data.alias_prefix + ? `https://solsynth.dev/posts/${data.alias_prefix}/${data.alias}` + : `https://solsynth.dev/posts/${data.id}` --- a.mimetype.startsWith('video'))?.rid ), @@ -45,14 +56,14 @@ const attachments = await fetchAttachmentMeta(data.body.attachments) ), }, basic: { - title: data.body?.title ? data.body.title : `Post #${data.id}`, + title, type: 'article', image: data.body?.thumbnail ? getAttachmentUrl(data.body.thumbnail) : getAttachmentUrl( attachments.find((a) => a.mimetype.startsWith('image'))?.rid ), - url: `https://solsynth.dev/posts/${data.id}`, + url, }, article: { publishedTime: new Date(data.created_at).toISOString(), @@ -62,8 +73,8 @@ const attachments = await fetchAttachmentMeta(data.body.attachments) }} twitter={{ card: 'summary_large_image', - title: data.body?.title ? data.body.title : `Post #${data.id}`, - description: data.body?.description ?? data.body?.content?.substring(0, 200), + title, + description, creator: '@' + data.publisher.name, image: data.body?.thumbnail ? getAttachmentUrl(data.body.thumbnail) @@ -74,10 +85,7 @@ const attachments = await fetchAttachmentMeta(data.body.attachments) /> - +