Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Next.js Images and Fonts

Next.js Images and Fonts

Next.js provides built-in image and font optimization features designed to improve application performance and user experience.

Next.js's image and font optimization features provide developers with powerful tools to help build high-performance web applications with excellent user experience.

Function Description
Image Optimization Use component, which supports local and remote images and automatically optimizes size and loading performance.
Font Optimization Use next/font module, which supports Google Fonts and local fonts and automatically hosts and optimizes loading.
Static Asset Management Place static assets into public directory, via /filename for direct access.

Static Asset Management

Next.js allows you to store static assets (such as images, fonts, etc.) in the public directory.

These assets can be accessed directly via /filename.

Example directory structure:

my-next-app/

├── app/

├── public/

│   ├── images/

│   │   └── logo.png

│   └── fonts/

│       └── my-font.woff2

├── package.json

└── next.config.js

public/images/logo.png It can be accessed via /images/logo.png.

Image Optimization

Next.js provides component, used to optimize image loading. It extends HTML's element and provides the following features:

Use Component

From the next/image Import component, and use it in the component.

Example

import

Image from

'next/image'

;

export

default

function

Page

(

)

{

 

return

<

Image src

=

"/images/logo.png"

alt

=

"Logo"

width

=

{

500

}

height

=

{

300

}

/>;

}

Local Images

Local images can be referenced directly from the public directory.

Example

import

Image from

'next/image'

;

import

profilePic from

'./me.png'

;

export

default

function

Page

(

)

{

 

return

(

   

<

Image

src

=

{

profilePic

}

alt

=

"Picture of the author"

     

// width and height are automatically read from the image file

     

// blurDataURL and placeholder are optional

   

/>

 

)

;

}

Remote Images

Remote images require you to specify width and height manually to avoid layout shift.

Example

import

Image from

'next/image'

;

export

default

function

Page

(

)

{

 

return

(

   

<

Image

src

=

"https://s3.amazonaws.com/my-bucket/profile.png"

alt

=

"Picture of the author"

      width

=

{

500

}

      height

=

{

500

}

   

/>

 

)

;

}

Configure Remote Images

Configure the allowed remote image domains in next.config.js.

Example

const

nextConfig

=

{

  images

:

{

    remotePatterns

:

[

     

{

        protocol

:

'https'

,

        hostname

:

's3.amazonaws.com'

,

        port

:

''

,

        pathname

:

'/my-bucket/**'

,

     

}

,

   

]

,

 

}

,

}

;

export

default

nextConfig

;


Font Optimization

Next.js provides next/font module, used to optimize font loading. It supports automatically hosting font files, improving privacy and performance.

Using Google Fonts

Import fonts from next/font/google and apply them to HTML elements.

Example

import

{

Geist

}

from

'next/font/google'

;

const

geist

=

Geist

(

{

  subsets

:

[

'latin'

]

,

}

)

;

export

default

function

Layout

(

{

children

}

)

{

 

return

(

   

<

html lang

=

"en"

className

=

{

geist.

className

}

     

<body{children}body html)>

;

}

If you use a non-variable font, you need to specify the weight:

Example

import

{

Roboto

}

from

'next/font/google'

;

const

roboto

=

Roboto

(

{

  weight

:

'400'

,

  subsets

:

[

'latin'

]

,

}

)

;

export

default

function

Layout

(

{

children

}

)

{

 

return

(

   

<

html lang

=

"en"

className

=

{

roboto.

className

}

     

<body{children}body html)>

;

}

Using Local Fonts

Import local font files from next/font/local.

Example

import

localFont from

'next/font/local'

;

const

myFont

=

localFont

(

{

  src

:

'./my-font.woff2'

,

}

)

;

export

default

function

Layout

(

{

children

}

)

{

 

return

(

   

<

html lang

=

"en"

className

=

{

myFont.

className

}

     

<body{children}body html)>

;

}

If you use multiple font files:

Example

const

roboto

=

localFont

(

{

  src

:

[

   

{

      path

:

'./Roboto-Regular.woff2'

,

      weight

:

'400'

,

      style

:

'normal'

,

   

}

,

   

{

      path

:

'./Roboto-Italic.woff2'

,

      weight

:

'400'

,

      style

:

'italic'

,

   

}

,

   

{

      path

:

'./Roboto-Bold.woff2'

,

      weight

:

'700'

,

      style

:

'normal'

,

   

}

,

   

{

      path

:

'./Roboto-BoldItalic.woff2'

,

      weight

:

'700'

,

      style

:

'italic'

,

   

}

,

 

]

,

}

)

;