惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
月光博客
月光博客
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
小众软件
小众软件
量子位
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

Netlify Developers

AI model comparison using Netlify's AI Gateway | Netlify Developers Build an AI agent to automatically process and summarize form submissions | Netlify Developers Build an AI agent to automatically generate relevant images for blog posts | Netlify Developers Prerendering SPA content pages to enable AI agent access | Netlify Developers Identify image optimization opportunities with Netlify Observability | Netlify Developers Fix top 404 page not found errors with Netlify Observability | Netlify Developers Introducing AI Gateway: built-in AI model access on Netlify | Netlify Developers Introducing Observability: your project insights on Netlify | Netlify Developers From first publish to first update with AI agents | Netlify Developers How to use Agent Runners on Netlify | Netlify Developers Add forms to your project with AI + Netlify | Netlify Developers Track Real User Metrics | Netlify Developers Lighthouse performance scores on Netlify | Netlify Developers Review your AI project before you ship | Netlify Developers Using environment variables in AI Projects on Netlify | Netlify Developers Build prototypes and MVPs fast with AI + Netlify | Netlify Developers Pause auto-publishing on Netlify | Netlify Developers Run serverless functions, storage, and more natively in Nuxt dev | Netlify Developers Netlify Office Hours: The AX Arcade challenge | Netlify Developers Netlify Office Hours: Prompting with style | Netlify Developers Netlify Office Hours: RAG Apps with OpenAI + Netlify DB | Netlify Developers How to build a RAG application with Neon, Netlify & OpenAI | Netlify Developers Netlify Office Hours: Netlify DB (powered by Neon) | Netlify Developers Netlify Office Hours: Netlify MCP Server | Netlify Developers Netlify Office Hours: Netlify Vite Plugin | Netlify Developers Building MCPs with Netlify | Netlify Developers Deploying sites from your AI tool | Netlify Developers Adding your domain using Netlify API | Netlify Developers Build a context driven chat bot with Netlify Blob and OpenAI | Netlify Developers Simplify deployments with Netlify’s branch-matching environment variables | Netlify Developers
Build a high-performance Remix image component with Unpic...
2024-04-26 · via Netlify Developers

Unlike many other frameworks, Remix doesn’t have a built-in image component. This is in keeping with its philosophy of using the web platform, but it does make it quite a bit more work to serve responsive images. In this guide, we’ll show you how to build a Remix image component that uses Unpic and Netlify Image CDN to automatically serve responsive, high-performance images.

#TL;DR

Using Unpic and Netlify Image CDN, you can create a high-performance image component for Remix that automatically serves responsive images.

#Why serve optimized images?

Serving optimized images is crucial for web performance. Images are usually the largest assets on a web page, and serving them in the right format and size can make a big difference in how fast your site loads. By serving responsive images, you can ensure that your images look great on all devices without slowing down your site. Doing this by hand is a pain, but there are tools that make this as easy as using an <img> tag.

#How to generate responsive images on Remix

Netlify Image CDN is enabled automatically for all sites on Netlify, making it super-easy to deliver images in the right size and format. To create our responsive image component we’ll be using Unpic, a toolkit that I created to deliver high-performance images on most frontend frameworks. It automatically supports Netlify out of the box, and we’ll be using the React component in this guide.

First, install Unpic with the package mananger of choice.

Then, use it in your page:

import { Image } from "@unpic/react";

// This is a local image import

import viaduct from "../viaduct.jpg";

export default function Page() {

return (

<div>

<Image src={viaduct} alt="A viaduct" width={800} height={600} cdn="netlify" />

</div>

);

}

That’s all you need to do! We can make it even easier though, by creating a custom Remix component that wraps the Unpic component. This way, you can use the same component throughout your app without having to repeat the cdn="netlify" prop.

// src/components/RemixImage.tsx

import { Image, type ImageProps } from "@unpic/react";

export default function RemixImage(props: ImageProps) {

return <Image {...props} cdn="netlify" />;

}

If the source image is already hosted on a CDN then it will use that one, but it will use the Netlify Image CDN for local images and images that are not hosted on an image CDN.

#How it works

In this example, we’re importing a local image using asset imports. This copies the source image into the output directory with a fingerprinted filename, then returns the URL. We can use this directly in Unpic, and it will generate the correct image URLs for the Netlify Image CDN.

This is the generated tag. You will see it has everything to create a responsive image. It resizes fluidly, loads the right resolutions depending on the screen size. You’ll also find it gives great scores on Lighthouse and other performance tools.

<img

alt="Viaduct"

loading="lazy"

decoding="async"

sizes="(min-width: 800px) 800px, 100vw"

style="object-fit:cover;max-width:800px;max-height:600px;aspect-ratio:1.333;width:100%"

srcset="

/.netlify/images?w=640&amp;h=480&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 640w,

/.netlify/images?w=750&amp;h=563&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 750w,

/.netlify/images?w=800&amp;h=600&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 800w,

/.netlify/images?w=828&amp;h=621&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 828w,

/.netlify/images?w=960&amp;h=720&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 960w,

/.netlify/images?w=1080&amp;h=810&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 1080w,

/.netlify/images?w=1280&amp;h=960&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 1280w,

/.netlify/images?w=1600&amp;h=1200&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg 1600w

"

src="/.netlify/images?w=800&amp;h=600&amp;fit=cover&amp;url=%2Fbuild%2F_assets%2Fviaduct-N2EIOTOR.jpg"

/>

If you are using images that are hosted on another domain that isn’t an image CDN, you will need to enable that source in your Netlify config:

[images]

remote_images = ["https://my-images.com/.*", "https://animals.more-images.com/[bcr]at/.*"]

The remote_images property accepts an array of regex patterns, letting you specify exactly which images you want to optimize.

Tip

You don’t need to add your own site domain to the remote_images list. Netlify Image CDN will automatically optimize images from your site.

#Get started

If you want to create a Remix site deploy to Netlify, you can get started with this command:

npx create-remix@latest --template netlify/remix-template

For more details and to see how to migrate an existing site, see the Remix on Netlify docs.