Skip to content

Commit dad17b6

Browse files
authored
Merge pull request #80 from OurSQLFoundation/fix/og-image-page-bundles
Fix broken og:image (share preview) on pages with `images`
2 parents 084fb82 + 6db5a64 commit dad17b6

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

‎layouts/partials/head.html‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,31 @@
2121
<link rel="alternate" type="{{ .MediaType.Type }}" href="{{ .Permalink }}" title="{{ $.Site.Title }} — {{ $.Title }}">
2222
{{ end }}
2323

24-
{{/* ── OG image: front matter → default social cover ── */}}
24+
{{/* ── OG image: `image` → first of `images` → default social cover ── */}}
2525
{{ $ogImage := "" }}
26+
{{ $ogW := 1200 }}
27+
{{ $ogH := 630 }}
2628
{{ if .Params.image }}
2729
{{ $res := resources.Get .Params.image }}
2830
{{ if not $res }}{{ $res = .Resources.Get .Params.image }}{{ end }}
2931
{{ with $res }}
3032
{{ $ogImage = (. | fingerprint).Permalink }}
3133
{{ end }}
32-
{{ else if .Params.images }}
33-
{{ $ogImage = absURL (index .Params.images 0) }}
34-
{{ else }}
34+
{{ end }}
35+
{{ if and (not $ogImage) .Params.images }}
36+
{{/* `images` entries are filenames inside the page's own bundle (logo.png, banner.jpg,
37+
photo.jpg…), so they must go through .Resources — absURL on a bare filename
38+
points at the site root and 404s. */}}
39+
{{ $file := index .Params.images 0 }}
40+
{{ $res := .Resources.Get $file }}
41+
{{ if not $res }}{{ $res = resources.Get (strings.TrimPrefix "/" $file) }}{{ end }}
42+
{{ with $res }}
43+
{{ $ogImage = (. | fingerprint).Permalink }}
44+
{{ $ogW = .Width }}
45+
{{ $ogH = .Height }}
46+
{{ end }}
47+
{{ end }}
48+
{{ if not $ogImage }}
3549
{{ $cover := resources.Get "images/social_cover.jpg" }}
3650
{{ if $cover }}
3751
{{ $ogImage = ($cover | fingerprint).Permalink }}
@@ -47,8 +61,8 @@
4761
<meta property="og:locale" content="en_US">
4862
{{ with $ogImage }}
4963
<meta property="og:image" content="{{ . }}">
50-
<meta property="og:image:width" content="1200">
51-
<meta property="og:image:height" content="630">
64+
<meta property="og:image:width" content="{{ $ogW }}">
65+
<meta property="og:image:height" content="{{ $ogH }}">
5266
{{ end }}
5367

5468
{{/* ── Article meta (blog posts only) ── */}}
@@ -60,7 +74,7 @@
6074
{{ end }}
6175

6276
{{/* ── Twitter Card ── */}}
63-
<meta name="twitter:card" content="{{ if $ogImage }}summary_large_image{{ else }}summary{{ end }}">
77+
<meta name="twitter:card" content="{{ if and $ogImage (ge (div (float $ogW) (float $ogH)) 1.4) }}summary_large_image{{ else }}summary{{ end }}">
6478
<meta name="twitter:title" content="{{ if .IsHome }}{{ .Site.Title }}{{ else }}{{ .Title }}{{ end }}">
6579
<meta name="twitter:description" content="{{ $desc }}">
6680
{{ with $ogImage }}<meta name="twitter:image" content="{{ . }}">{{ end }}

0 commit comments

Comments
 (0)