Refactor SEO component for better URL and robots handling

Refactor SEO component to improve canonical URL and social image handling. Introduced robots meta tag logic based on indexability.
This commit is contained in:
Cris Charles authored and GitHub committed 2026-09-24 05:12:39 +08:00
1 parent ec3768e6a9
commit 5b7742b132
1 file changed
+24 -3
+24 -3
View File
@@ -16,8 +16,29 @@ const {
publishedTime, publishedTime,
} = Astro.props; } = Astro.props;
const canonicalURL = Astro.props.canonicalURL || new URL(Astro.url.pathname, Astro.site || Astro.url.origin || 'https://crischarles.com').href; const siteOrigin = Astro.site ? new URL(Astro.site).origin : 'https://crischarles.com';
const socialImageURL = new URL(image, Astro.site || Astro.url.origin || 'https://crischarles.com').href; const normalizedPath = Astro.url.pathname === '/' ? '/' : Astro.url.pathname.replace(/\/+$/, '');
const canonicalURL =
Astro.props.canonicalURL ||
new URL(normalizedPath, siteOrigin).href;
const imagePath = image.startsWith('http')
? image
: image.startsWith('/')
? image
: `/${image}`;
const socialImageURL = new URL(imagePath, siteOrigin).href;
const isIndexablePage =
!Astro.url.pathname.startsWith('/api/') &&
!Astro.url.pathname.startsWith('/maintenance') &&
!Astro.url.pathname.startsWith('/_astro') &&
!Astro.url.pathname.startsWith('/_image');
const robotsValue = isIndexablePage
? 'index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1'
: 'noindex, nofollow';
const socialProfiles = [ const socialProfiles = [
'https://github.com/ssbg04', 'https://github.com/ssbg04',
@@ -86,7 +107,7 @@ const schemaData = {
<meta name="title" content={title} /> <meta name="title" content={title} />
<meta name="description" content={description} /> <meta name="description" content={description} />
<meta name="author" content="Cris Charles Garcia" /> <meta name="author" content="Cris Charles Garcia" />
<meta name="robots" content="index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1" /> <meta name="robots" content={robotsValue} />
<link rel="canonical" href={canonicalURL} /> <link rel="canonical" href={canonicalURL} />
<!-- Open Graph / Facebook --> <!-- Open Graph / Facebook -->