🔍 Optimize for SEO of posts

This commit is contained in:
LittleSheep 2024-12-21 00:18:49 +08:00
parent 8317ee43fd
commit f5cce64862
2 changed files with 23 additions and 14 deletions

View File

@ -12,7 +12,8 @@ interface Props {
} }
let { title, trailingTitle } = Astro.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' const locale = Astro.currentLocale ?? 'en'
--- ---

View File

@ -13,7 +13,7 @@ import { getAttachmentUrl, fetchAttachmentMeta } from '@/scripts/attachment'
const { slug } = Astro.params const { slug } = Astro.params
const baseUrl = import.meta.env.PUBLIC_SOLAR_NETWORK_URL 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) { if (resp.status !== 200) {
return new Response(null, { status: 404 }) return new Response(null, { status: 404 })
@ -27,16 +27,27 @@ const rawContent = await marked(data.body.content as string, {
const content = sanitizeHtml(rawContent) const content = sanitizeHtml(rawContent)
const attachments = await fetchAttachmentMeta(data.body.attachments) 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}`
--- ---
<head> <head>
<SEO <SEO
title={data.body?.title ? data.body.title : `Post #${data.id}`} title={`${title} | Solar Network`}
description={data.body?.description ?? data.body?.content?.substring(0, 200)} description={description}
canonical={url}
charset='UTF-8'
openGraph={{ openGraph={{
optional: { optional: {
siteName: "Solar Network", siteName: 'Solar Network',
description: data.body?.description ?? data.body?.content?.substring(0, 200), description,
video: getAttachmentUrl( video: getAttachmentUrl(
attachments.find((a) => a.mimetype.startsWith('video'))?.rid attachments.find((a) => a.mimetype.startsWith('video'))?.rid
), ),
@ -45,14 +56,14 @@ const attachments = await fetchAttachmentMeta(data.body.attachments)
), ),
}, },
basic: { basic: {
title: data.body?.title ? data.body.title : `Post #${data.id}`, title,
type: 'article', type: 'article',
image: data.body?.thumbnail image: data.body?.thumbnail
? getAttachmentUrl(data.body.thumbnail) ? getAttachmentUrl(data.body.thumbnail)
: getAttachmentUrl( : getAttachmentUrl(
attachments.find((a) => a.mimetype.startsWith('image'))?.rid attachments.find((a) => a.mimetype.startsWith('image'))?.rid
), ),
url: `https://solsynth.dev/posts/${data.id}`, url,
}, },
article: { article: {
publishedTime: new Date(data.created_at).toISOString(), publishedTime: new Date(data.created_at).toISOString(),
@ -62,8 +73,8 @@ const attachments = await fetchAttachmentMeta(data.body.attachments)
}} }}
twitter={{ twitter={{
card: 'summary_large_image', card: 'summary_large_image',
title: data.body?.title ? data.body.title : `Post #${data.id}`, title,
description: data.body?.description ?? data.body?.content?.substring(0, 200), description,
creator: '@' + data.publisher.name, creator: '@' + data.publisher.name,
image: data.body?.thumbnail image: data.body?.thumbnail
? getAttachmentUrl(data.body.thumbnail) ? getAttachmentUrl(data.body.thumbnail)
@ -74,10 +85,7 @@ const attachments = await fetchAttachmentMeta(data.body.attachments)
/> />
</head> </head>
<Layout <Layout title={title} trailingTitle="Solar Network">
title={data.body?.title ? data.body.title : `Post #${data.id}`}
trailingTitle="Solar Network"
>
<div role="alert" class="alert shadow-lg px-12 m-0 rounded-none mb-5"> <div role="alert" class="alert shadow-lg px-12 m-0 rounded-none mb-5">
<Icon <Icon
name="material-symbols:ungroup" name="material-symbols:ungroup"