Scripts

X (formerly Twitter) is a social media platform for sharing posts.

Nuxt Scripts provides a <ScriptXEmbed> component that fetches tweet data server-side and exposes it via slots for complete styling control. All data is proxied through your server - no client-side API calls to X.

<ScriptXEmbed>

The <ScriptXEmbed> component is a headless component that:

  • Fetches tweet data server-side via the X syndication API
  • Proxies all images through your server for privacy
  • Exposes tweet data via scoped slots for custom rendering
  • Caches responses for 10 minutes

Demo

Matthew Prince 🌥
Matthew Prince 🌥

@eastdakota

I'm told there are some who read my music choices while writing @Cloudflare's quarterly earnings for hints. Unfortunately, this time I'm sitting in a New York hotel lobby and don't have control over the music. But I do have a crackling real-wood fire — which is pretty awesome!

2:47 AM · Feb 5, 202413 replies126 likes

Props

The <ScriptXEmbed> component accepts the following props:

PropTypeDefaultDescription
tweetIdstringRequiredThe ID of the tweet to embed
apiEndpointstring/api/_scripts/x-embedCustom API endpoint for fetching tweet data
imageProxyEndpointstring/api/_scripts/x-embed-imageCustom endpoint for proxying images
rootAttrsHTMLAttributes{}Root element attributes

Slot Props

The default slot receives the following props:

interface SlotProps {
  // Raw data
  tweet: XEmbedTweetData
  // User info
  userName: string
  userHandle: string
  userAvatar: string // Proxied URL
  userAvatarOriginal: string // Original X URL
  isVerified: boolean
  // Tweet content
  text: string
  // Formatted values
  datetime: string // "12:47 PM · Feb 5, 2024"
  createdAt: Date
  likes: number
  likesFormatted: string // "1.2K"
  replies: number
  repliesFormatted: string // "234"
  // Media
  photos?: Array<{
    url: string
    proxiedUrl: string
    width: number
    height: number
  }>
  video?: {
    poster: string
    posterProxied: string
    variants: Array<{ type: string; src: string }>
  }
  // Links
  tweetUrl: string
  userUrl: string
  // Quote tweet
  quotedTweet?: XEmbedTweetData
  // Reply context
  isReply: boolean
  replyToUser?: string
  // Helpers
  proxyImage: (url: string) => string
}

Named Slots

SlotDescription
defaultMain content with slot props
loadingShown while fetching tweet data
errorShown if tweet fetch fails, receives { error }

How It Works

  1. Server-side fetch: Tweet data is fetched from cdn.syndication.twimg.com during SSR
  2. Image proxying: All images are rewritten to proxy through /api/_scripts/x-embed-image
  3. Caching: Responses are cached for 10 minutes at the server level
  4. No client-side API calls: The user's browser never contacts X directly

This approach is inspired by Cloudflare Zaraz's embed implementation.

Privacy Benefits

  • No third-party JavaScript loaded
  • No cookies set by X
  • No direct browser-to-X communication
  • User IP addresses not shared with X
  • All content served from your domain