Skip to content

Conversation

@ziemann-engineering
Copy link

Description

Currently, the head parameter in gr.Blocks() adds content via client-side JavaScript, which doesn't work for social media crawlers that don't execute JavaScript. This commit renders the head content server-side using Jinja2 templating, ensuring meta tags are present in the initial HTML response.

The head content is already available in the config dict (added in blocks.py:get_config_file()), so this simply inserts it into the template before the default Open Graph tags.

I tested this locally by modifying gradio/templates/frontend/index.html, I am not 100% sure this targets the correct file.

Note: there are now several redundant meta tag sections in the rendered page. I left the client-side insertion in the code, this can likely be removed or modified. In addition, there are still the hardcoded tags, which have duplicates.

Closes: #11233

AI Disclosure

We encourage the use of AI tooling in creating PRs, but the any non-trivial use of AI needs be disclosed. E.g. if you used Claude to write a first draft, you should mention that. Trivial tab-completion doesn't need to be disclosed. You should self-review all PRs, especially if they were generated with AI.

  • I used AI to figure out the issue and write the fix. Manually reviewed and tested with https://www.opengraph.xyz/ (and signal messenger). My modified page is up at ki.ziemann-engineering.ch.

ziemann-engineering and others added 2 commits November 3, 2025 12:36
Currently, the head parameter in gr.Blocks() adds content via client-side JavaScript, which doesn't work for social media crawlers that don't execute JavaScript. This commit renders the head content server-side using Jinja2 templating, ensuring meta tags are present in the initial HTML response.

The head content is already available in the config dict (added in
blocks.py:get_config_file()), so this simply inserts it into the template
before the default Open Graph tags.

This fix enables custom social media preview cards for Gradio apps.
Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this would work for server-side-rendered gradio apps since 1) they use a different html template (js/app/src/app.html) and 2) it is created before the config is fetched in the server

@freddyaboulton
Copy link
Collaborator

Closing this for now. Thanks @ziemann-engineering

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom meta tags not fully respected in Telegram

3 participants