Improve metadata with alternate language support
Updated metadata component to add support for alternate languages and a canonical link.
This commit is contained in:
parent
9bd4ff79ac
commit
f56f433fac
1 changed files with 49 additions and 9 deletions
|
|
@ -15,36 +15,74 @@ const defaultProps = {
|
||||||
// Extract props with default values assigned from defaultProps. Values can be overridden when the component is used.
|
// Extract props with default values assigned from defaultProps. Values can be overridden when the component is used.
|
||||||
// For example:
|
// For example:
|
||||||
// <MainLayout title="Custom Title" meta="Custom description." />
|
// <MainLayout title="Custom Title" meta="Custom description." />
|
||||||
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
|
// Define the metadata for your website and individual pages
|
||||||
const URL = `${Astro.site}`; // Set the website URL in astro.config.mjs
|
const URL = `${Astro.site}`; // Set the website URL in astro.config.mjs
|
||||||
const author = SITE.author;
|
const author = SITE.author;
|
||||||
|
const canonical = Astro.url.href;
|
||||||
|
const basePath = Astro.url.pathname;
|
||||||
const ogTitle = OG.title;
|
const ogTitle = OG.title;
|
||||||
const ogDescription = OG.description;
|
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 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 `<link rel="alternate" hreflang="${lang}" href="${href}" />`;
|
||||||
|
})
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
// Generate and optimize the favicon images
|
// Generate and optimize the favicon images
|
||||||
const faviconSvg = await getImage({
|
const faviconSvg = await getImage({
|
||||||
src: faviconSvgSrc,
|
src: faviconSvgSrc,
|
||||||
format: 'svg',
|
format: "svg",
|
||||||
});
|
});
|
||||||
|
|
||||||
const appleTouchIcon = await getImage({
|
const appleTouchIcon = await getImage({
|
||||||
src: faviconSrc,
|
src: faviconSrc,
|
||||||
width: 180,
|
width: 180,
|
||||||
height: 180,
|
height: 180,
|
||||||
format: 'png',
|
format: "png",
|
||||||
});
|
});
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<!-- Inject structured data into the page if provided. This data is formatted as JSON-LD, a method recommended by Google for structured data pass:
|
<!-- Inject structured data into the page if provided. This data is formatted as JSON-LD, a method recommended by Google for structured data pass:
|
||||||
https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data -->
|
https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data -->{
|
||||||
{structuredData && (
|
structuredData && (
|
||||||
<script type="application/ld+json" set:html={JSON.stringify(structuredData)}></script>
|
<script
|
||||||
)}
|
type="application/ld+json"
|
||||||
|
set:html={JSON.stringify(structuredData)}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
<!-- Define the character set, description, author, and viewport settings -->
|
<!-- Define the character set, description, author, and viewport settings -->
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta content={meta} name="description" />
|
<meta content={meta} name="description" />
|
||||||
|
|
@ -54,6 +92,8 @@ const appleTouchIcon = await getImage({
|
||||||
content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0"
|
content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0"
|
||||||
/>
|
/>
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||||
|
<link rel="canonical" href={canonical} />
|
||||||
|
<Fragment set:html={alternateLanguageLinks} />
|
||||||
|
|
||||||
<!-- Facebook Meta Tags -->
|
<!-- Facebook Meta Tags -->
|
||||||
<meta property="og:locale" content="en_US" />
|
<meta property="og:locale" content="en_US" />
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue