Skip to content

Fix: Duplicate og:type when using extra_custom_props #264

@lucabfelix

Description

@lucabfelix

Problem:

When extra_custom_props is used to define og:type, the template ends up rendering the tag twice:

Once from extra_custom_props

Again from object_type or og_type

This results in duplicated tags, which breaks Open Graph validation and causes incorrect previews on platforms like Facebook and WhatsApp.

Example

Model configuration:

def get_extra_custom_props(self):
return [
('property', 'og:type', 'product'),
('property', 'product:price:amount', '100.00'),
]

_metadata = {
'object_type': 'article',
'extra_custom_props': 'get_extra_custom_props',
}

Current HTML output (incorrect):

Expected behavior

If og:type is already defined via extra_custom_props, it should not be rendered again from object_type or og_type.

Proposed solutions
Option 1: Prevent duplication in the template (preferred)

Add a helper method to detect whether og:type is already defined in extra_custom_props and skip rendering it again.

Template change

File: meta/templates/meta/meta.html

Before:

{% if meta.og_type %}{% og_prop 'type' meta.og_type %}
{% elif meta.object_type %}{% og_prop 'type' meta.object_type %}{% endif %}

After:

{# Render og:type only if it is not already defined in extra_custom_props #}
{% if not meta.has_og_type_in_custom_props %}
{% if meta.og_type %}{% og_prop 'type' meta.og_type %}
{% elif meta.object_type %}{% og_prop 'type' meta.object_type %}{% endif %}
{% endif %}

Helper method

File: meta/views.py

def has_og_type_in_custom_props(self):
"""
Returns True if og:type is defined in extra_custom_props.
"""
if not self.extra_custom_props:
return False

# Expected format: [('property', 'og:type', 'value'), ...]
for prop in self.extra_custom_props:
    if (
        len(prop) >= 3
        and prop[0] == 'property'
        and prop[1] == 'og:type'
    ):
        return True

return False

Option 2: Document the limitation (simpler alternative)

Since extra_custom_props is rendered before the default Open Graph tags, the documentation could explicitly state that object_type should not be used together with a custom og:type.

Documentation note

If you define og:type using extra_custom_props, do not also define object_type, as this will cause duplicated meta tags.

Incorrect example:

_metadata = {
'object_type': 'article',
'extra_custom_props': 'get_custom_props',
}

def get_custom_props(self):
return [
('property', 'og:type', 'product'),
]

Correct example:

_metadata = {
'extra_custom_props': 'get_custom_props',
}

def get_custom_props(self):
return [
('property', 'og:type', 'product'),
]

Testing
Case 1: extra_custom_props defines og:type
meta = Meta(
title='Test',
extra_custom_props=[
('property', 'og:type', 'product'),
]
)

assert meta.has_og_type_in_custom_props() is True

Case 2: Only object_type defined
meta = Meta(
title='Test',
object_type='article',
)

assert meta.has_og_type_in_custom_props() is False

Case 3: Both defined
meta = Meta(
title='Test',
object_type='article',
extra_custom_props=[
('property', 'og:type', 'product'),
]
)

extra_custom_props should take precedence

assert meta.has_og_type_in_custom_props() is True

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions