Overview
ThePrivacyEmbed component is a reusable wrapper that implements the “facade pattern” for embedding third-party content. It displays a poster image with a load button instead of immediately loading external iframes, preventing cookies and tracking until the user explicitly consents.
This component is used internally by
YouTubePlayer but can be used directly to wrap any external embeddable content.Props
The iframe URL to load when the user clicks the load button. For YouTube URLs, automatically converts to
youtube-nocookie.com.Descriptive title for the embedded content, used for accessibility labels.
Optional image URL to display before loading. Shows as a background image behind the load button.
Additional CSS classes to apply to the container element for custom styling.
Features
Automatic YouTube Privacy Enhancement
The component automatically detects and converts YouTube URLs:Zero-Cookie Initial Load
Benefits:- No cookies set on page load
- No third-party scripts executed
- No tracking pixels fired
- Minimal external requests
Interactive Facade
The component displays a clickable facade with:- Optional poster image background
- Play button icon (SVG)
- “Load Content” text label
- Hover effects for interactivity
Usage Examples
YouTube Video
Vimeo Video
Google Maps Embed
Without Poster Image
Component Behavior
Initial State
-
Renders a facade
<div>with:- Poster image (if provided) with opacity effects
- Play button SVG icon
- “Load Content” text
- Cursor pointer for clickability
-
Hidden iframe container stores URL in
data-srcattribute
After Click
- JavaScript event listener detects click on facade
- Reads
data-srcanddata-titlefrom iframe container - Creates
<iframe>element dynamically - Sets iframe attributes:
- Appends iframe to container
- Hides facade with
display: none
Styling
Default Container Classes
privacy-embed: Unique identifier for JavaScript targetinggroup: Tailwind group for hover effects on childrenrelative: Positions facade absolutely withinw-full: Full width of parentoverflow-hidden: Clips rounded cornersbg-gray-900: Dark background before content loads
Facade Styling
The facade includes hover effects:- Play button scales up 110%
- Background becomes more opaque
- Poster image opacity reduces for emphasis
Custom Styling Example
Accessibility
Semantic HTML
- Uses
<button>for the load trigger (keyboard accessible) - Includes descriptive
aria-label - Proper iframe
titleattribute for screen readers
Keyboard Support
The facade button responds to:EnterkeySpacekey- Mouse click
ARIA Labels
Astro Integration
View Transitions Support
The component uses
astro:page-load event listener to reinitialize after Astro view transitions.Privacy & Performance
Privacy Benefits
| Aspect | Without PrivacyEmbed | With PrivacyEmbed |
|---|---|---|
| Cookies on load | Yes (immediate) | No (only after consent) |
| Third-party scripts | Loaded immediately | Loaded on demand |
| User tracking | Automatic | Opt-in only |
| External requests | 10-20+ per embed | 1 (poster image) |
Performance Impact
Savings per embed:- ~500KB-1MB prevented load per iframe
- 10-20 fewer HTTP requests
- Reduced JavaScript execution on page load
- Faster Time to Interactive (TTI)
- Without: ~5MB initial load
- With PrivacyEmbed: ~500KB initial load
Technical Implementation
Data Attributes
The component uses HTML5 data attributes for deferred loading:JavaScript Lifecycle
- Page Load: Event listener attached to all
.privacy-embedelements - User Click: Facade click event fires
- Validation: Check if iframe already exists (prevent double-loading)
- Creation: Dynamically create and configure iframe
- Injection: Append iframe to container
- Cleanup: Hide facade
Prevention of Double-Loading
Advanced Use Cases
Multiple Embeds on Page
Custom Play Button
The SVG play icon can be customized by modifying the component source:Related Components
- YouTubePlayer - Pre-configured for YouTube videos
- SoundCloudPlayer - Audio embeds (uses direct iframe)