Skip to content

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

@yanghuidong

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions