All posts
Web design & development

next/image, Fonts and Scripts: Next.js Performance Features Explained

Next.js image optimization explained with next/image, next/font and next/script: practical defaults for marketing sites with heavy heroes, fonts and tags.

By Mehdi Hassan7 min read

The short answer

Next.js image optimization comes from three built-in features: next/image resizes and serves modern formats with lazy loading, next/font self-hosts fonts with fallbacks that prevent layout shift, and next/script controls when third-party tags load. Used with the right defaults, they fix most marketing-site performance problems before any custom work.

Key takeaways

  • Give every image a correct sizes value; without it, phones download desktop-sized files.
  • Mark only the hero image as high priority, and let everything else lazy load.
  • next/font self-hosts fonts and adjusts fallbacks, which removes most font-related layout shift.
  • Load analytics and chat with lazyOnload or after interaction, not before the page is usable.
  • Video isn't covered by next/image; use poster images, short compressed loops and click-to-load players.

Part of Next.js for Marketing Websites: When It's the Right Choice

Which built-in Next.js features improve website performance?

The three built-in Next.js features that matter most for marketing sites are the Image component (next/image), the font module (next/font) and the Script component (next/script). Together they handle the heaviest things marketing teams add: large visuals, custom typefaces and third-party tags.

They're not magic, though. Each one has defaults that work for simple pages and settings you need to get right for real ones. This guide gives the defaults we use on every build. For the bigger question of whether code suits your team, see our guide to Next.js for a marketing website.

Feature

What it does

Metric it helps most

The setting people get wrong

next/image

Resizes, converts to WebP or AVIF, lazy loads, reserves space

LCP and CLS

Missing or wrong sizes value

next/font

Self-hosts fonts, preloads them, tunes fallback metrics

CLS and LCP

Loading too many weights

next/script

Controls when and how external scripts load

INP and LCP

Loading every tag before the page is interactive

Metadata API

Generates titles, Open Graph images and tags

None directly; helps SEO

Large dynamic OG images on every request

How does Next.js image optimization work?

Next.js image optimization works by generating several sizes of each image on demand, serving the smallest one that fits the visitor's screen in a modern format, and caching the result. The Image component also lazy loads off-screen images and reserves their space so nothing jumps.

The key input is the sizes attribute. It tells the browser how wide the image will be at each breakpoint, so it can pick the right file. Without it, a full-width image is treated as full viewport width on every device, which is fine, but a card image in a three-column grid gets downloaded far larger than it displays. Remote images also need their domains listed in the config before they can be optimized.

  • Use width and height, or fill with a sized parent. Both reserve space and stop layout shift.
  • Write a real sizes value for any image narrower than the viewport, for example a card that's a third of the page on desktop and full width on mobile.
  • Keep the default lazy loading for everything below the fold.
  • Allow AVIF and WebP in the image config; the component picks what the browser supports.
  • Compress sources before upload. Optimization helps, but a huge source file still slows the first transform.

How should you load the hero image?

Load the hero image eagerly with high fetch priority, because it's usually the Largest Contentful Paint element. On the Image component, that means setting the priority option on the hero only; recent versions also offer preload and fetchPriority settings, so check the docs for your version.

Prioritizing more than one or two images defeats the purpose, since they all compete for bandwidth. Also make sure the hero image is in the server-rendered HTML, not injected by a client component or a CSS background set after load, or the browser can't discover it early. Our Core Web Vitals platform fixes cover the full LCP checklist.

How does next/font prevent layout shift?

next/font prevents layout shift by self-hosting the font files with your site and generating a fallback font with adjusted metrics, so the page doesn't reflow when the custom font arrives. It works for Google Fonts and for local font files.

Self-hosting also removes the extra connection to a font provider, which helps the first paint. The main decisions are how many families and weights you load and which character subsets you need. A variable font that covers every weight in one file is usually lighter than four static weights. Load display fonts only where they're used, and apply fonts through CSS variables so a design system can reference them by name, as described in our guide to building a design system for a marketing site.

Which next/script strategy should each tag use?

Pick the latest strategy each script can tolerate. Most marketing tags don't need to run before the visitor can read and click, and loading them early is one of the main causes of slow interactions.

Strategy

When it loads

Use it for

beforeInteractive

Before hydration, in the initial HTML

Rare: consent banners that must run first

afterInteractive (default)

Right after hydration

Tag manager, core analytics

lazyOnload

When the browser is idle

Chat widgets, heatmaps, social embeds

worker (experimental)

In a web worker

Heavy scripts, after testing compatibility

On interaction

When the visitor clicks a facade

Video embeds, chat, calendars

The on-interaction pattern is worth highlighting: show a static placeholder for a chat bubble or a video, and load the real script only when someone clicks. It removes the cost entirely for the majority who never use it. Heavy tags are the biggest INP risk on Next.js sites, which we cover in improving INP on Next.js.

Hypothetical example: a homepage audit using only built-in features

For example, suppose a homepage has a large hero image set as a CSS background, four font weights loaded from a provider, and a tag manager plus a chat widget loaded in the head.

The fixes use only built-in features. The hero becomes an Image component with fill and priority. Fonts move to next/font with one variable file and a tuned fallback. The tag manager loads afterInteractive, and the chat widget becomes a click-to-load facade. No redesign is needed, and each change is measurable in field data over the following weeks.

What about video, SVG and other heavy assets?

Video and SVG sit outside next/image, so they need their own rules. Treat a hero video as an enhancement on top of a fast poster image, and keep SVGs small and inline only when they're simple.

  • Hero video: set a poster image, use preload none or metadata, compress to a short loop, and consider showing only the poster on mobile.
  • Embedded video: use a lightweight facade and load the player on click.
  • SVG icons: inline small icons; serve large illustrations as files so they cache.
  • Lottie and animation files: load them after the first paint and pause them off-screen.

If you host on Cloudflare, check how image optimization is configured, since it may need a custom loader or the host's own image service; our guide to hosting Next.js on Cloudflare Workers explains the options. Teams moving from a builder often see the biggest gains here, as covered in our Wix to Next.js migration guide.

Frequently asked questions

Does next/image work with a headless CMS?
Yes. Add the CMS's image domain to the remote patterns in your Next.js config, and the Image component will optimize those files like local ones. Many headless CMSs also offer their own image transformation service, in which case you can use a custom loader so the CMS does the resizing and Next.js just builds the right URLs.
Should I use AVIF or WebP for marketing images?
Enable both in the image config and let Next.js choose per browser. AVIF usually produces smaller files at similar quality but takes longer to encode the first time, while WebP is faster to generate and universally supported in current browsers. For most marketing sites, allowing both gives the best results without manual work.
Does next/font work with Adobe Fonts or other services?
next/font supports Google Fonts and local files. For fonts from a subscription service, check whether your license allows self-hosting; if it does, download the files and load them with the local option. If the license only allows the provider's embed, you lose the self-hosting benefit, so tune the fallback font yourself to limit layout shift.
Why is my Next.js site still slow with next/image?
Images are only one cause. Common others are a dynamic page that should be static, heavy client-side JavaScript, third-party scripts loading early and large fonts. Check real-user data for which metric fails, then look at the matching cause. A good LCP with a poor INP, for example, points to scripts and hydration, not images.
Should Open Graph images be generated dynamically?
Generated Open Graph images are useful for blogs and changelogs with many pages. Make sure they're generated at build time or cached, not created fresh on every request, since crawlers and social platforms fetch them often. For a handful of key pages, a designed static image is simpler and usually looks better.

· CEO & Design Director

Copywriter, designer and developer, and founder of Designify Studio. Over five years, 60+ projects across 14+ industries, for startups that have raised $300M+.

Want this done for you? Website, $8,000.

A multi-page marketing site, designed by us, developed in Next.js and animated. $8,000, one-time, up to 6 pages. Book a free 30-minute call to see if it fits.

Schedule Call