Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Open Graph image URL to be customised #2673

Merged
merged 2 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ You can use this option instead of setting the boolean `selected` option on each

This change was introduced in [pull request #2616: Allow selecting options by passing current values](https://github.com/alphagov/govuk-frontend/pull/2616).

#### Customise the Open Graph image URL without duplicate meta tags

You can now customise the Open Graph image URL included in the `<head>` by setting the `opengraphImageUrl` Nunjucks option.

Additionally, the default Open Graph image URL meta tag will now only be included if either `opengraphImageUrl` or `assetUrl` is set.

This change was introduced in [pull request #2673: Allow Open Graph image URL to be customised](https://github.com/alphagov/govuk-frontend/pull/2673).

### Recommended changes

#### Replace deprecated `govuk-header__link--service-name` class in the header
Expand Down
10 changes: 5 additions & 5 deletions src/govuk/template.njk
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{% from "./components/skip-link/macro.njk" import govukSkipLink -%}
{% from "./components/header/macro.njk" import govukHeader -%}
{% from "./components/footer/macro.njk" import govukFooter -%}
{# specify absolute url for the static assets folder e.g. http://wwww.domain.com/assets #}
{%- set assetUrl = assetUrl | default(assetPath) -%}
<!DOCTYPE html>
<html lang="{{ htmlLang | default('en') }}" class="govuk-template {{ htmlClasses }}">
<head>
Expand All @@ -23,9 +21,11 @@
{% endblock %}

{% block head %}{% endblock %}
{# The default og:image is added below head so that scrapers see any custom metatags first, and this is just a fallback #}
{# image url needs to be absolute e.g. http://wwww.domain.com/.../govuk-opengraph-image.png #}
<meta property="og:image" content="{{ assetUrl | default('/assets') }}/images/govuk-opengraph-image.png">

{# OpenGraph images needs to be absolute, so we need either a URL for the image or for assetUrl to be set #}
{% if opengraphImageUrl or assetUrl %}
<meta property="og:image" content="{{ opengraphImageUrl | default(assetUrl + '/images/govuk-opengraph-image.png') }}">
{% endif %}
</head>
<body class="govuk-template__body {{ bodyClasses }}" {%- for attribute, value in bodyAttributes %} {{attribute}}="{{value}}"{% endfor %}>
<script{% if cspNonce %} nonce="{{ cspNonce }}"{% endif %}>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
Expand Down
27 changes: 18 additions & 9 deletions src/govuk/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,27 @@ describe('Template', () => {
expect($icon.attr('href')).toEqual('/whatever/images/favicon.ico')
})

it('uses a default assets URL of whatever assetPath is', () => {
const $ = renderTemplate({ assetPath: '/whatever' })
const $ogImage = $('meta[property="og:image"]')
describe('opengraph image', () => {
it('is not included if neither assetUrl nor opengraphImageUrl are set ', () => {
const $ = renderTemplate({})
const $ogImage = $('meta[property="og:image"]')

expect($ogImage.attr('content')).toEqual('/whatever/images/govuk-opengraph-image.png')
})
expect($ogImage.length).toBe(0)
})

it('can have the assets URL overridden using assetUrl', () => {
const $ = renderTemplate({ assetUrl: '//a.gov.uk' })
const $ogImage = $('meta[property="og:image"]')
it('is included using default path and filename if assetUrl is set', () => {
const $ = renderTemplate({ assetUrl: 'https://foo.com/my-assets' })
const $ogImage = $('meta[property="og:image"]')

expect($ogImage.attr('content')).toEqual('//a.gov.uk/images/govuk-opengraph-image.png')
expect($ogImage.attr('content')).toEqual('https://foo.com/my-assets/images/govuk-opengraph-image.png')
})

it('is included if opengraphImageUrl is set', () => {
const $ = renderTemplate({ opengraphImageUrl: 'https://foo.com/custom/og-image.png' })
const $ogImage = $('meta[property="og:image"]')

expect($ogImage.attr('content')).toEqual('https://foo.com/custom/og-image.png')
})
})

describe('<meta name="theme-color">', () => {
Expand Down