Skip to content
Tutorials Center
My learning

Topics

Lesson 65 of 132 ยท Performance

SEO, Metadata and Core Web Vitals in React

Make a React site easy to find and fast to use - head tags with React 19, why rendering on the server matters for crawlers, streaming, and fixing LCP, INP and CLS.

Two things decide whether a page succeeds on the open web: can people find it, and does it feel fast once they arrive? Search engines and social networks read your page to decide the first. Browsers and real visitors measure the second. A plain React single-page app can do both well, but only if you know where its default setup falls short.

In this lesson you will learn how to set the title and meta tags in React 19, why the HTML your server sends first matters so much, what the three Core Web Vitals measure, and the React-specific habits that improve each one. The earlier profiling lesson measures what happens inside React. This lesson looks at what the visitor and the search engine see from outside.

What a crawler sees

Imagine a client-only app built with Vite. The server sends this file for every URL:

HTML
<!doctype html>
<html lang="en">
  <head><title>My Shop</title></head>
  <body>
    <div id="root"></div>
    <script type="module" src="/assets/index-C3k9sQ1a.js"></script>
  </body>
</html>

Everything useful appears only after the script downloads and runs. Large search engines do run JavaScript, but later and with a queue, so new or changed pages can take longer to be understood. Many other consumers do not run it at all: link-preview bots from chat apps and social networks read <meta property="og:title"> straight from the HTML they receive. If your page title and preview image are set only by JavaScript, those previews will show the generic title for every page.

The remedy is to send real content in the first response. There are three ways, and they trade effort for freshness:

Approach HTML at first request Good for
Client-side rendering Empty shell Logged-in apps and dashboards that search engines should not index anyway
Static generation Full pages built ahead of time Blogs, docs, marketing pages
Server-side rendering (SSR) Full page rendered on each request Product pages, listings, anything that changes often

The SSR and hydration lesson shows how this works with renderToString and hydrateRoot, and server components cover how frameworks such as Next.js do it for you. In practice, a public site that depends on search traffic should use a framework (see Next.js App Router basics) rather than hand-built SSR.

Managing the document head

Each page needs its own <title>, a <meta name="description">, and often a canonical link and social-preview tags. These live in <head>, far from the component that knows the page content. For years React apps needed a library such as React Helmet to move them there.

React 19 does it natively. If you render <title>, <meta> or <link> anywhere in a component, React hoists the element into the document's <head> for you. During server rendering the tags are written into the head of the streamed HTML, so crawlers and link previews see them.

Head tags from any component

The inspector reads the real document: the title and description are in <head> and inBody is false, even though the elements were written inside the page component. Two details matter:

  • Give <title> a single string. The template literal above does that; writing <title>{name} | Shop</title> passes several children, which React warns about.
  • If two components render a <title>, the one deeper in the tree that renders last wins. Keep one owner per page (usually the route component) to avoid surprises.

Frameworks wrap this in their own APIs. In the Next.js App Router you export a metadata object or a generateMetadata function from a page, and the framework merges the tags from layouts and pages for you.

Tags worth setting

  • <title>: unique per page, describes the page, front-load the important words. Around 60 characters before search engines may cut it.
  • <meta name="description">: a one-sentence summary. It is not a ranking factor on its own, but it is often the text shown under your link in results.
  • <link rel="canonical">: the preferred URL when the same content is reachable at several addresses (tracking parameters, sort orders).
  • Open Graph tags (og:title, og:description, og:image): the preview card in chat apps and social posts.
  • <meta name="robots" content="noindex">: keep private or thin pages out of search results.
  • Structured data as JSON-LD: helps search engines show rich results (product price, article date, breadcrumbs).

JSON-LD is a script tag, and the data often comes from a database, so it needs one precaution. If a value contains the text </script>, it would end your tag early and open an injection hole. Escape < when you serialize:

JSX
function JsonLd({ data }) {
  const json = JSON.stringify(data).replace(/</g, '\\u003c');
  return <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: json }} />;
}

This is one of the few legitimate uses of dangerouslySetInnerHTML; the escaping is what makes it safe (see React security).

Search engines follow <a href> links. A <div onClick={() => navigate('/pricing')}> is invisible to them, to keyboard users and to people who want to open the page in a new tab. Use your router's link component, which renders a real anchor (see client-side routing). Give pages stable, readable URLs, and make sure every page you want indexed is reachable through links, listed in a sitemap, or both. A missing page should return an actual 404 status, not a 200 with a "not found" message, which only server rendering can do correctly.

Streaming and perceived speed

With classic SSR the server must finish all data fetching before it sends any HTML. Streaming SSR sends the page in pieces. React renders the parts that are ready, sends them, leaves a placeholder where a <Suspense> boundary is waiting for data, and fills it in when the data arrives. The visitor sees the header and main content early while a slow comments section is still loading. See the Suspense section in the SSR lesson and use and Suspense.

sequenceDiagram
  participant B as Browser
  participant S as Server
  B->>S: GET /product/42
  S-->>B: Head, header, product details (fast data)
  Note over B: Visitor can read the product
  S-->>B: Reviews section (slow data), swapped in
  Note over B: Page complete

Streaming lets the server send the shell and fast parts first and the slow Suspense sections later, on the same response.

