Fix og:url and canonical to use pretty URL when available
This commit is contained in:
parent
0109c51f83
commit
462b941b71
1 changed files with 8 additions and 2 deletions
|
|
@ -21,6 +21,12 @@ interface Props {
|
||||||
|
|
||||||
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
|
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
|
||||||
|
|
||||||
|
// For pretty URLs (e.g., /slug), use the pretty URL as canonical
|
||||||
|
const requestedPath = Astro.request.url.replace(Astro.site?.href || '', '/').replace(/\/$/, '');
|
||||||
|
const isPrettyUrl = requestedPath.startsWith('/20') || /^\/\d{4}-/.test(requestedPath);
|
||||||
|
const finalCanonicalPath = isPrettyUrl ? requestedPath : Astro.url.pathname;
|
||||||
|
const finalCanonicalURL = new URL(finalCanonicalPath, Astro.site);
|
||||||
|
|
||||||
const { title, description, image = "/astro-micro.jpg" } = Astro.props;
|
const { title, description, image = "/astro-micro.jpg" } = Astro.props;
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -35,7 +41,7 @@ const { title, description, image = "/astro-micro.jpg" } = Astro.props;
|
||||||
<meta name="generator" content={Astro.generator} />
|
<meta name="generator" content={Astro.generator} />
|
||||||
|
|
||||||
<!-- Canonical URL -->
|
<!-- Canonical URL -->
|
||||||
<link rel="canonical" href={canonicalURL} />
|
<link rel="canonical" href={finalCanonicalURL} />
|
||||||
|
|
||||||
<!-- Primary Meta Tags -->
|
<!-- Primary Meta Tags -->
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
|
|
@ -44,7 +50,7 @@ const { title, description, image = "/astro-micro.jpg" } = Astro.props;
|
||||||
|
|
||||||
<!-- Open Graph / Facebook -->
|
<!-- Open Graph / Facebook -->
|
||||||
<meta property="og:type" content="website" />
|
<meta property="og:type" content="website" />
|
||||||
<meta property="og:url" content={Astro.url} />
|
<meta property="og:url" content={finalCanonicalURL} />
|
||||||
<meta property="og:title" content={title} />
|
<meta property="og:title" content={title} />
|
||||||
<meta property="og:description" content={description} />
|
<meta property="og:description" content={description} />
|
||||||
<meta property="og:image" content={new URL(image, Astro.url)} />
|
<meta property="og:image" content={new URL(image, Astro.url)} />
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue