Skip to content

Commit 6027e54

Browse files
committed
More OG image tweaks
1 parent 5db161e commit 6027e54

6 files changed

Lines changed: 34 additions & 35 deletions

File tree

lib/jola_dev_web/components/layouts/root.html.heex

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@
2727
<meta property="og:type" content={if(@conn.assigns[:post], do: "article", else: "website")} />
2828
<meta
2929
property="og:image"
30-
content={
31-
Phoenix.VerifiedRoutes.unverified_url(
32-
JolaDevWeb.Endpoint,
33-
@conn.assigns[:og_image] || "/images/og-image.png"
34-
)
35-
}
30+
content={JolaDevWeb.Helpers.OGImage.url_for(@conn)}
3631
/>
3732
<meta property="og:image:type" content="image/png" />
3833
<meta property="og:image:width" content="1200" />
@@ -63,12 +58,7 @@
6358
/>
6459
<meta
6560
name="twitter:image"
66-
content={
67-
Phoenix.VerifiedRoutes.unverified_url(
68-
JolaDevWeb.Endpoint,
69-
@conn.assigns[:og_image] || "/images/og-image.png"
70-
)
71-
}
61+
content={JolaDevWeb.Helpers.OGImage.url_for(@conn)}
7262
/>
7363
<meta name="twitter:image:alt" content={@conn.assigns[:page_title] || "jola.dev"} />
7464
<%= unless @conn.status && @conn.status >= 400 do %>
@@ -98,7 +88,7 @@
9888
</script>
9989
<link rel="icon" href="/favicon.ico" sizes="any" />
10090
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
101-
<%= for schema <- JolaDevWeb.SEO.json_ld(@conn) do %>
91+
<%= for schema <- JolaDevWeb.Helpers.SEO.json_ld(@conn) do %>
10292
<script type="application/ld+json">
10393
<%= raw(Jason.encode!(schema)) %>
10494
</script>

lib/jola_dev_web/controllers/blog_controller.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ defmodule JolaDevWeb.BlogController do
88
posts: posts,
99
page_title: "Blog | jola.dev",
1010
meta_description:
11-
"Blog posts by Johanna Larsson on software engineering, Elixir, and engineering leadership.",
12-
og_image: JolaDev.OGImage.path_for("posts")
11+
"Blog posts by Johanna Larsson on software engineering, Elixir, and engineering leadership."
1312
)
1413
end
1514

@@ -27,8 +26,7 @@ defmodule JolaDevWeb.BlogController do
2726
tag: tag,
2827
noindex: true,
2928
page_title: "Posts tagged \"#{tag}\" | jola.dev",
30-
meta_description: "Blog posts by Johanna Larsson tagged with #{tag}.",
31-
og_image: JolaDev.OGImage.path_for("posts/tag/#{tag}")
29+
meta_description: "Blog posts by Johanna Larsson tagged with #{tag}."
3230
)
3331
end
3432
end
@@ -39,8 +37,7 @@ defmodule JolaDevWeb.BlogController do
3937
post: post,
4038
related_posts: JolaDev.Blog.recent_posts(post),
4139
page_title: "#{post.title} | jola.dev",
42-
meta_description: post.description,
43-
og_image: JolaDev.OGImage.path_for("posts/#{post.id}")
40+
meta_description: post.description
4441
)
4542
else
4643
conn

lib/jola_dev_web/controllers/page_controller.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule JolaDevWeb.PageController do
88
page_title: "Johanna Larsson — Software Engineer & Speaker",
99
meta_description:
1010
"Johanna Larsson is a software engineer, engineering leader, writer, and speaker with many years of experience building products and leading teams.",
11-
og_image: JolaDev.OGImage.path_for("home"),
1211
recent_posts: recent_posts
1312
)
1413
end
@@ -18,7 +17,6 @@ defmodule JolaDevWeb.PageController do
1817
page_title: "About | jola.dev",
1918
meta_description:
2019
"About Johanna Larsson — software engineer, engineering leader, writer, and speaker with many years of experience.",
21-
og_image: JolaDev.OGImage.path_for("about"),
2220
page_type: :about
2321
)
2422
end
@@ -27,17 +25,15 @@ defmodule JolaDevWeb.PageController do
2725
render(conn, :projects,
2826
page_title: "Projects | jola.dev",
2927
meta_description:
30-
"Open source projects by Johanna Larsson, including HexDiff, ElixirEvents, and more.",
31-
og_image: JolaDev.OGImage.path_for("projects")
28+
"Open source projects by Johanna Larsson, including HexDiff, ElixirEvents, and more."
3229
)
3330
end
3431

