Skip to content

Commit 3d414f6

Browse files
authored
fix og image (#258)
1 parent c90ff51 commit 3d414f6

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/layouts/GlobalLayout.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const { title, description, focus, image } = Astro.props;
1818
---
1919

2020
<html lang="ja" class="bg-gray-50 text-gray-800" data-theme="light">
21-
<Meta {title} {description} image={image} />
21+
<head>
22+
<Meta {title} {description} image={image} />
23+
</head>
2224
<body class="min-h-full w-full">
2325
<ClientRouter />
2426
<Header {focus} />

src/layouts/meta.astro

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
import { addOrigin } from "+/lib/utils";
3+
24
interface Props {
35
title: string | null;
46
description: string | null;
@@ -22,7 +24,10 @@ const url = Astro.url.href;
2224
<meta property="og:site_name" content="ut.code();" />
2325
<meta property="og:title" content={title} />
2426
{description && <meta property="og:description" content={description} />}
25-
<meta property="og:image" content={image?.src ?? "/utcode-logo/normal.png"} />
27+
<meta
28+
property="og:image"
29+
content={addOrigin(image?.src ?? "/utcode-logo/normal.png", Astro.site)}
30+
/>
2631
<meta property="og:url" content={url} />
2732
<meta property="og:locale" content="ja_JP" />
2833
<meta name="twitter:card" content="summary_large_image" />

src/lib/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function addOrigin(url: string, site: URL | undefined) {
2+
if (url.startsWith("http://") || url.startsWith("https://")) {
3+
return url;
4+
}
5+
if (url.startsWith("/")) {
6+
if (!site) throw new Error("site is undefined");
7+
return site.origin + url;
8+
}
9+
console.warn("WARNING: unknown URL schema:", url);
10+
return url;
11+
}

0 commit comments

Comments
 (0)