DEV Community

Cover image for How To Embed Youtube In Nuxt Website Via Markdown File
Dev By RayRay
Dev By RayRay

Posted on • Originally published at byrayray.dev on

How To Embed Youtube In Nuxt Website Via Markdown File

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

Enter fullscreen mode Exit fullscreen mode

Or

npm i lite-youtube-embed

Enter fullscreen mode Exit fullscreen mode

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

https://gist.github.com/devbyray/ba094f0b488ac5152f00b5efd36d4dff

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.

https://gist.github.com/devbyray/6ee03592419349b6a664fc2536a5620e

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

https://gist.github.com/devbyray/3e8ab7649b7d1cbee387c8bba4019733

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.

https://gist.github.com/devbyray/7667b330b483c40b28f2bf9eb5a8b7cb

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.

https://gist.github.com/devbyray/3e2085fcec376d81a022b8552d07e0d6

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.pngAfter 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 😁

Top comments (1)

Collapse
 
dawadam profile image
Dawadam

Thanks for this good trick. ;)