Skip to the solution
HardFrontend6 min read

What are the resource preloading APIs in React 19 (preload, preinit, etc.)?

335 views

The problem

Question presented to candidate: "React 19 added resource preloading APIs. What are they, and what problem do they solve that a plain link tag does not?"

What a strong answer should cover:

  • Six functions exported from react-dom: preload, preinit, preconnect, prefetchDNS, preloadModule, preinitModule.
  • They let a component declare a resource need from inside component code, rather than requiring a hand-maintained list of tags in the document head.
  • preload fetches and caches; preinit fetches and executes/applies. That is the key distinction β€” preinit a stylesheet and it applies, preinit a script and it runs.
  • preconnect opens the connection (DNS, TCP, TLS) without fetching anything; prefetchDNS does only the DNS lookup β€” cheaper, for a host you might use.
  • React deduplicates calls, so calling from several components is safe.
  • They work during SSR too, so the hints are emitted into the streamed HTML β€” earlier than any client-side effect could manage.
  • Why it matters: the browser preload scanner cannot see resources that only a component knows it needs, so this closes a real gap.
  • Judgement: preloading everything is counterproductive β€” it competes for bandwidth with what the page needs right now.

Clarifying questions expected:

  • "Is the resource needed for this render, or a likely next navigation?" β€” that decides preload versus prefetch-style hints.

Code / implementation expected: Yes β€” calling them from a component, and inspecting what lands in the document head.

  • react
  • react-19
  • performance

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 10 sections

Try it in the playground

Open any example in the playground to run and change it.

ReactCalling the preloading APIs and inspecting what lands in the head
import { useState } from "react";
import { preload, preinit, preconnect, prefetchDNS } from "react-dom";

// A component that knows about a resource the document head could never
// predict β€” it is only needed once this modal opens.
function RichEditor() {
  // Fetch and cache: we will want this font shortly.
  // Fonts need crossOrigin or the browser fetches them twice.
  preload("https://fonts.gstatic.com/s/inter/v13/example.woff2", {
    as: "font",
    type: "font/woff2",
    crossOrigin: "anonymous",
  });

  // Fetch AND apply: this stylesheet should take effect now.
  preinit("https://cdn.example.com/editor-theme.css", { as: "style" });

  return (
    <div style={{ background: "#eef", padding: 12, borderRadius: 8 }}>
      Editor mounted β€” it declared its own resource needs on render.
    </div>
  );
}

export default function App() {
  const [openEditor, setOpenEditor] = useState(false);
  const [links, setLinks] = useState([]);

  const warmUpApi = () => {
    // Confident we will call this host: open the whole connection.
    preconnect("https://api.example.com");
    // Only a possibility: just resolve the name, which is far cheaper.
    prefetchDNS("https://analytics.example.com");
    inspect();
  };

  const inspect = () =>
    setLinks(
      [...document.head.querySelectorAll("link")]
        .map((l) => l.rel + "  β†’  " + (l.getAttribute("href") || "").slice(0, 58))
        .filter((s) => s.includes("example")),
    );

  return (
    <div style={{ padding: 24, fontFamily: "system-ui", lineHeight: 1.6, maxWidth: 540 }}>
      <p>
        <button onClick={warmUpApi} style={{ marginRight: 6 }}>
          preconnect + prefetchDNS
        </button>
        <button onClick={() => { setOpenEditor(true); setTimeout(inspect, 50); }}>
          open the editor (preload + preinit)
        </button>{" "}
        <button onClick={inspect}>re-inspect head</button>
      </p>

      {openEditor && <RichEditor />}

      <h4 style={{ margin: "14px 0 6px" }}>Resource hints now in document.head</h4>
      <pre style={{ background: "#f6f6f6", padding: 10, borderRadius: 6, fontSize: 12 }}>
        {links.length ? links.join("\n") : "(none yet β€” press a button)"}
      </pre>

      <p style={{ color: "#666", fontSize: 13 }}>
        React deduplicates these, so calling them on every render of the editor
        produces one hint each. Notice the editor declared its own needs β€” no
        central list in the document head had to know about it in advance.
      </p>
    </div>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 105 of 119 in React.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All React.js questions
Up nextHard

What is hydration in React?

Continue