The Hard Problem of Rendering Tweets


0 reactions 2022-06-12

I’ve been unhappy with my tweet rendering strategy for a while - Twitter encourages you to use their heavy JS script to render tweets, which undoubtedly heaps all sorts of tracking on the reader, docks your lighthouse performance score by ~17 points, adds ~4 seconds to Time to Interactive, occasionally gets adblocked (so nothing renders!)

perf impact screenshots



The solution, of course, is to render it yourself, hopefully on the server side.

Solution up front

You can see my Svelte REPL solution here and paste in your own data generated from any curl request:

curl ",author_id,conversation_id,created_at,geo,id,in_reply_to_user_id,lang,public_metrics,referenced_tweets,text,withheld&expansions=attachments.media_keys,attachments.poll_ids,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,,,duration_ms,height,media_key,preview_image_url,public_metrics,type,url,variants,width&poll.fields=duration_minutes,end_datetime,id,options,voting_status&user.fields=created_at,description,id,name,profile_image_url,username" -H "Authorization: Bearer $BEARER_TOKEN" | pbcopy

Use to construct your curl query; you’ll also need a $BEARER_TOKEN from a twitter developers app you have to set up separately.

The problem

However, a Tweet isn’t just a simple data object. Tweets can have polls, images, videos, quote tweets, likes, retweets, quote tweets, mentions, hashtags, threads/conversations, and on and on. This is a lot of product complexity to model and display correctly.

The Next.js team have put together a nextjs component with some nifty AST parsing and serverside cheerio automation: but even here I found that it doesnt correctly handle video tweets and omits displaying retweets.


