Directory
- Welcome
YouTube Component
The YouTube component provides optimized video embedding with automatic thumbnail loading, custom play buttons, and performance optimizations. Choose between two implementation options to best suit your needs.
Key Features
-
Performance Optimized: Lazy-loaded iframes and smart resource preloading
-
SEO-Friendly: Includes structured data and semantic markup
-
Customizable: Supports custom thumbnails and player parameters
Implementation Options
<YouTube src="dQw4w9WgXcQ" title="Never Gonna Give You Up" thumbnailQuality="high" />
The YouTube
component offers:
- Custom play button styling
- Structured data support
- Simplified prop-based configuration
- Enhanced thumbnail options
<lite-youtube videoid="dQw4w9WgXcQ" playlabel="Play: Never Gonna Give You Up"></lite-youtube>
The lite-youtube
component provides:
- Optimized for YouTube Shorts
- Extremely lightweight
- Mobile-first approach
To read more about the lite-youtube
component, please visit the lite-youtube documentation.
Component API
YouTube Component Props
Prop | Type | Default | Description |
---|---|---|---|
src | string | YouTube video ID or URL | |
buttonProps | ComponentProps<typeof Button> | Custom play button properties | |
description | string | Video description for structured data | |
params | string | Additional YouTube player parameters | |
structuredData | boolean | true | Enable/disable Schema.org markup |
title | string | Video title for accessibility and overlay | |
thumbnailProps | ComponentProps<typeof Image> | Custom thumbnail image properties (src can be undefined and will be generated from the video ID) | |
thumbnailQuality | "max" | "high" | "default" | "low" | Thumbnail image quality (for generated thumbnails) | |
uploadDate | Date | Video upload date for structured data |
Functions
The component exposes the following function for you to use:
play(): Promise<void>;
Usage Examples
Basic Implementation
<YouTube src="dQw4w9WgXcQ" />
Advanced Configuration
<YouTube src="dQw4w9WgXcQ" title="Never Gonna Give You Up" description="Rick Astley's iconic music video" thumbnailQuality="max" params="start=30&autoplay=1" buttonProps={{ class: "custom-play-button", variant: "secondary" }}/>
With Structured Data
<YouTube src="dQw4w9WgXcQ" title="Never Gonna Give You Up" description="Rick Astley's iconic music video" uploadDate={new Date('1987-07-27')} structuredData={true}/>
Performance Optimizations
The YouTube component implements several performance optimizations:
-
Smart Resource Loading
- Defers iframe creation until play
- Preconnects to required domains
- Lazy loads high-quality thumbnails
-
Reduced Network Impact
- Uses youtube-nocookie.com domain
- Optimizes API loading
- Minimizes initial page load
-
Progressive Enhancement
- Starts with static thumbnail
- Upgrades to WebP when available
- Gracefully falls back when needed
Best Practices
-
Video Loading
- Use appropriate thumbnail quality
- Consider mobile data usage
- Implement lazy loading for off-screen videos
-
Accessibility
- Always provide descriptive titles
- Include meaningful play labels
- Ensure keyboard navigation support
-
Performance
- Limit videos per page
- Use thumbnail quality appropriately
- Consider mobile viewers
Limited Time Launch Sale
Start embedding YouTube videos with optimal performance today. Get our AstroJS starter template and access the YouTube component along with many other pre-built features.
GET 60% OFF!