Remix adding resources with the link tag

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

The link tag is a way to add specific resources to your webpage, the most commonly known one is stylesheets.
We can load them in plain HTML by using the link tag like this:
<link rel="stylesheet" href="assets/styles.css" />
Another use-case is to load the site's icon like the favicon with this link element.
And last but not least, it's not limited to this. It can be used to preload some assets!
Yes, the link API is pretty sick and powerful.
If you followed along with my articles on Remix, you used one of their fantastic starter where the root.tsx file already uses the links element.
In Remix, you can add links to an array like this:
import tailwindStylesheetUrl from './styles/tailwind.css';
export const links: LinksFunction = () => {
return [{ rel: 'stylesheet', href: tailwindStylesheetUrl }];
};
This LinksFunctions can be invoked from every route file you create, so you can also inject very specific stylesheets on different pages.
Inside our render we then simply have to inject a property like this:
export default function App() {
return (
<html>
<head>
...
<Links />
</head>
...
);
}
If you import this in your root, you don't specifically have to import it on other pages.
A different single page has some charts that need the CSS stylesheet.
This page can be about.tsx, for instance. If we open that up, we can add a links function there like this:
import chartStyles from '../styles/charts.css';
export const links: LinksFunction = () => {
return [
{
rel: 'stylesheet',
href: chartStyles,
},
];
};
And now, if we open the about page, we can see our original tailwind stylesheet and this chart stylesheet injected.
As mentioned, we have the option to add quite a few different types of links. Let's take a look at some examples.
First up, a favicon. The icon uses in your web browser.
export const links: LinksFunction = () => {
return [
{
rel: 'icon',
href: '/favicon.png',
type: 'image/png',
},
];
};
Then we already saw we could add a local stylesheet. The good thing to note is the import we use. This import is essential to note as it will make sure Remix will fingerprint this file for production caching. (Making it a unique URL)
import chartStyles from '../styles/charts.css';
export const links: LinksFunction = () => {
return [
{
rel: 'stylesheet',
href: chartStyles,
},
];
};
We can also load external links by simply passing the full URL to the href property.
export const links: LinksFunction = () => {
return [
{
rel: 'stylesheet',
href: 'https://example.com/some/styles.css',
crossOrigin: 'true',
},
];
};
As mentioned, the links API is very powerful, and it can even be used to prefetch images, for instance.
export const links: LinksFunction = () => {
return [
{
rel: 'prefetch',
as: 'image',
href: '/img/bunny.jpg',
},
];
};
The cool part about prefetching is that you can specify media queries for this prefetch.
LEt's say we only want to prefetch this image on bigger screens.
export const links: LinksFunction = () => {
return [
{
rel: 'prefetch',
as: 'image',
href: '/img/bunny.jpg',
media: '(min-width: 1000px)',
},
];
};
We can also use the above media technique to add stylesheets only on certain queries:
export const links: LinksFunction = () => {
return [
{
rel: 'stylesheet',
href: printStyles,
media: 'print',
},
{
rel: 'stylesheet',
href: highResStyles,
media: 'screen and (min-resolution: 300dpi)',
},
];
};
And that's it. The links element in Remix is super powerful, as it's built on the native links API. I love that Remix takes such a promising premise of using what is already there and enhancing the browser capabilities.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter