From 6b845b743736ea1ff0e2a33c0f78f8146d4637ca Mon Sep 17 00:00:00 2001 From: prototypa Date: Sun, 11 Sep 2022 06:25:00 -0400 Subject: [PATCH] Better SEO --- package.json | 2 + src/components/core/MetaTags.astro | 52 +++++++++++-------- src/pages/[...blog]/[...page].astro | 1 + src/pages/[...blog]/[slug].astro | 2 + .../[category]/[...page].astro | 3 +- src/pages/[...tags]/[tag]/[...page].astro | 3 +- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 369c316..9a0f51b 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "start": "astro dev", "build": "astro build", "preview": "astro preview", + "astro": "astro", "format": "prettier -w .", "lint:eslint": "eslint . --ext .js,.ts,.astro", "subfont": "subfont -ir --no-fallbacks --silent --root dist" @@ -18,6 +19,7 @@ "@astrojs/rss": "^1.0.0", "@astrojs/sitemap": "^1.0.0", "@astrojs/tailwind": "^1.0.0", + "@astrolib/seo": "^0.2.0", "@fontsource/inter": "^4.5.12", "@tailwindcss/typography": "^0.5.4", "@typescript-eslint/eslint-plugin": "^5.36.2", diff --git a/src/components/core/MetaTags.astro b/src/components/core/MetaTags.astro index 576f5b0..6354ff8 100644 --- a/src/components/core/MetaTags.astro +++ b/src/components/core/MetaTags.astro @@ -1,4 +1,5 @@ --- +import { AstroSeo } from "@astrolib/seo" import { getImage } from '@astrojs/image'; import { getRelativeUrlByFilePath } from '~/utils/directories'; @@ -19,9 +20,13 @@ const { title = SITE.name, description = '', image: _image = defaultImage, + canonical, noindex = false, nofollow = false, + + ogTitle = title, + ogType = 'website', } = Astro.props; const image = @@ -35,30 +40,33 @@ const image = -{title} - -{canonical && } + + canonical={canonical} + noindex={noindex} + nofollow={nofollow} - - - -{image && } - - -{canonical && } - - - -{image && } - - -{canonical && } - - - -{image && } + openGraph={{ + url: canonical, + title: ogTitle, + description: description, + type: ogType, + images: image ? [ + { + url: image.toString(), + alt: ogTitle, + }, + ] : undefined, + // site_name: 'SiteName', + }} + twitter={{ + // handle: '@handle', + // site: '@site', + cardType: image ? 'summary_large_image' : undefined, + }} +/> diff --git a/src/pages/[...blog]/[...page].astro b/src/pages/[...blog]/[...page].astro index cb893be..5944dcd 100644 --- a/src/pages/[...blog]/[...page].astro +++ b/src/pages/[...blog]/[...page].astro @@ -26,6 +26,7 @@ const meta = { title: `Blog ${currentPage > 1 ? `— Page ${currentPage} ` : ''}— ${SITE.name}`, description: SITE.description, canonical: getCanonical(getPermalink(page.url.current)), + ogType: "blog" }; --- diff --git a/src/pages/[...blog]/[slug].astro b/src/pages/[...blog]/[slug].astro index 243490c..1a03ee7 100644 --- a/src/pages/[...blog]/[slug].astro +++ b/src/pages/[...blog]/[slug].astro @@ -29,6 +29,8 @@ const meta = { description: post.description, canonical: post.canonical || getCanonical(getPermalink(post.slug, 'post')), image: await findImage(post.image), + ogTitle: post.title, + ogType: "article" }; --- diff --git a/src/pages/[...categories]/[category]/[...page].astro b/src/pages/[...categories]/[category]/[...page].astro index 93901e6..cfd6549 100644 --- a/src/pages/[...categories]/[category]/[...page].astro +++ b/src/pages/[...categories]/[category]/[...page].astro @@ -38,10 +38,11 @@ const meta = { title: `Category '${category}' ${currentPage > 1 ? `— Page ${currentPage} ` : ''}— ${SITE.name}`, description: SITE.description, canonical: getCanonical(getPermalink(page.url.current)), + noindex: true, }; --- - + Category: {category} diff --git a/src/pages/[...tags]/[tag]/[...page].astro b/src/pages/[...tags]/[tag]/[...page].astro index 893052f..a1bdfc6 100644 --- a/src/pages/[...tags]/[tag]/[...page].astro +++ b/src/pages/[...tags]/[tag]/[...page].astro @@ -38,10 +38,11 @@ const meta = { title: `Posts by tag '${tag}' ${currentPage > 1 ? `— Page ${currentPage} ` : ''}— ${SITE.name}`, description: SITE.description, canonical: getCanonical(getPermalink(page.url.current)), + noindex: true }; --- - + Tag: {tag}