From 5b7742b132200a543a5238e977b739f9aee0a49b Mon Sep 17 00:00:00 2001 From: Cris Charles <143066036+ssbg04@users.noreply.github.com> Date: Thu, 24 Sep 2026 05:12:39 +0800 Subject: [PATCH] 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. --- src/components/SEO.astro | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/components/SEO.astro b/src/components/SEO.astro index 579ef81..25321aa 100644 --- a/src/components/SEO.astro +++ b/src/components/SEO.astro @@ -16,8 +16,29 @@ const { publishedTime, } = Astro.props; -const canonicalURL = Astro.props.canonicalURL || new URL(Astro.url.pathname, Astro.site || Astro.url.origin || 'https://crischarles.com').href; -const socialImageURL = new URL(image, Astro.site || Astro.url.origin || 'https://crischarles.com').href; +const siteOrigin = Astro.site ? new URL(Astro.site).origin : 'https://crischarles.com'; +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 = [ 'https://github.com/ssbg04', @@ -86,7 +107,7 @@ const schemaData = { - +