Skip to content

agility/agility-cms-app-youtube

Repository files navigation

Agility CMS YouTube Video Field App

A custom field application for Agility CMS that enables content editors to easily embed and manage YouTube videos directly within their content. Built with the Agility Apps SDK v2, this app provides a seamless experience for adding YouTube videos with automatic metadata fetching and live previews.

YouTube App Next.js TypeScript Tailwind CSS

πŸŽ₯ Features

URL Input & Validation

  • Simple paste-and-go interface for YouTube URLs
  • Supports multiple URL formats:
    • https://www.youtube.com/watch?v=dQw4w9WgXcQ
    • https://youtu.be/dQw4w9WgXcQ
    • https://youtube.com/embed/dQw4w9WgXcQ
    • https://m.youtube.com/watch?v=dQw4w9WgXcQ
  • Automatic video ID extraction and validation

Automatic Metadata Fetching

  • Uses YouTube's public oEmbed API to retrieve:
    • Video title
    • Thumbnail image
    • Video dimensions (width/height)
    • Author information (channel name and URL)
    • Provider information
  • No API keys required for public videos

Live Video Preview

  • Embedded YouTube player using YouTube's standard embed iframe
  • Full playback controls
  • Responsive player that adapts to container size
  • Support for all YouTube player features

Responsive Design

  • Desktop (β‰₯600px): Side-by-side layout with video and details
  • Mobile (<600px): Stacked layout for optimal mobile viewing
  • Automatically adjusts iframe height using useResizeHeight()

Complete Data Storage

  • Stores full video metadata as structured JSON
  • Easy access to video information in templates and API responses
  • Persistent storage across content edits

πŸš€ Quick Start

Installation

  1. Clone the repository

    git clone https://github.com/agility/agility-cms-app-youtube.git
    cd agility-cms-app-youtube
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Run the development server

    npm run dev
    # or
    yarn dev
  4. Open in browser Navigate to http://localhost:3001

Testing the Field

  1. Navigate to /fields/youtube-field in your browser
  2. Paste a YouTube URL (try: https://www.youtube.com/watch?v=dQw4w9WgXcQ)
  3. Click "Fetch" to load the video
  4. Preview the video and see the metadata

πŸ“ Project Structure

app/
β”œβ”€β”€ fields/
β”‚   └── youtube-field/
β”‚       └── page.tsx              # Main YouTube field component
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ app-uninstall/
β”‚   β”‚   └── route.ts              # Uninstall webhook handler
β”‚   └── hello/
β”‚       └── route.ts              # Health check endpoint
β”œβ”€β”€ layout.tsx                    # Root layout with permissions policy
└── page.tsx                      # Home page with app documentation

components/
└── CommonDashboard.tsx           # Shared dashboard component

public/
└── .well-known/
    └── agility-app.json          # App definition file

styles/
└── globals.css                   # Global styles with Tailwind

πŸ”§ Technical Implementation

Core Dependencies

{
	"@agility/app-sdk": "^2.1.0",
	"next": "16.0.0",
	"react": "19.2.0",
	"typescript": "^5.5.4"
}

Key Hooks & Methods Used

useAgilityAppSDK()

Access SDK functionality and field data:

const { initializing, fieldValue } = useAgilityAppSDK()

useResizeHeight()

Automatically adjust iframe height:

const containerRef = useResizeHeight(10)
return <div ref={containerRef}>...</div>

contentItemMethods.setFieldValue()

Save video data to the field:

contentItemMethods.setFieldValue({
	value: JSON.stringify(videoData)
})

Data Structure

The field stores video data as JSON:

interface YouTubeVideoData {
	url: string
	title: string
	author_name: string
	author_url: string
	type: string
	height: number
	width: number
	version: string
	provider_name: string
	provider_url: string
	thumbnail_height: number
	thumbnail_width: number
	thumbnail_url: string
	html: string
	video_id: string
}

YouTube Player Integration

// Using the YouTube Player API
const loadYouTubePlayer = (videoId: string) => {
	// Load the YouTube Player API
	const tag = document.createElement("script")
	tag.src = "https://www.youtube.com/iframe_api"
	document.head.appendChild(tag)

	// Initialize player when API is ready
	window.onYouTubeIframeAPIReady = () => {
		new window.YT.Player("player", {
			videoId: videoId,
			width: 640,
			height: 360,
			events: {
				onReady: (event) => {
					console.log("Player is ready")
				}
			}
		})
	}
}

πŸ” Permissions Policy

The app includes proper Permissions Policy headers to avoid browser console warnings:

// next.config.js
headers: [
	{
		key: "Permissions-Policy",
		value: "autoplay=*, encrypted-media=*, fullscreen=*, clipboard-write=*, web-share=*"
	}
]

Permissions enabled:

  • autoplay: Allow video autoplay
  • encrypted-media: Support DRM content
  • fullscreen: Enable fullscreen video
  • clipboard-write: Allow copying URLs
  • web-share: Enable sharing features

App Definition

The app definition is served at /.well-known/agility-app.json and includes:

{
	"name": "YouTube Video Field",
	"version": "1.0.0",
	"description": "Embed and manage YouTube videos",
	"surfaces": {
		"fields": [
			{
				"name": "youtube-field",
				"displayName": "YouTube Video",
				"path": "/fields/youtube-field"
			}
		]
	}
}

πŸ› Troubleshooting

Player Not Loading

Check the browser console for errors:

  • "Invalid YouTube URL": URL format is incorrect
  • "Failed to fetch video data": Video may be private or network issue
  • "Unable to load video player": Player initialization failed

Console Warnings

Permissions policy warnings have been resolved. If you still see them:

  1. Clear browser cache
  2. Restart development server
  3. Check next.config.js headers configuration

Testing Public Videos

Use these URLs for testing:

  • https://www.youtube.com/watch?v=dQw4w9WgXcQ
  • https://youtu.be/jNQXAC9IVRw

See YOUTUBE_FIELD_USAGE.md for detailed debugging information.

πŸ“š Documentation

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments


Made with ❀️ for the Agility CMS community

About

A custom field app for YouTube videos

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published