🔥 HTML Elements #
In this post on Astro JS Mux Video, we see how you can integrate the Mux HTML components video player into your site. We will also see that you can put a site together quite quickly with Astro. And that is without the need for learning frameworks like React and Svelte. Astro is principally a static site builder, though it also supports server rendered content. It is aligned with the islands architecture philosophy. That is where components on a page can exist and hydrate independently of each other. This is a recipe for fast sites.
Mux is a video streaming service. You upload your video in a single format, and they sweat the details of making it work with user devices, be they Android or iOS mobile, Windows, Linux, or macOS desktop. In fact, you can even have the player stream to Chromecast compatible devices. The Mux Player is a custom element or Web Component. The advantage of using Web Components is that, in theory, you can write your components once and use them in any web framework or with none at all, just like we will today!
There is minimal setup, and you really can build out an Astro site using the Mux Custom Elements video player in a matter of minutes. So let’s punch it!
🧱 Getting Started with Mux Video Player #
You do not need a Mux account if you want to follow along. As an aside, however, we see how to set one up in the Svelte Video Blog Tutorial . To play a Mux video, you just need a Mux playback ID for the video. We have a Mux promotional asset ID which we can use here for this purpose.
If you are following along, let’s start by spinning up a new Astro project from the Terminal:
At the time of writing, the Mux player is still in beta testing. Taking that into account, you should probably avoid using this setup for production projects.
In the block above we set up a new Astro project, disabled Astro telemetry (which is enabled by
default) and installed the @mux/mux-video
package in our project. Ignore
the telemetry disable
step if you are OK with sending data to Astro.
📀 Video Component #
The Mux Player is a Web Component. All we need to do to make it work with Astro is export the NPM package in a JavaScript (or TypeScript) file. The final step, to make sure it gets bundled with our site, is then to add a link to this file in a script tag. We will do that on our home page, which we will look at next.
Here’s the code for the Video Component. Make a src/components
folder and create a mux-video.ts
file in there:
Hope that wasn’t too much typing 😅.
📼 Home Page #
Essentially, as we just mentioned, you now just need to import the component in a script tag, then
add a mux-video
element in the Astro markup. Here is a pared back version:
In line 9
we make the import mentioned. The script
tag in line 10
, meanwhile, adds Chromecast support — it’s that simple! The player also supports Airplay (used by Apple devices). We use
minimal attributes on the mux-video
element. You see, this web component
does not look too different to a regular HTML component. In fact, we will see below, we can add the
usual attributes we have on an HTML5 video element. We use a Mux promotional video for the playback-id
here. When using your own video, You’ll notice Mux generate two IDs. It is the public playback-id rather than the master-access ID which you will need here.
💫 Levelling Up #
In the markup below, we take it up a level, adding attributes to reduce layout shift and video controls as well as some styling. We learn more about reducing cumulative shift and lazy-loading video in the Svelte Video blog tutorial. We use SvelteKit there, though Astro works with Svelte components.
src/pages/index.astro
— click to expand code.
I added self-hosted fonts here. For this to work, you need to download them from google-webfonts-helper , then extract the zip and place the .woff
and .woff2
files in a new public/fonts
folder in your project.
There is a live demo if you want to see the full thing in action.
🗳 Poll #
🙌🏽 Astro JS Mux Video: Wrapping Up #
We have had a look at the Mux custom elements video player in this post. In particular, we saw:
- how to add the mux-video to an Astro site;
- linking your Mux video assets in the mux-video player; and
- that Astro can work without a framework like React or Svelte.
We have just scratched the surface of what we can do with the Mux custom elements player in this
Astro JS Mux video post. The video works fine on this simple page, where it is the main content.
However, for video lower down the page, you might consider using a Svelte or React component.
Svelte, especially, can make it easy to add lazy loading. This is important for making your page
load faster. If you prefer to stay frameworkless, many modern browsers have lazy loading support and you can access this adding a loading="lazy"
attribute. Also,
check out Mux docs for more customizations .
Have a look at the full project code on the Rodney Lab GitHub page . I hope you found this article useful and am keen to hear how you will the starter on your own projects as well as possible improvements.
🏁 Astro JS Mux Video: Summary #
Does Astro work with custom elements or Web Components? #
- Astro works just fine with Web Components. We saw an example where we worked with the mux-video HTML custom element. Here, we created a special JavaScript component file for the player. We only needed one line of code in this file. That line just exported the mux-video custom element package, which we had already added as an NPM package in our project. To ensure Astro bundled the package, we imported the component file in a script tag in a `.astro` page file.
Do you have to use React, Svelte or Vue with Astro? #
- Astro ships zero JavaScript by default. We have seen, though, that by using a script tag, we can import Web Components or custom elements and bundle those with our page. This works fine, though to take full advantage of the Astro Islands paradigm it is worth considering adding Svelte or React. This can make it easy to get control over lazy loading and also finer tuned partial hydration. Lazy loading is only loading components just before the users scrolls them into view. That is, instead of immediately on page load. Lazy loading brings the advantage that we can optimize loading the part of the page which the visitor will focus on initially. This improves user experience and should be reflected in Lighthouse or Core Web Vitals metrics.
How can you add video streaming to an Astro JS project? #
- An easy way to add video streaming to your site is to use the Mux service with the mux-video HTML component. You just upload a single version of your video (no need to create copies in different formats for different devices, Mux do that for you). Using a service like Mux (over social video sharing platforms) lets you control if ads appear amid your content. On top, you gain control to stop other, potentially unrelated content player once your clip ends. Finally, complying with local data privacy laws can become less of a headache.
🙏🏽 Astro JS Mux Video: Feedback #
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Just dropped a new post taking you through how you can add
— Rodney (@askRodney) August 10, 2022
@MuxHQ video to your 🚀 Astro site using their new HTML components player and no framework.
Even adds Chromecast support 😲
Hope you find it useful!https://t.co/VxO2x7UFdz
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as SvelteKit. Also, subscribe to the newsletter to keep up-to-date with our latest projects.