How To Embed Youtube In Nuxt Website Via Markdown File
Embedding Youtube in a blog build with Nuxt has never been easier
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:
- Include the Youtube Lite package
- Add plugin
- Create Component
- Use component in the Markdown file
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.
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.
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.
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.
Thanks!
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 ๐