For search engines, content that arrives by streaming is still in the HTML, so it can be indexed. Put critical content (the product name, the article text) outside slow boundaries.

Core Web Vitals

Google's Core Web Vitals are three measurements of real-user experience. Each is judged at the 75th percentile of your visitors' real visits, so a page is "good" only when at least three in four visits are good.

Metric Measures Good Poor
LCP (Largest Contentful Paint) How long until the largest visible element (hero image, headline) is drawn 2.5 s or less over 4 s
INP (Interaction to Next Paint) How quickly the page responds visually to clicks, taps and key presses, across the whole visit 200 ms or less over 500 ms
CLS (Cumulative Layout Shift) How much visible content jumps around unexpectedly 0.1 or less over 0.25

INP replaced the older First Input Delay metric in March 2024. There is a gap between the two numbers in each row: values between "good" and "poor" are labeled "needs improvement".

Classify vitals

Measuring

There are two kinds of data. Lab data comes from a controlled run in a tool such as Lighthouse or the browser's performance panel; it is repeatable and great for debugging. Field data comes from real visitors, through Google's Chrome User Experience Report (shown in PageSpeed Insights and Search Console) or by collecting numbers yourself with the small web-vitals library:

JavaScript
import { onLCP, onINP, onCLS } from 'web-vitals';

function send(metric) {
  navigator.sendBeacon('/analytics', JSON.stringify({ name: metric.name, value: metric.value }));
}

onLCP(send);
onINP(send);
onCLS(send);

Search ranking uses field data, so lab scores are a guide, not the final judgment.

Improving LCP

LCP is mostly about how quickly the first HTML and the main image or heading arrive.

  • Send real HTML (static generation or SSR). A client-only app cannot draw its hero until JavaScript has run.
  • Never lazy-load the hero image. Mark it fetchpriority="high" and leave out loading="lazy"; lazy-load only images below the fold.
  • Serve properly sized, compressed images (AVIF or WebP) with srcset, and host fonts and critical files close to the visitor through a CDN.
  • Keep the first bundle small with code splitting so the browser is not busy parsing code for screens nobody opened.

Improving INP

INP suffers when the main thread is busy at the moment of a click.

  • Keep event handlers short. Heavy work after a click blocks the next paint.
  • Mark slow, non-urgent updates as transitions so typing and clicking stay responsive.
  • Avoid rendering huge lists; use virtualization and measure with the profiler.
  • Remove needless re-renders with memo or the React Compiler, but only after measuring.
  • Third-party scripts (chat widgets, tag managers) are common offenders. Load them late.

Improving CLS

Layout shift happens when something appears and pushes content you were reading. The cure is to reserve space before the content arrives.

  • Give every <img> and <video> width and height attributes, or a CSS aspect-ratio.
  • Reserve a fixed-height box for ads, embeds and async widgets.
  • Do not insert banners above existing content; push in from a position that does not move the text, or reserve the room.
  • Use font-display: swap with a matching fallback font to limit text jumps when web fonts load.

Press the buttons below to simulate a late-loading image. The left column leaves no room, so the text jumps down; the right column reserves the space with aspect-ratio, so nothing moves.

Layout shift: with and without reserved space

A practical checklist

  1. Public pages: send full HTML with unique title, description and canonical.
  2. Use real <a href> links and return proper 404 and redirect status codes.
  3. Add Open Graph tags and JSON-LD for pages that are shared or compared.
  4. Preload the hero image, lazy-load everything below it, and size every image.
  5. Keep the main thread free: transitions, small handlers, late third-party scripts.
  6. Measure field data, fix the worst page first, and re-measure.

Private screens such as a dashboard behind a login do not need any of this for search, because crawlers cannot sign in. Spend the effort where traffic comes from search or shared links.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why can a client-only React app have poor link previews in chat apps?

    Preview crawlers fetch the HTML and read the head. If the tags are added later by JavaScript, they see only the generic shell.
  2. 2. In React 19, what happens when a component renders a <title> element in its output?

    React 19 treats title, meta and link as special: it places them in the document head, including during server rendering.
  3. 3. Which change most directly reduces Cumulative Layout Shift?

    CLS counts unexpected movement. Reserving space before an image or widget loads means nothing is pushed around when it appears.
  4. 4. What does INP measure?

    Interaction to Next Paint tracks the delay between a click, tap or key press and the next frame showing the result. Long main-thread tasks make it worse.
  5. 5. Why is it a mistake to add loading="lazy" to the largest image at the top of the page?

    The browser waits to load lazy images until it knows they are near the viewport, which pushes the hero image later and worsens LCP. Lazy-load only content below the fold.

Summary

  • Crawlers and link previews read the first HTML response, so public pages should be statically generated or server rendered, not an empty client shell.
  • React 19 hoists <title>, <meta> and <link> rendered anywhere into <head>; frameworks add metadata APIs on top.
  • Set a unique title, description and canonical URL per page, add Open Graph and JSON-LD where useful, and escape < in JSON-LD.
  • Use real links and correct status codes. Stream slow parts behind Suspense and keep critical content outside them.
  • Core Web Vitals are LCP (2.5 s), INP (200 ms) and CLS (0.1), judged on field data at the 75th percentile.
  • Improve LCP with real HTML and a prioritized hero image, INP with short handlers and transitions, and CLS by reserving space.

Next: Compound Components and Slots