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, thought it also support 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!
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
telemetry disable step if you are OK with sending data to Astro.
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 😅.
Essentially, as we just mentioned, you now just need to import the component in a script tag, then
mux-video element in the Astro markup. Here is a pared back version:
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.
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
.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.
We have taken 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,
- 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 customisations
Take 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.
- 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 you 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.
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.
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.