---
title: "YouTube Player · Nuxt Scripts"
description: "Show performance-optimized YouTube videos in your Nuxt app."
canonical_url: "https://scripts.nuxt.com/scripts/v0/content/youtube-player"
meta:
  "og:description": "Show performance-optimized YouTube videos in your Nuxt app."
  "og:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_YouTube%2BPlayer%2Cdescription_Show%2Bperformance-optimized%2BYouTube%2Bvideos%2Bin%2Byour%2BNuxt%2Bapp.%2Cheadline_Content%2Cp_Ii9zY3JpcHRzL3YwL2NvbnRlbnQveW91dHViZS1wbGF5ZXIi%2Cs_yuoRiu_Lxj0yK_Hm.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "YouTube Player · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_YouTube%2BPlayer%2Cdescription_Show%2Bperformance-optimized%2BYouTube%2Bvideos%2Bin%2Byour%2BNuxt%2Bapp.%2Cheadline_Content%2Cp_Ii9zY3JpcHRzL3YwL2NvbnRlbnQveW91dHViZS1wbGF5ZXIi%2Cs_yuoRiu_Lxj0yK_Hm.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Content**

# **YouTube Player**

[Copy for LLMs](https://scripts.nuxt.com/scripts/v0/content/youtube-player.md)

[**YouTube**](https://youtube.com/) is a video hosting platform that allows you to upload and share videos.

Nuxt Scripts provides a `**useScriptYouTubePlayer**` composable and a headless `**ScriptYouTubePlayer**` component to interact with the YouTube Player.

## Types

To use YouTube with full TypeScript support, you will need to install the `**@types/youtube**` dependency.

```bash
pnpm add -D @types/youtube
```

## ScriptYouTubePlayer

The `**ScriptYouTubePlayer**` component is a wrapper around the `**useScriptYouTubePlayer**` composable. It provides a simple way to embed YouTube videos in your Nuxt app.

It's optimized for performance by leveraging the [**Element Event Triggers**](https://scripts.nuxt.com/docs/v0/guides/script-triggers#element-event-triggers), only loading the YouTube Player when the specific elements events happen.

By default, it will load on the `**mousedown**` event.

### Demo

![Play video](https://i.ytimg.com/vi_webp/d_IFKP1Ofq0/hq720.webp)

**Click to load**

Clicking the video will load the YouTube iframe and start the video.

### Props

The `**ScriptYouTubePlayer**` component accepts the following props:

- `**trigger**`: The trigger event to load the YouTube Player. Default is `**mousedown**`. See [**Element Event Triggers**](https://scripts.nuxt.com/docs/v0/guides/script-triggers#element-event-triggers) for more information.
- `**placeholderAttrs**`: The attributes for the placeholder image. Default is `**{ loading: 'lazy' }**`.
- `**aboveTheFold**`: Optimizes the placeholder image for above-the-fold content. Default is `**false**`.

All script options from the [**YouTube IFrame Player API**](https://developers.google.com/youtube/iframe_api_reference) are supported on the `**playerVars**` prop, please consult the [**Supported paramters**](https://developers.google.com/youtube/player_parameters#Parameters) for full documentation.

```ts
export interface YouTubeProps {
  // YouTube Player
  videoId: string
  playerVars?: YT.PlayerVars
  width?: number
  height?: number
}
```

### Privacy

The `**<YoutubePlayer>**` component is privacy-friendly by default and sets the video host to `**https://www.youtube-nocookie.com**`.

To modify this behavior, you can set the `**host**` prop to `**https://www.youtube.com**`.

```vue
<ScriptYouTubePlayer video-id="d_IFKP1Ofq0" :player-options="{ host: 'https://www.youtube.com' }" />
```

### Placeholder

The YouTube Player placeholder image is 1280x720 webp that is lazy-loaded by default.

To modify the placeholder size you can set the `**thumbnailSize**` prop, if you'd prefer to use a `**jpg**` you can pass the `**webp**` prop as `**false**`.

```vue
<ScriptYouTubePlayer video-id="d_IFKP1Ofq0" thumbnail-size="maxresdefault" />
```

If you need fine control over the placeholder you can set `**placeholderAttrs**` prop or completely override it using the `**#placeholder**` slot.

#### **Eager Loading**

You should change this behavior if your video is above the fold or consider using the `**#placeholder**` slot to customize the placeholder image.

```vue
<ScriptYouTubePlayer above-the-fold />
```

### Component API

See the [**Facade Component API**](https://scripts.nuxt.com/docs/v0/guides/facade-components#facade-components-api) for full props, events, and slots.

### Events

The `**ScriptYouTubePlayer**` component emits all events from the YouTube Player SDK. Please consult the [**Player Events**](https://developers.google.com/youtube/iframe_api_reference#Events) for full documentation.

```ts
const emits = defineEmits<{
  'ready': [e: YT.PlayerEvent]
  'state-change': [e: YT.OnStateChangeEvent, target: YT.Player]
  'playback-quality-change': [e: YT.OnPlaybackQualityChangeEvent, target: YT.Player]
  'playback-rate-change': [e: YT.OnPlaybackRateChangeEvent, target: YT.Player]
  'error': [e: YT.OnErrorEvent, target: YT.Player]
}>()
```

### Slots

As the component is provided headless, there are a number of slots for you to customize the player however you like before it's loaded in.

**default**

The default slot is used to display content that will always be visible.

```vue
<template>
  <ScriptYouTubePlayer video-id="d_IFKP1Ofq0">
    <div class="bg-blue-500 text-white p-5">
      Video by Nuxt
    </div>
  </ScriptYouTubePlayer>
</template>
```

**awaitingLoad**

The slot is used to display content while the video is loading.

```vue
<template>
  <ScriptYouTubePlayer video-id="d_IFKP1Ofq0">
    <template #awaitingLoad>
      <div class="bg-blue-500 text-white p-5">
        Click to play!
      </div>
    </template>
  </ScriptYouTubePlayer>
</template>
```

**loading**

The slot is used to display content while the video is loading.

```vue
<template>
  <ScriptYouTubePlayer video-id="d_IFKP1Ofq0">
    <template #loading>
      <div class="bg-blue-500 text-white p-5">
        Loading...
      </div>
    </template>
  </ScriptYouTubePlayer>
</template>
```

**placeholder**

The slot is used to display a placeholder image before the video is loaded. By default, this will show the youtube thumbnail for the video. You can display it however you like.

```vue
<template>
  <ScriptYouTubePlayer video-id="d_IFKP1Ofq0">
    <template #placeholder="{ placeholder }">
      <img :src="placeholder" alt="Video Placeholder">
    </template>
  </ScriptYouTubePlayer>
</template>
```

## useScriptYouTubePlayer

The `**useScriptYouTubePlayer**` composable lets you have fine-grain control over the YouTube Player SDK. It provides a way to load the YouTube Player SDK and interact with it programmatically.

```ts
export function useScriptYouTubePlayer<T extends YouTubePlayerApi>(_options?: YouTubePlayerInput) {}
```

Please follow the [**Registry Scripts**](https://scripts.nuxt.com/docs/v0/guides/registry-scripts) guide to learn more about advanced usage.

### YouTubePlayerApi

```ts
/// <reference types="youtube" />
export interface YouTubePlayerApi {
  YT: typeof YT
}
```

## Example

Loading the YouTube Player SDK and interacting with it programmatically.

```vue
<script setup lang="ts">
const video = ref()
const { onLoaded } = useScriptYouTubePlayer()

const player = ref(null)
onLoaded(async ({ YT }) => {
  // we need to wait for the internal YouTube APIs to be ready
  const YouTube = await YT
  await new Promise<void>((resolve) => {
    if (typeof YT.Player === 'undefined')
      YouTube.ready(resolve)
    else
      resolve()
  })
  // load the API
  player.value = new YT.Player(video.value, {
    videoId: 'd_IFKP1Ofq0'
  })
})
function play() {
  player.value?.playVideo()
}
</script>

<template>
  <div>
    <div ref="video" />
    <button @click="play">
      Play
    </button>
  </div>
</template>
```

[Edit this page](https://github.com/nuxt/scripts/edit/0.x/docs/content/content/youtube-player.md)

[Markdown For LLMs](https://scripts.nuxt.com/scripts/v0/content/youtube-player.md)

[**Vimeo Player** Show performance-optimized Vimeo videos in your Nuxt app.](https://scripts.nuxt.com/scripts/v0/content/vimeo-player) [**Clarity** Use Clarity in your Nuxt app.](https://scripts.nuxt.com/scripts/v0/marketing/clarity)

**On this page **

- [Types](#types)
- [ScriptYouTubePlayer](#scriptyoutubeplayer)
- [useScriptYouTubePlayer](#usescriptyoutubeplayer)
- [Example](#example)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/scripts/v0/content/youtube-player#webpage","@type":"WebPage","description":"Show performance-optimized YouTube videos in your Nuxt app.","name":"YouTube Player","url":"https://scripts.nuxt.com/scripts/v0/content/youtube-player","breadcrumb":{"@id":"https://scripts.nuxt.com/scripts/v0/content/youtube-player#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/scripts/v0/content/youtube-player"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/scripts/v0/content/youtube-player#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Scripts","item":"https://scripts.nuxt.com/scripts","position":2},{"@type":"ListItem","name":"V0","item":"https://scripts.nuxt.com/scripts/v0","position":3},{"@type":"ListItem","name":"Content","item":"https://scripts.nuxt.com/scripts/v0/content","position":4},{"@type":"ListItem","name":"YouTube Player","item":"https://scripts.nuxt.com/scripts/v0/content/youtube-player","position":5}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
