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

Gradio meta tags override issue #10701

Open
1 task done
zgreathouse opened this issue Mar 1, 2025 · 3 comments
Open
1 task done

Gradio meta tags override issue #10701

zgreathouse opened this issue Mar 1, 2025 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@zgreathouse
Copy link

Describe the bug

Problem Description

When attempting to set custom meta tags in a Gradio application via the head parameter for Blocks, Gradio overrides or conflicts with custom meta tags, particularly Open Graph and Twitter Card meta tags. This makes it difficult to properly set up social media previews for a Gradio application.

Steps to Reproduce

  • Create a simple Gradio application with custom social media (twitter) meta tags.
  • Inspect the HTML source of the resulting page in the browser
  • Notice that the twitter meta tags have been overridden by Gradio's default values.
    (See attached sample code)

Expected Behavior

Custom meta tags provided through the head parameter should take precedence over any default meta tags Gradio adds, especially Open Graph and Twitter Card meta tags.

Actual Behavior

Gradio overwrites the custom meta tags with its own values.
The specific tags I notice this behavior with are:

  • property=og:description
  • property=og:image
  • name=twitter:creator
  • name=twitter:description
  • name=twitter:image

Impact

This issue prevents Gradio applications from properly setting up social media previews, which is critical for applications that are shared on platforms like Twitter, Facebook, Slack, and other social media sites. Without the ability to set custom meta tags, Gradio applications cannot control how they appear when shared on these platforms. (There are work arounds like serving the gradio app in an iframe, but it would be so much simpler if the head parameter worked as expected.)

Possible Solution

Consider adding a specific parameter for social media meta tags or enhancing the existing head parameter to ensure custom meta tags take precedence over any default values.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr

def build_gradio_interface() -> gr.Blocks:
  custom_head = """
  <!-- HTML Meta Tags -->
  <title>Sample App</title>
  <meta name="description" content="An open-source web application showcasing various features and capabilities.">
  
  <!-- Facebook Meta Tags -->
  <meta property="og:url" content="https://example.com">
  <meta property="og:type" content="website">
  <meta property="og:title" content="Sample App">
  <meta property="og:description" content="An open-source web application showcasing various features and capabilities.">
  <meta property="og:image" content="https://example.com/sample-image.jpg">
  
  <!-- Twitter Meta Tags -->
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:creator" content="@example_user">
  <meta name="twitter:title" content="Sample App">
  <meta name="twitter:description" content="An open-source web application showcasing various features and capabilities.">
  <meta name="twitter:image" content="https://example.com/sample-image.jpg">
  <meta property="twitter:domain" content="example.com">
  <meta property="twitter:url" content="https://example.com">
  
  <!-- Meta Tags Generated via https://www.opengraph.xyz/ -->
  """
  
  with gr.Blocks(
    title="My App",
    head=custom_head,
  ) as demo:
    gr.HTML("<h1>My App</h1>")

  return demo

demo = build_gradio_interface()
demo.launch()

Screenshot

No response

Logs

System Info

- Gradio version: 5.18.0
- Browser: Chrome/Firefox
- OS: Windows/Mac/Linux

Severity

I can work around it

@zgreathouse zgreathouse added the bug Something isn't working label Mar 1, 2025
@abidlabs abidlabs marked this as a duplicate of #8891 Mar 1, 2025
@abidlabs
Copy link
Member

abidlabs commented Mar 1, 2025

Thanks @zgreathouse we'll consider proposing an API for this! cc @dawoodkhan82

@dwipper
Copy link

dwipper commented Mar 1, 2025

@zgreathouse Here was the script I wrote that worked:

<script>
  // Update viewport tag
  var metaTag = document.querySelector('meta[name="viewport"]');
  var content = metaTag.getAttribute('content');
  metaTag.setAttribute('content', content + ', maximum-scale=1');

  // Add in meta description tag
  var head = document.head;
  var firstElementInHead = head.firstChild;
  var metaDescription = document.createElement('meta');
  metaDescription.name = "description";
  metaDescription.content = "Application Description";
  var ico_link = document.createElement('link');
  ico_link.rel = 'icon';
  ico_link.href = 'file/favicon.ico';
  head.insertBefore(metaDescription, firstElementInHead);
  var firstElementInHead = head.firstChild;
  head.insertBefore(ico_link, firstElementInHead);
  
//Function to update various meta tags
    function updateMetaContent(selector, propertyOrName, value) {
        var meta = document.querySelector(selector + '[' + propertyOrName + ']');
        if (meta) {
            meta.content = value;
        }
    }
    // Updating each meta tag
    updateMetaContent('meta[property]', 'property="og:url"', 'https://xxxxx.com/');
    updateMetaContent('meta[property]', 'property="og:title"', 'Application Title');
    updateMetaContent('meta[property]', 'property="og:image"', 'https://xxxxx.com/iimage.png');
    updateMetaContent('meta[property]', 'property="og:description"', 'Application Description');
    updateMetaContent('meta[name]', 'name="twitter:creator"', 'xxxxxx.com');
    updateMetaContent('meta[name]', 'name="twitter:description"', 'Application Description');
    updateMetaContent('meta[name]', 'name="twitter:image"', 'https://xxxxx.com/iimage.png');
</script>

@infwinston
Copy link

+1 I'm encountering the same issue and looking for workaround for this issue. Is it possible to patch my gradio locally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants