400-123-4567

基本特性: 图片优化 | Next.js | Next.js中文网发布日期:2024-05-06 05:30:07 浏览次数:
示例

The Next.js Image component, , is an extension of the HTML element, evolved for the modern web. It includes a variety of built-in performance optimizations to help you achieve good Core Web Vitals. These scores are an important measurement of user experience on your website, and are factored into Google's search rankings.

Some of the optimizations built into the Image component include:

  • Improved Performance: Always serve correctly sized image for each device, using modern image formats.
  • Visual Stability: Prevent Cumulative Layout Shift automatically.
  • Faster Page Loads: Images are only loaded when they enter the viewport, with optional blur-up placeholders
  • Asset Flexibility: On-demand image resizing, even for images stored on remote servers

To add an image to your application, import the component:


Now, you can define the for your image (either local or remote).

To use a local image, your , , or files:


Dynamic or are not supported. The must be static so it can be analyzed at build time.

Next.js will automatically determine the and of your image based on the imported file. These values are used to prevent Cumulative Layout Shift while your image is loading.



To use a remote image, the property should be a URL string, which can be relative or absolute. Because Next.js does not have access to remote files during the build process, you'll need to provide the , and optional props manually:


Learn more about the sizing requirements in .

Sometimes you may want to access a remote image, but still use the built-in Next.js Image Optimization API. To do this, leave the at its default setting and enter an absolute URL for the Image .

To protect your application from malicious users, you must define a list of remote domains that you intend to access this way. This is configured in your file, as shown below:



Note that in the example earlier, a partial URL () is provided for a remote image. This is possible because of the loader architecture.

A loader is a function that generates the URLs for your image. It appends a root domain to your provided , and generates multiple URLs to request the image at different sizes. These multiple URLs are used in the automatic srcset generation, so that visitors to your site will be served an image that is the right size for their viewport.

The default loader for Next.js applications uses the built-in Image Optimization API, which optimizes images from anywhere on the web, and then serves them directly from the Next.js web server. If you would like to serve your images directly from a CDN or image server, you can use one of the built-in loaders or write your own with a few lines of JavaScript.

Loaders can be defined per-image, or at the application level.

You should add the property to the image that will be the Largest Contentful Paint (LCP) element for each page. Doing so allows Next.js to specially prioritize the image for loading (e.g. through preload tags or priority hints), leading to a meaningful boost in LCP.

The LCP element is typically the largest image or text block visible within the viewport of the page. When you run , you'll see a console warning if the LCP element is an without the property.

Once you've identified the LCP image, you can add the property like this:


See more about priority in the component documentation.

One of the ways that images most commonly hurt performance is through layout shift, where the image pushes other elements around on the page as it loads in. This performance problem is so annoying to users that it has its own Core Web Vital, called Cumulative Layout Shift. The way to avoid image-based layout shifts is to always size your images. This allows the browser to reserve precisely enough space for the image before it loads.

Because is designed to guarantee good performance results, it cannot be used in a way that will contribute to layout shift, and must be sized in one of three ways:

  1. Automatically, using a static import
  2. Explicitly, by including a and property
  3. Implicitly, by using which causes the image to expand to fill its parent element.

If you are accessing images from a source without knowledge of the images' sizes, there are several things you can do:

Use

The layout mode allows your image to be sized by its parent element. Consider using CSS to give the image's parent element space on the page, then using the with , , or , along with the to define how the image should occupy that space.

Normalize your images

If you're serving images from a source that you control, consider modifying your image pipeline to normalize the images to a specific size.

Modify your API calls

If your application is retrieving image URLs using an API call (such as to a CMS), you may be able to modify the API call to return the image dimensions along with the URL.

If none of the suggested methods works for sizing your images, the component is designed to work well on a page alongside standard elements.

Styling the Image component is not that different from styling a normal element, but there are a few guidelines to keep in mind:

Pick the correct layout mode

The image component has several different layout modes that define how it is sized on the page. If the styling of your image isn't turning out the way you want, consider experimenting with other layout modes.

Target the image with className, not based on DOM structure

Regardless of the layout mode used, the Image component will have a consistent DOM structure of one tag wrapped by exactly one . For some modes, it may also have a sibling for spacing. These additional elements are critical to allow the component to prevent layout shifts.

The recommended way to style the inner is to set the prop on the Image component to the value of an imported CSS Module. The value of will be automatically applied to the underlying element.

Alternatively, you can import a global stylesheet and manually set the prop to the same name used in the global stylesheet.

You cannot use styled-jsx because it's scoped to the current component.

You cannot use the prop because the component does not pass it through to the underlying .

When using , the parent element must have

This is necessary for the proper rendering of the image element in that layout mode.

When using , the parent element must have

This is the default for elements but should be specified otherwise.

View all properties available to the component.

For examples of the Image component used with the various fill modes, see the Image component example app.

The component and Next.js Image Optimization API can be configured in the file. These configurations allow you to enable remote domains, define custom image breakpoints, change caching behavior and more.

Read the full image configuration documentation for more information.

以下是我们建议接下来需要学习的内容:


平台注册入口