Skip to content

Commit

Permalink
fix: og:image will be incorrect if the image uses a cross-domain URL
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Dec 24, 2022
1 parent 4b6ccbc commit 8de1abd
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@

{% if page.image %}
{% assign img = page.image.path | default: page.image %}
{% assign img_path = page.img_path | append: '/' | append: img | replace: '//', '/' %}
{% capture target %}"{{ img | absolute_url }}"{% endcapture %}

{% if site.img_cdn contains '//' %}
<!-- it's a cross-origin URL -->
{% capture replacement %}"{{ site.img_cdn }}{{ img_path }}"{% endcapture %}
{% else %}
<!-- it's a local file path -->
{%- capture replacement -%}
"{{ site.img_cdn | append: '/' | append: img_path | replace: '//', '/' | absolute_url }}"
{%- endcapture -%}
{% endif %}

{% assign seo_tags = seo_tags | replace: target, replacement %}
{% unless img contains '://' %}
{% assign img_path = page.img_path | append: '/' | append: img | replace: '//', '/' %}
{% capture target %}"{{ img | absolute_url }}"{% endcapture %}

{% if site.img_cdn contains '//' %}
<!-- it's a cross-origin URL -->
{% capture replacement %}"{{ site.img_cdn }}{{ img_path }}"{% endcapture %}
{% else %}
<!-- it's a local file path -->
{%- capture replacement -%}
"{{ site.img_cdn | append: '/' | append: img_path | replace: '//', '/' | absolute_url }}"
{%- endcapture -%}
{% endif %}

{% assign seo_tags = seo_tags | replace: target, replacement %}

{% endunless %}

{% endif %}

{{ seo_tags }}
Expand Down

0 comments on commit 8de1abd

Please sign in to comment.