From f56f433fac4dba33f3dce64c74ba2fe5315e0b6e Mon Sep 17 00:00:00 2001
From: Emil Gulamov <125820963+mearashadowfax@users.noreply.github.com>
Date: Sat, 30 Mar 2024 03:34:10 +0400
Subject: [PATCH] Improve metadata with alternate language support
Updated metadata component to add support for alternate languages and a canonical link.
---
src/components/Meta.astro | 58 +++++++++++++++++++++++++++++++++------
1 file changed, 49 insertions(+), 9 deletions(-)
diff --git a/src/components/Meta.astro b/src/components/Meta.astro
index cb3181c..208219a 100644
--- a/src/components/Meta.astro
+++ b/src/components/Meta.astro
@@ -15,36 +15,74 @@ const defaultProps = {
// Extract props with default values assigned from defaultProps. Values can be overridden when the component is used.
// For example:
//
-const { meta = defaultProps.meta, structuredData = defaultProps.structuredData } = Astro.props;
+const {
+ meta = defaultProps.meta,
+ structuredData = defaultProps.structuredData,
+} = Astro.props;
// Define the metadata for your website and individual pages
const URL = `${Astro.site}`; // Set the website URL in astro.config.mjs
const author = SITE.author;
+const canonical = Astro.url.href;
+const basePath = Astro.url.pathname;
const ogTitle = OG.title;
const ogDescription = OG.description;
-const socialImageRes = await getImage({ src: OG.image, width: 1200, height: 600 });
+const socialImageRes = await getImage({
+ src: OG.image,
+ width: 1200,
+ height: 600,
+});
const socialImage = Astro.url.origin + socialImageRes.src; // Get the full URL of the image (https://stackoverflow.com/a/9858694)
+const languages: { [key: string]: string } = {
+ en: "",
+ fr: "fr",
+};
+
+function createHref(lang: string, prefix: string, path: string): string {
+ const hasPrefix = path.startsWith(`/${prefix}/`);
+
+ const basePath = hasPrefix ? path : `/${prefix}${path}`;
+ const normalizedBasePath = basePath.replace(/\/\/+/g, "/");
+ return `${URL.slice(0, -1)}${normalizedBasePath}`;
+}
+
+const fullPath: string = Astro.url.pathname;
+
+const alternateLanguageLinks: string = Object.entries(languages)
+ .map(([lang, prefix]: [string, string]) => {
+ const basePath: string =
+ lang === "en" ? fullPath.replace(/^\/fr\//, "/") : fullPath;
+
+ const href: string = createHref(lang, prefix, basePath);
+
+ return ``;
+ })
+ .join("\n");
+
// Generate and optimize the favicon images
const faviconSvg = await getImage({
src: faviconSvgSrc,
- format: 'svg',
+ format: "svg",
});
const appleTouchIcon = await getImage({
src: faviconSrc,
width: 180,
height: 180,
- format: 'png',
+ format: "png",
});
-
---
-{structuredData && (
-
-)}
+ https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data -->{
+ structuredData && (
+
+ )
+}
@@ -54,6 +92,8 @@ const appleTouchIcon = await getImage({
content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0"
/>
+
+