DEV Community

Cover image for An Emoji Banner for Your GitHub README: What GitHub's Image Proxy Does to It and How Long a Title Fits
FindUtils
FindUtils

Posted on

An Emoji Banner for Your GitHub README: What GitHub's Image Proxy Does to It and How Long a Title Fits

The first thing most people see of a project is its README, and a lot of READMEs open with a banner: an icon, the project name, maybe one line about what it does. Usually that's a PNG someone made in a design tool and committed to the repo. Then the project gets renamed, or someone wants a dark version, and the PNG is out of date.

A banner can also be a URL that draws the image when it's requested. EmojiFavicons has a /readme/ endpoint that does this: an 800×200 SVG with an emoji on the left and your title (and an optional subtitle) on the right. One line of Markdown:

![My Project](https://emojifavicons.com/readme/rocket?title=My+Project&subtitle=Built+with+love)
Enter fullscreen mode Exit fullscreen mode

That's the easy part. I wanted to know what GitHub does with an external image like this, whether a dark and a light version can switch with the visitor's theme, and how long a title can get before it runs off the banner. So I ran five README snippets through GitHub's Markdown API (it returns the HTML GitHub generates for Markdown), fetched every image URL in the result, loaded the HTML in headless Chrome 156 in light and dark mode at desktop and phone width, and measured titles in five different fonts. All the scripts and outputs are listed at the end.

What GitHub does to the image URL

GitHub never puts your image URL in the page. It rewrites it to its image proxy, camo.githubusercontent.com, followed by a signature and your original URL in hex. The original stays only in a data-canonical-src attribute. When someone opens the README, their browser asks GitHub's proxy, and the proxy fetches the image from the original server.

Here is what came back when I fetched the proxied banner and the original side by side:

Header From emojifavicons.com From GitHub's proxy
Status 200 200
content-type image/svg+xml image/svg+xml
cache-control public, max-age=3600 public, max-age=3600
content-length 619 619
content-security-policy (none) default-src 'none'; img-src data:; style-src 'unsafe-inline'

Three things follow from that.

The proxy passes the cache time through. The banner says "keep me for an hour", and so does the proxy. A visitor who has already seen the README may see an old banner for up to an hour after you change the URL's output. The /status/up icon I tested further down says 60 seconds, and the proxy passed 60 seconds on as well. Both requests I made a few seconds apart were cache misses at the proxy's edge (from two different edge servers), so I can't tell you how long the proxy itself holds an image. What I can say is that it doesn't stretch the cache time the image asks for.

Nothing external loads inside the SVG. The proxy adds a content security policy that allows only inline styles and data: images. On top of that, browsers never load external resources for an SVG shown with <img>. So if you build your own banner, a web font or a linked image inside the SVG won't show up. The text is drawn with whatever fonts the visitor's device has. That's why the banner's font-family is system-ui,-apple-system,sans-serif, and it matters for the title length below.

The original server sees GitHub, not your visitors. The request comes from the proxy, so the image server can't count README views or see who looked. For a banner that's fine. It also means an image in a README is a poor way to count visitors: the server sees the proxy's requests, and how often the proxy asks again depends on caching, not on how many people looked.

A dark and a light banner with <picture>

GitHub's page can be light or dark, and a dark banner on a white page (or the other way round) looks like a hole in the page. The banner endpoint has theme=dark (the default) and theme=light. To show the right one, GitHub supports the standard HTML <picture> element with a prefers-color-scheme media query:

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://emojifavicons.com/readme/rocket?title=My+Project&theme=dark">
  <img alt="My Project" src="https://emojifavicons.com/readme/rocket?title=My+Project&theme=light">
</picture>
Enter fullscreen mode Exit fullscreen mode

GitHub kept the <picture>, wrapped it in its own <themed-picture> element, and rewrote both URLs through the proxy. I loaded that HTML in Chrome with the color scheme set to light and then dark:

scheme=light viewport=900px -> light banner, natural 800x200, shown 800x200 px (title scaled to 32.0 px)
scheme=light viewport=375px -> light banner, natural 800x200, shown 343x86 px (title scaled to 13.7 px)
scheme=dark  viewport=900px -> dark banner, natural 800x200, shown 800x200 px (title scaled to 32.0 px)
scheme=dark  viewport=375px -> dark banner, natural 800x200, shown 343x86 px (title scaled to 13.7 px)
Enter fullscreen mode Exit fullscreen mode

The right banner showed up every time. GitHub also documents a second method: two plain images whose URLs end in #gh-dark-mode-only and #gh-light-mode-only. The renderer kept those fragments (in data-canonical-src and inside the proxied URL), but hiding the wrong one is done by GitHub's own stylesheet on github.com, which I couldn't run outside GitHub. <picture> is plain HTML that any browser understands, so that's the one I'd use.

One detail from the phone-width rows: GitHub scales images down to fit the column, so on a 375-pixel-wide screen the 800-pixel banner was shown 343 pixels wide. The 32-pixel title became 13.7 pixels, and an 18-pixel subtitle would be under 8 pixels, too small to read. Keep the subtitle short, and put anything important in the Markdown under the banner.

How long can the title be?

The title is a single line of text that starts at x=220 on an 800-pixel banner. It doesn't wrap or shrink, so a long title just runs past the right edge and gets cut off. And because the visitor's own font draws it, the same title has a different width on different devices.

I don't have every operating system's system font on my test machine, so I used five fonts that were installed as stand-ins (Noto Sans, Roboto, Inter, Open Sans and DejaVu Sans, which is noticeably wider). For each title I measured where the text ends with getComputedTextLength(). Anything past 800 is cut off:

Title Characters Noto Sans Roboto Inter Open Sans DejaVu Sans
My Project 10 389 375 388 390 412
Weekend Weather Station 23 637 591 627 638 692
İstanbul Şehir Rehberi API 26 638 598 625 636 696
Open Source Invoice Generator 29 718 667 708 718 784
A Fast Static Site Generator for Docs 37 796 747 788 796 885, cut
WWWWWWWWWWWWWWWWWW 18 777 724 830, cut 777 855, cut
Self-Hosted Analytics Dashboard Starter Kit 43 912, cut 852, cut 909, cut 914, cut 1015, cut

The 37-character title fit in four fonts with 4 to 53 pixels to spare and ran 85 pixels off the edge in the fifth. Character count is only a rough guide: 18 capital Ws were too wide for two of the fonts. But the ordinary titles of up to 29 characters fit in all five fonts with room left over. If your project name is longer than that, shorten the banner title and put the full name in the first line of the README.

Encode the title, and check the emoji name

The title goes in the query string, so characters with a meaning in URLs need encoding. The one that bites is &. With title=Tom+&+Jerry the banner said just "Tom", because everything after & became a separate (ignored) parameter. Encoded as title=Tom%20%26%20Jerry, it said "Tom & Jerry". In Python, urllib.parse.quote() does this; in JavaScript, encodeURIComponent().

Once the title arrived intact, the SVG handled it correctly. I sent eight awkward titles (Tom & Jerry, a < b, Say "hi", It's done, İstanbul Şehir Rehberi, Café ☕ Club, an Arabic word and <b>bold</b>), and every response was valid XML with the title shown as literal text. <b> stayed the text <b>bold</b> and didn't turn into markup.

The emoji part of the URL is a name, like rocket. A name the service doesn't know returns a 404 with the text "Emoji not found", and GitHub shows a broken image. Open the URL in a browser once before you commit it.

The emoji is drawn by the visitor's font

The banner doesn't contain a picture of a rocket. It contains the character 🚀 in a <text> element, and the visitor's device draws it with its own emoji font. On my test machine that was Noto Color Emoji. On an iPhone it's Apple's design, and on Windows it's Segoe UI Emoji. That's usually what you want, because it matches the emoji the visitor sees everywhere else. But a very new emoji can show up as an empty box on an older device. I checked which emoji the common fonts can draw in Can the Visitor's Device Draw This Emoji?. For a banner, pick an emoji that has been around for a few years.

A status icon in a README is not live

EmojiFavicons also has small status icons: /status/up (green circle), /status/down (red), /status/degraded (yellow) and /status/maintenance (wrench). They're 60-second-cache SVGs, and they render fine in a README:

<img src="https://emojifavicons.com/status/up" width="16" height="16" alt=""> API: operational
Enter fullscreen mode Exit fullscreen mode

GitHub kept the width and height and added its own max-height: 16px. But notice what decides the colour: the URL. /status/up is always green, because nothing checks your API. Put it in a README and it stays green until someone edits the README and commits. The 60-second cache doesn't change that.

These icons make sense where your own code picks the URL: a status page or an admin dashboard that runs a health check and writes /status/up or /status/down into the page. In a README, write the status as text or link to the real status page. A green dot that's always green is worse than no dot.

Two small things: alt text and search

The banner's text is inside an image, so it isn't text on the page. Screen readers read the alt attribute instead (alt="My Project" in the examples above), and the words inside the SVG aren't part of the README's text on the page. Give the image a real alt, and keep the project name as a normal Markdown heading or first line too.

The script

This is the core of what I ran: render a snippet with GitHub's Markdown API, then fetch every image URL in the HTML and print the headers that matter. GitHub's API allows a limited number of requests without a token, which was plenty for five snippets.

import json, re, subprocess, urllib.request

def gh(md):
    req = urllib.request.Request(
        'https://api.github.com/markdown',
        data=json.dumps({'text': md, 'mode': 'gfm'}).encode(),
        headers={'Accept': 'application/vnd.github+json', 'User-Agent': 'readme-banner-test'})
    return urllib.request.urlopen(req, timeout=20).read().decode()

def headers(url):
    # a browser-like user agent: some image hosts answer bare curl with a bot check
    out = subprocess.run(['curl', '-s', '-o', '/dev/null', '-D', '-', '-A', 'Mozilla/5.0 Chrome/156.0', url],
                         capture_output=True, text=True).stdout
    keep = r'(?i)(HTTP/|content-type|cache-control|content-security-policy|content-length)'
    return [l.strip() for l in out.splitlines() if re.match(keep, l)]

snippet = '![My Project](https://emojifavicons.com/readme/rocket?title=My+Project)'
html = gh(snippet)
print(html)
for url in sorted(set(re.findall(r'(?:src|srcset)="([^"]+)"', html))):
    print('fetch', url[:80], '...')
    for line in headers(url.replace('&amp;', '&')):
        print('   ', line)
Enter fullscreen mode Exit fullscreen mode

Swap in your own snippet to see what GitHub does with it before you push.

What I didn't test

  • The github.com page itself. I used GitHub's Markdown API and added GitHub's one image rule (max-width: 100%) by hand. GitHub's page has more CSS and scripts than that, and its mobile app may behave differently.
  • Other browsers. Only Chrome 156. Firefox and Safari support <picture> and prefers-color-scheme too, but I didn't run them.
  • Real system fonts. The five fonts were stand-ins. Segoe UI and San Francisco weren't on my test machine.
  • Other places that show READMEs, like package registries and other code hosts. They have their own rules for external images.

Checklist

  1. One line of Markdown is enough: ![Name](https://emojifavicons.com/readme/<emoji>?title=...). Encode the title (& becomes %26).
  2. Open the URL once before committing. An unknown emoji name is a 404 and a broken image.
  3. Use <picture> for dark and light with theme=dark and theme=light.
  4. Keep the title to about 29 characters or fewer, and the subtitle short. On a phone, both are shown at less than half size.
  5. Expect up to an hour of caching after you change the banner URL.
  6. Don't use status icons as live status in a README. The URL picks the colour, not your API.
  7. Write a real alt and keep the project name as text too.
  8. Launching soon? If you list the project somewhere people look for new tools, such as main.app, the listing often links straight to the repo, so the README is the first page people see.

The full list of banner and icon options, including the theme values, is in the EmojiFavicons docs.


Need help with a project? The team behind FindUtils takes on freelance work on Upwork, including a Laravel Livewire app or admin panel with dynamic pages and forms, for example a status dashboard whose icons come from a real health check.

Top comments (0)