Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 39 ยท Streaming and Performance

Prefetching and Pending UI

Make navigation feel instant in Remix and React Router: prefetch with the Link prefetch prop, show progress with useNavigation, build optimistic UI with fetchers, and avoid layout jumps.

Perceived speed matters as much as real speed. Two techniques make an app feel instant: doing the work earlier (prefetching) and telling the user something is happening (pending UI). Remix and React Router framework mode give you both with a handful of props and hooks, and they are built on the same router that runs your loaders, so there is no extra state to keep in sync.

A <Link> can fetch what the next page needs before the user clicks. The prefetch prop chooses when:

TSX
// read-only, React Router framework mode
import { Link, NavLink } from "react-router";

<Link to="/pricing" prefetch="intent">Pricing</Link>
<Link to="/blog" prefetch="viewport">Blog</Link>
<NavLink to="/docs" prefetch="render">Docs</NavLink>
Value When the prefetch starts Good for
none (default) never rarely used links
intent on hover or keyboard focus (with a short delay) most links; cheap and effective
viewport when the link scrolls into view lists of likely next steps
render as soon as the link renders the one or two links almost everyone takes

Prefetching loads the route's JavaScript chunk, CSS and loader data. With intent, the 200 ms or so between hover and click is enough to hide most of the latency. Be selective: prefetching every link in a long list wastes the visitor's data and your server's capacity. Prefetched data respects the response's cache headers (see HTTP Caching and Headers), so loaders for prefetched routes should be cacheable or cheap.

Node.js Stand-in: how much latency does hover prefetching hide?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Pending UI with useNavigation

While the router fetches loader data for a new page, the old page stays on screen (this is better than a blank screen). useNavigation() tells you what is going on so you can show progress:

TSX
import { useNavigation } from "react-router";

export default function Root() {
  const navigation = useNavigation();
  const loading = navigation.state === "loading";

  return (
    <body>
      <div className={loading ? "progress-bar active" : "progress-bar"} aria-hidden="true" />
      <main aria-busy={loading} className={loading ? "dimmed" : ""}>
        <Outlet />
      </main>
    </body>
  );
}

navigation.state is one of "idle", "loading" (GET navigation or revalidation after an action) and "submitting" (a form post). navigation.location is where it is heading, which lets you highlight only the link being loaded. Two tips: add a small delay (around 150 ms) before showing a spinner, since fast navigations that flash a spinner feel worse than none, and mark the container aria-busy for assistive technology.

Node.js Stand-in: the navigation state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Pending UI for a single form: useFetcher

Not every action should navigate. useFetcher submits to an action or loader without changing the URL, and exposes its own state. It is ideal for a like button, a rename field or a "mark as done" checkbox:

TSX
import { useFetcher } from "react-router";

function LikeButton({ postId, liked }: { postId: string; liked: boolean }) {
  const fetcher = useFetcher();
  // Optimistic UI: assume success while the request is still in flight
  const optimisticLiked = fetcher.formData ? fetcher.formData.get("liked") === "1" : liked;

  return (
    <fetcher.Form method="post" action={`/posts/${postId}/like`}>
      <input type="hidden" name="liked" value={optimisticLiked ? "0" : "1"} />
      <button type="submit" aria-pressed={optimisticLiked}>
        {optimisticLiked ? "Unlike" : "Like"}
      </button>
    </fetcher.Form>
  );
}

While the request is in flight, fetcher.formData holds what was submitted, so the button flips immediately. If the action fails, the router revalidates and the real value returns. The key idea: derive the interface from what the user just did, then let the server's answer replace it.

Node.js Stand-in: optimistic update with rollback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Avoiding layout jumps

Reserve space for streamed or pending content (a fixed-height skeleton), set width and height on images, and keep the old page visible during navigation instead of replacing it with a loader. These small habits improve the Cumulative Layout Shift score that search engines measure. Streaming with fallbacks is covered in Streaming with Promises.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which prefetch value starts loading when a link is hovered or focused?

    intent prefetches on hover or focus, a good balance between speed and wasted requests.
  2. 2. What does navigation.state equal while a form post is running its action?

    After the action finishes, loaders revalidate and the state becomes loading until the new data renders.
  3. 3. When is useFetcher better than a normal Form?

    Fetchers run actions and loaders in the background and keep their own state.
  4. 4. Why delay a loading spinner by about 150 ms?

    Only slow navigations benefit from visible progress.

Summary

  • prefetch="intent" hides most latency; use render and viewport sparingly.
  • useNavigation exposes idle, loading and submitting for progress UI; keep the old page visible.
  • useFetcher plus fetcher.formData gives optimistic UI without navigation.
  • Reserve space to avoid layout jumps.

Next: Bundle Size and Performance