3532
def talks(conn, _params) do
3633
render(conn, :talks,
3734
page_title: "Talks | jola.dev",
3835
meta_description:
39-
"Conference talks and presentations by Johanna Larsson on Elixir, distributed systems, and engineering leadership.",
40-
og_image: JolaDev.OGImage.path_for("talks")
36+
"Conference talks and presentations by Johanna Larsson on Elixir, distributed systems, and engineering leadership."
4137
)
4238
end
4339
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule JolaDevWeb.Helpers.OGImage do
2+
@moduledoc """
3+
Builds URLs for OG images from assigns and falls back to request_path.
4+
"""
5+
use JolaDevWeb, :verified_routes
6+
7+
def url_for(%{assigns: %{post: post}}), do: build("posts/#{post.id}")
8+
def url_for(%{assigns: %{tag: tag}}), do: build("posts/tag/#{tag}")
9+
def url_for(%{request_path: "/"}), do: build("home")
10+
def url_for(%{request_path: "/" <> rest}), do: build(rest)
11+
12+
defp build(slug),
13+
do: unverified_url(JolaDevWeb.Endpoint, JolaDev.OGImage.path_for(slug))
14+
end

lib/jola_dev_web/helpers/seo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule JolaDevWeb.SEO do
1+
defmodule JolaDevWeb.Helpers.SEO do
22
@moduledoc """
33
Generates JSON-LD structured data for search engines and AI systems.
44
"""

test/jola_dev_web/helpers/seo_test.exs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
defmodule JolaDevWeb.SEOTest do
1+
defmodule JolaDevWeb.Helpers.SEOTest do
22
use JolaDevWeb.ConnCase, async: true
33

4+
alias JolaDevWeb.Helpers.SEO
5+
46
describe "json_ld/1" do
57
test "includes WebSite schema on all pages", %{conn: conn} do
68
conn = get(conn, ~p"/")
7-
schemas = JolaDevWeb.SEO.json_ld(conn)
9+
schemas = SEO.json_ld(conn)
810

911
website = Enum.find(schemas, &(&1["@type"] == "WebSite"))
1012
assert website["name"] == "jola.dev"
@@ -14,7 +16,7 @@ defmodule JolaDevWeb.SEOTest do
1416
test "includes BlogPosting schema on blog post pages", %{conn: conn} do
1517
post = List.first(JolaDev.Blog.all_posts())
1618
conn = get(conn, ~p"/posts/#{post.id}")
17-
schemas = JolaDevWeb.SEO.json_ld(conn)
19+
schemas = SEO.json_ld(conn)
1820

1921
blog_posting = Enum.find(schemas, &(&1["@type"] == "BlogPosting"))
2022
assert blog_posting["headline"] == post.title
@@ -30,7 +32,7 @@ defmodule JolaDevWeb.SEOTest do
3032

3133
test "includes ProfilePage schema on about page", %{conn: conn} do
3234
conn = get(conn, ~p"/about")
33-
schemas = JolaDevWeb.SEO.json_ld(conn)
35+
schemas = SEO.json_ld(conn)
3436

3537
profile = Enum.find(schemas, &(&1["@type"] == "ProfilePage"))
3638
assert profile["mainEntity"]["@type"] == "Person"
@@ -40,21 +42,21 @@ defmodule JolaDevWeb.SEOTest do
4042

4143
test "does not include BlogPosting on non-post pages", %{conn: conn} do
4244
conn = get(conn, ~p"/projects")
43-
schemas = JolaDevWeb.SEO.json_ld(conn)
45+
schemas = SEO.json_ld(conn)
4446

4547
refute Enum.any?(schemas, &(&1["@type"] == "BlogPosting"))
4648
end
4749

4850
test "omits BreadcrumbList on the home page", %{conn: conn} do
4951
conn = get(conn, ~p"/")
50-
schemas = JolaDevWeb.SEO.json_ld(conn)
52+
schemas = SEO.json_ld(conn)
5153

5254
refute Enum.any?(schemas, &(&1["@type"] == "BreadcrumbList"))
5355
end
5456

5557
test "includes BreadcrumbList on /about", %{conn: conn} do
5658
conn = get(conn, ~p"/about")
57-
schemas = JolaDevWeb.SEO.json_ld(conn)
59+
schemas = SEO.json_ld(conn)
5860

5961
breadcrumb = Enum.find(schemas, &(&1["@type"] == "BreadcrumbList"))
6062

@@ -77,7 +79,7 @@ defmodule JolaDevWeb.SEOTest do
7779
test "includes BreadcrumbList with post title on post pages", %{conn: conn} do
7880
post = List.first(JolaDev.Blog.all_posts())
7981
conn = get(conn, ~p"/posts/#{post.id}")
80-
schemas = JolaDevWeb.SEO.json_ld(conn)
82+
schemas = SEO.json_ld(conn)
8183

8284
breadcrumb = Enum.find(schemas, &(&1["@type"] == "BreadcrumbList"))
8385
items = breadcrumb["itemListElement"]
@@ -90,7 +92,7 @@ defmodule JolaDevWeb.SEOTest do
9092
test "includes BreadcrumbList with tag on tag pages", %{conn: conn} do
9193
tag = "elixir"
9294
conn = get(conn, ~p"/posts/tag/#{tag}")
93-
schemas = JolaDevWeb.SEO.json_ld(conn)
95+
schemas = SEO.json_ld(conn)
9496

9597
breadcrumb = Enum.find(schemas, &(&1["@type"] == "BreadcrumbList"))
9698
items = breadcrumb["itemListElement"]

0 commit comments

Comments
 (0)