How To Embed Youtube In Nuxt Website Via Markdown File

Embedding Youtube in a blog build with Nuxt has never been easier

Dev By RayRay
ยทSep 20, 2022ยท

3 min read

How To Embed Youtube In Nuxt Website Via Markdown File

Photo by AltumCode on Unsplash

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • 1. Include Youtube Lite package
  • 2. Add plugin
  • 3. Create Component
  • 4. Use component in Markdown file
  • Thanks!

Embedding Youtube in a blog build with Nuxt has never been easier. Include the lite-youtube-embed package, create a Nuxt plugin and component, and you can use it in all your Markdown content files.

In this post, I will show you how you can do it yourself. We follow the following steps:

  1. Include the Youtube Lite package
  2. Add plugin
  3. Create Component
  4. Use component in the Markdown file

divider-byrayray.png

1. Include Youtube Lite package

First, we need to install the npm package lite-youtube-embed, created by Google Chrome team member and Frontend Developer Paul Irish ๐Ÿ’ช. This package is built for embedding Youtube videos with better performance than the "normal" way.

But I don't want to dive into the performance bottlenecks of embedding Youtube players on your blog. I want to show you how you can implement a Youtube player in your Markdown files with Nuxt.

Run this command to install it in your Nuxt project.

yarn add lite-youtube-embed

Or

npm i lite-youtube-embed

Add the CSS file in your nuxt.config.js into the css property.

divider-byrayray.png

2. Add plugin

Create a plugin file youtube.client.js into your plugins folder. Make sure you include .client.js in the name, so Nuxt will only load this in the browser. We also need to import the JavaScript library, of course.

Now you need to register this plugin in your nuxt.config.js like below.

divider-byrayray.png

3. Create Component

To use the <lite-youtube> web component in your Markdown files, you must create a Vue component to wrap it. Otherwise, you won't be able to show it on the page.

I created a Youtube.vue component in the components folder. I added two props to pass the Youtube video ID and a label.

With this Youtube component, you can start using it in your Markdown files.

divider-byrayray.png

4. Use component in Markdown file

So the question is, how to use it in your Markdown files?

Add it into your files like you usually would do with your Vue components.

Nuxt will magically turn your whole Markdown file into a page of HTML, which you can see in the browser like this.

Screenshot Youtube Video player embed

divider-byrayray.png

Thanks!

hashnode-footer.png After reading this story, I hope you learned something new or are inspired to create something new! ๐Ÿค— If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode.

Did you know that you can create a Developer blog like this one, yourself? It's entirely for free. ๐Ÿ‘๐Ÿ’ฐ๐ŸŽ‰๐Ÿฅณ๐Ÿ”ฅ

If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a DM on Twitter @DevByRayRay when you want to keep it private. My DM's are always open ๐Ÿ˜

Did you find this article valuable?

Support Dev By RayRay by becoming a sponsor. Any amount is appreciated!

See recent sponsors |ย Learn more about Hashnode Sponsors
ย 
Share this