(use to construct your curl query

Basic display

I started out modeling the component in the Sveltejs REPL:


Part of this was just a mix of grabbing some relevant Nextjs code, but then making different design choices like taking Twitter’s actual SVG icons and displaying retweets. This was brain numbing and took a couple hours but wasn’t too hard.

More work can be done to add polls, images and videos but I chose to skip that for my basic implementation.

Tweet Body parsing

The text parsing became the tricky part.

Simple text like @swyx on “learning in public” Have a listen: rendered on Twitter is enriched into

<div lang="en" dir="auto" class="css-901oao r-1nao33i r-37j5jr r-1blvdjr r-16dba41 r-vrz42v r-bcqeeo r-bnwqim r-qvutc0" id="id__9kn1r6pzdhd" data-testid="tweetText"><div class="css-1dbjc4n r-xoduu5"><span class="r-18u37iz"><a dir="ltr" href="/swyx" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1loqt21 r-poiln3 r-bcqeeo r-qvutc0" style="color: rgb(29, 155, 240);">@swyx</a></span></div><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> on “learning in public”
Have a listen: </span><a dir="ltr" href="" rel="noopener noreferrer" target="_blank" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-qvutc0" style=""><span aria-hidden="true" class="css-901oao css-16my406 r-poiln3 r-hiw28u r-qvk6io r-bcqeeo r-qvutc0">https://</span></a></div>

This is a very basic part of the twitter experience so I wanted to model it properly.

Here is the research I did on available options:

The test code I used was this:

var twitter = require('twitter-text') //
var Autolinker = require('autolinker')
const text = `
#hello < @world > Joe went to
@swyx on “learning in public”
Have a listen:

var autolinker = new Autolinker( {
    mention: 'twitter',
    hashtag: 'twitter'
} );

var html = text );

which gets you:

<a href="" title="#hello" class="tweet-url hashtag" rel="nofollow">#hello</a> &lt; @<a class="tweet-url username" href="" data-screen-name="world" rel="nofollow">world</a> &gt; Joe went to
@<a class="tweet-url username" href="" data-screen-name="swyx" rel="nofollow">swyx</a> on “learning in public”
Have a listen: <a href="" rel="nofollow"></a> <a href="" rel="nofollow"></a>

<a href="" target="_blank" rel="noopener noreferrer">#hello</a> < <a href="" target="_blank" rel="noopener noreferrer">@world</a> > Joe went to <a href="" target="_blank" rel="noopener noreferrer"></a>
<a href="" target="_blank" rel="noopener noreferrer">@swyx</a> on “learning in public”
Have a listen: <a href="" target="_blank" rel="noopener noreferrer"></a> <a href="" target="_blank" rel="noopener noreferrer"></a>

I also found this small library but it seems to use Twitter’s v1 API response which is useless for modern needs. unfurling - the failed attempt

Twitter’s link shortening is user unfriendly because it adds tracking, latency, and makes the url opaque. (docs, docs)

To unfurl the URL to something more user friendly, we could add an async process to send a ping to the url, and get back the redirect header (you can use the followRedirects in the fetch API). Autolinker does not seem to support this or an async replacement function.

You can read Loige’s blogpost: for the basic intuition and use his tall library:

import { tall } from 'tall'

  .then(unshortenedUrl => console.log('Tall url', unshortenedUrl))
  .catch(err => console.error('AAAW 👻', err))

There is also a url-unshort library to do this with retries and caching:

const uu = require('url-unshort')()

try {
  const url = await uu.expand('')

  if (url) console.log('Original url is: ${url}')
  else console.log('This url can\'t be expanded')

} catch (err) {

I ended up going with tall and postprocessing autolinker:

async function superautolink(text) {
  const urls = []
  var autolinker = new Autolinker( {
      mention: 'twitter',
      hashtag: 'twitter',
      replaceFn : function( match ) {
          if (match.getType() === 'url') {
                  const url = match.getUrl();
                  if (url.startsWith('')) urls.push(url)
         return true
  var html = text );
  for (let url of urls) {
    const unfurl = await tall(url);
    html = html.replaceAll(url, unfurl) // handle links
    html = html.replaceAll(url.slice(8), unfurl) // handle raw link text
  return html

which correctly unshortened the URLs

<a href="" target="_blank" rel="noopener noreferrer">#hello</a> < <a href="" target="_blank" rel="noopener noreferrer">@world</a> > Joe went to <a href="" target="_blank" rel="noopener noreferrer"></a>
<a href="" target="_blank" rel="noopener noreferrer">@swyx</a> on “learning in public”
Have a listen: <a href="" target="_blank" rel="noopener noreferrer"></a> <a href="" target="_blank" rel="noopener noreferrer"></a>

However I found that I needed to run this unshortening in the browser and the tall library requires Node.js’ http module, so back to square one for me. unfurling - the simple way

A discovery I had in testing these unfurls was the sneaky way that Twitter makes it hard for you to unwrap the url. if you fetch(''), you get back <head><noscript><META http-equiv="refresh" content="0;URL="></noscript><title></title></head><script>window.opener = null; location.replace("https:\/\/\/3Qc0MIq")</script> which basically forces an in-place reload rather than using the proper HTTP redirect headers. annoying!

However, assuming Twitter’s redirect response is stable, you can exploit this.

  .then(res => res.text())
  .then(x => x.match("(?<=<title>)(.*?)(?=</title>)")[0]) //
  .then(console.log) //

et voila…


Rendering images

The API forces you to perform a lookup, which isn’t the hardest thing in the world to do. (The CSS is harder)


aside: Nextjs gets the aspect ratio presentation wrong


Then it’s just a matter of getting some test cases:

I wasn’t super confident in my css grid ability so I blended some css grid with JS to represent the different layouts (particularly the 3-image layout):

export let tweet
export let data

let grid = {
	4: `
	"foo-0 foo-1" 100px
	"foo-2 foo-3" 100px
	/ 50% 50%;
	3: `
	"foo-0 foo-1" 100px
	"foo-0 foo-2" 100px
	/ 1fr 1fr;
	2: `
	"foo-0 foo-1" 200px
	/ 50% 50%
	1: `"foo-0" 100% / 100%`,

{#if tweet.attachments}
<div style={`margin-top: 0.5rem; display: grid; grid: ${grid}; gap: 1px; background-color: black; border: 1px solid black; overflow: hidden; border-radius: 5px`}>
	{#each tweet.attachments.media_keys as mediakey, index}
	<img style={`width: 100%; height: 100%; object-fit: cover; grid-area: foo-${index}`} alt="todo" src={ => fullmedia.media_key === mediakey).url} />

Rendering Video

The next thing to do is video. Nextjs doesnt handle it so we’ll have to figure this out.

For a single embedded video, Twitter provides a bunch of different bitrates:

        "preview_image_url": "",
        "type": "video",
        "variants": [
            "content_type": "application/x-mpegURL",
            "url": ""
            "bit_rate": 950000,
            "content_type": "video/mp4",
            "url": ""
            "bit_rate": 632000,
            "content_type": "video/mp4",
            "url": ""
            "bit_rate": 2176000,
            "content_type": "video/mp4",
            "url": ""
        "public_metrics": {
          "view_count": 765
        "width": 720,
        "media_key": "7_1532586917866266625",
        "height": 1280,
        "duration_ms": 90000

I considered offering a custom player, but felt that wasn’t worth it. So I just offer a basic video control:

<video controls src="/static/short.mp4" poster="/static/poster.png" preload="none"> </video>

This was the best blogpost I found on the topic:


Here’s a generic search for all polls:

Polls can have:

It wasn’t too bad:

export let tweet
export let data
const pollid = tweet.attachments.poll_ids[0]
const polldata = data.includes.polls.find(poll => === pollid)
let winningChoice = null
const totalvotes = polldata.options.reduce((a,b) => {
	if (!winningChoice) winningChoice = b
	else if (winningChoice.votes < b.votes) winningChoice = b
	return a + b.votes
}, 0)

<ul style="position: relative; list-style-type: none; padding-left: 0">
{#each polldata.options as option}
{@const percent = option.votes / totalvotes}
	{@const isWinning = winningChoice.position === option.position}
	<li style="position: relative; height: 1rem; margin-top: 1rem">
		<div style={`height: 1.5rem; border-radius: 5px; background-color: ${isWinning ? `rgba(29, 155, 240, 0.58)`: 'rgba(51, 54, 57, 0.3)'}; width: ${percent * 100}%`}></div>
		<div style="width: 100%; position: absolute; top: 0; display: flex; justify-content: space-between; padding-left: 0.25rem">
				<span>{#if isWinning}
					{/if}{Math.round(percent * 1000)/10}%</span>
	<p class="tweettime tweetbrand svelte-145fr9">
{totalvotes} votes ending {new Date(polldata.end_datetime).toDateString()}


Other Twitter features

There are Twitter Lists, Twitter communities, Twitter Spaces, and others, that I don’t handle well, but at least it doesnt look actively horrible:


To handle this well I would have to write an “unfurl” module to unfurl quote tweets and these other features. Can’t be bothered right now.

Get updates on new posts and projects

3000+ subscribers including my Mom – see past issues

Leave a reaction if you liked this post! 🧡