Skip to content

Route head function can still execute before loader function finishes #6221

Closed

Description

@yanghuidong

Which project does this relate to?

Router

Describe the bug

A follow-up of PR #6093 - In certain scenarios, head function can still run before loaderData is ready, resulting in incorrect head computation, see an example below.

Your Example Website or App

n/a

Steps to Reproduce the Bug or Issue

Say we have a blog site, and accessing an article requires auth. Let's start with the user being logged out.

  1. Visit /article/123, shows "Not found" page content, and a generic title.
  2. Log in, and for the sake of argument, the user is redirected to /dashboard.
  3. Click browser's "Back" button, i.e. navigate back to /article/123.

Expected behavior

Now we can see an inconsistent state: the document body shows the article content correctly, but we still get the generic title, instead of the article's title, as we expect to see.

The reason is, the head function, which is supposed to return the article title from its loaderData, runs before the loader function can actually finish, and thus, loaderData is not available at the time of execution. (On the other hand, the article content shows correctly, because Route.useLoaderData() works as expected.)

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.143.7
  • OS: macOS
  • Browser: Chrome
  • Browser Version: 144.0
  • Bundler: Vite
  • Bundler Version: 7.3.0

Additional context

No response

Activity

  1. Dince-afk commented on Jun 3, 2026

    @Dince-afk

    I am experiencing a head and component loaderData content mismatch as well in this route:

    import { generatePageTitle } from "@/lib/seo";
    import { createFileRoute } from "@tanstack/react-router";
    
    export const Route = createFileRoute("/quote")({
      head: ({ loaderData }) => ({
        meta: [
          {
            title: generatePageTitle(`Quote by ${loaderData?.author || "..."}`),
          },
        ],
      }),
      loader: async () => {
        await new Promise((resolve) => setTimeout(resolve, 2000));
        const ranNum = Math.floor((Math.random() + 1) * 25);
        const res = await fetch(`https://dummyjson.com/quotes/${ranNum}`);
        const quote: { quote: string; author: string; id: number } =
          await res.json();
        return quote;
      },
      pendingComponent: () => <div>Loading...</div>,
      component: RouteComponent,
    });
    
    function RouteComponent() {
      const randomQuote = Route.useLoaderData();
      return (
        <div className="space-y-4 text-center">
          <p className="mx-auto max-w-prose italic">{randomQuote.quote}</p>
          <p className="font-extralight text-neutral-500">{randomQuote.author}</p>
          <p className="text-neutral-500">{randomQuote.id}</p>
        </div>
      );
    }

    On subsequent client navigations to that route I keep getting fresh (based on the reactive useLoaderData() hook) component data, but for the head title Quote by ${loaderData?.author || "..."} I don't get the same fresh loaderData. Always just the previous navigation loaderData. Head title is always lagging behind one loaderData run.

  2. added 3 commits that reference this issue on Jun 25, 2026
    4f35374
    aadf3ba
    e9acd3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions