Fixing Render-Blocking Resources and Layout Shift in a Next.js Site
A step-by-step walkthrough of the Core Web Vitals problems that show up most often in Next.js sites — render-blocking fonts, layout shift from un-sized images, unused JavaScript, and long main-thread tasks — and exactly how to fix each one.
Core Web Vitals are not a check-box exercise. Render-blocking resources and layout shift make a page feel broken, and they directly affect how Google ranks and how users perceive a product. I audited a personal Next.js site and found the same four issues that appear in almost every project I review. Here is how I fixed them.
Problem 1: render-blocking fonts
The most common culprit is a CSS @import for Google Fonts. That single line forces the browser to download a stylesheet from a third-party origin, which in turn points at another origin for the actual font files. The page cannot paint reliably until that chain resolves — a classic render-blocking, network-dependency-tree problem.
The fix is to let the framework self-host the fonts at build time. In the App Router that is next/font, which downloads and inlines the font, emits a preload link, generates a metric-compatible fallback to prevent shift, and serves the file from your own immutable-cached origin.
import { DM_Sans } from "next/font/google";
export const fontBody = DM_Sans({
subsets: ["latin"],
display: "swap",
variable: "--font-body",
fallback: ["Helvetica", "Arial", "sans-serif"],
});
Why this fixes three audits at once
Self-hosting removes the render-blocking request, collapses the network dependency tree, and gives you long-lived caching plus a fallback that matches the real font's metrics — which removes the font-related layout shift.
Problem 2: layout shift from un-sized media
Any image without intrinsic dimensions causes a layout shift when it loads. A logo displayed at 32 pixels that ships as a 4000-pixel file is a double offence: a huge payload and a shift. Use next/image so the browser reserves space from the width and height you provide, and resize the source asset to the size it is actually rendered at.
import Image from "next/image";
<Image src="/logos/recurso.png" alt="Recurso Labs logo" width={32} height={32} className="w-8 h-8 object-contain" />
Also remove spaces from asset filenames. Encoded paths are easy to get wrong, cache poorly, and behave inconsistently across CDNs.
Problem 3: unused JavaScript
The fastest JavaScript is the JavaScript you never ship. In a Next.js App Router project, adding "use client" to a component that has no interactivity pulls it and its dependencies into the client bundle for no reason. Static sections — services, skills, project lists — should be server components.
- Keep "use client" only on components that use state, effects, refs, or event handlers.
- Remove dependencies you are not importing. Dead packages are dead weight in audits and in review.
- Prefer a small icon set that tree-shakes instead of a monolithic UI kit.
- Check the build output: the First Load JS number is the one your users pay.
Problem 4: long main-thread tasks
A requestAnimationFrame loop that never stops will keep the main thread busy for the entire lifetime of the page, even when the animated element is far off screen. That shows up as long tasks and a janky scroll.
- Pause animation work when the element leaves the viewport, using an IntersectionObserver.
- Pause when the tab is hidden, using the visibilitychange event.
- Reduce particle counts on small screens and cap devicePixelRatio.
- Honour prefers-reduced-motion and skip the animation entirely for those users.
const io = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting && !document.hidden) start();
else stop();
});
io.observe(canvas);
The order that matters
- Self-host fonts. It is the biggest, easiest win.
- Give every image explicit dimensions and resize the sources.
- Move static components to the server and delete unused dependencies.
- Gate any animation loop on visibility and motion preferences.
- Re-measure. Verify with real numbers instead of assuming.
None of these require a rewrite. They are configuration and discipline, and together they turn a slow, shifting page into one that paints instantly and stays stable.