Skip to content

Commit

Permalink
Allow non-GOV.UK favicon and opengraph assets
Browse files Browse the repository at this point in the history
Fixes: #347

We already provide a way to non display the crown next to the service name by setting `show_govuk_logo: false` in config/tech-docs.yml. This is used by external users such as [MOJ](https://user-guide.modernisation-platform.service.justice.gov.uk).

This work extends the usage of that setting enabled custom favicon and opengraph image insted of the default Crown.
It's limited to just the `favicon.ico`, `favicon.svg` and `opengraph-image.png`, but this should suffice.

Users need to create those assets and place them in their `source/images` folder.
  • Loading branch information
kr8n3r committed Jan 21, 2025
1 parent d41b0d5 commit 9d88ddd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/govuk_tech_docs/meta_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def canonical_url
attr_reader :config, :current_page

def page_image
"#{host}/assets/govuk/assets/images/govuk-opengraph-image.png"
if config[:tech_docs][:show_govuk_logo]
"#{host}/assets/govuk/assets/images/govuk-opengraph-image.png"
else
"#{host}/images/opengraph-image.png"
end
end

def site_name
Expand Down
15 changes: 10 additions & 5 deletions lib/source/layouts/core.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
<%= stylesheet_link_tag :manifest %>

<link rel="canonical" href="<%= meta_tags.canonical_url %>">
<link rel="icon" sizes="48x48" href="/assets/govuk/assets/images/favicon.ico">
<link rel="icon" sizes="any" href="/assets/govuk/assets/images/favicon.svg" type="image/svg+xml">
<link rel="mask-icon" href="/assets/govuk/assets/images/govuk-icon-mask.svg" color="#0b0c0c">
<link rel="apple-touch-icon" href="/assets/govuk/assets/images/govuk-icon-180.png">
<link rel="manifest" href="/assets/govuk/assets/manifest.json">
<% if config[:tech_docs][:show_govuk_logo] %>
<link rel="icon" sizes="48x48" href="/assets/govuk/assets/images/favicon.ico">
<link rel="icon" sizes="any" href="/assets/govuk/assets/images/favicon.svg" type="image/svg+xml">
<link rel="mask-icon" href="/assets/govuk/assets/images/govuk-icon-mask.svg" color="#0b0c0c">
<link rel="apple-touch-icon" href="/assets/govuk/assets/images/govuk-icon-180.png">
<link rel="manifest" href="/assets/govuk/assets/manifest.json">
<% else %>
<link rel="icon" sizes="48x48" href="/images/favicon.ico">
<link rel="icon" sizes="any" href="/images/favicon.svg" type="image/svg+xml">
<% end %>

<% meta_tags.tags.each do |name, content| %>
<%= tag :meta, name: name, content: content %>
Expand Down
52 changes: 52 additions & 0 deletions spec/govuk_tech_docs/meta_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def generate_title(site_name:, page_title:)
host: "https://www.example.org",
service_name: "Foo",
full_service_name: "Test Site",
show_govuk_logo: true,
)

current_page = double("current_page",
Expand All @@ -56,6 +57,31 @@ def generate_title(site_name:, page_title:)
)
end

it "returns standard meta tag with non GOV.UK twitter image" do
config = generate_config(
host: "https://www.example.org",
service_name: "Foo",
full_service_name: "Test Site",
show_govuk_logo: false,
)

current_page = double("current_page",
data: { description: "The description.", title: "The Title" },
url: "/foo.html",
metadata: { locals: {} })

tags = GovukTechDocs::MetaTags.new(config, current_page).tags

expect(tags).to eql(
"description" => "The description.",
"twitter:card" => "summary",
"twitter:domain" => "www.example.org",
"twitter:image" => "https://www.example.org/images/opengraph-image.png",
"twitter:title" => "The Title - Test Site",
"twitter:url" => "https://www.example.org/foo.html",
)
end

it "adds a noindex robots tag when the site config prevents indexing" do
config = generate_config(
prevent_indexing: true,
Expand Down Expand Up @@ -128,6 +154,7 @@ def generate_title(site_name:, page_title:)
host: "https://www.example.org",
service_name: "Foo",
full_service_name: "Test Site",
show_govuk_logo: true,
)

current_page = double("current_page",
Expand All @@ -147,6 +174,31 @@ def generate_title(site_name:, page_title:)
)
end

it "returns a custom opengraph meta tag image" do
config = generate_config(
host: "https://www.example.org",
service_name: "Foo",
full_service_name: "Test Site",
show_govuk_logo: false,
)

current_page = double("current_page",
data: { description: "The description.", title: "The Title" },
url: "/foo.html",
metadata: { locals: {} })

og_tags = GovukTechDocs::MetaTags.new(config, current_page).opengraph_tags

expect(og_tags).to eql(
"og:description" => "The description.",
"og:image" => "https://www.example.org/images/opengraph-image.png",
"og:site_name" => "Test Site",
"og:title" => "The Title",
"og:type" => "object",
"og:url" => "https://www.example.org/foo.html",
)
end

it "uses the local variable as page title for proxied pages" do
current_page = double("current_page",
data: { description: "The description." },
Expand Down

0 comments on commit 9d88ddd

Please sign in to comment.