Skip to content

Throwing notFound() in loader causes infinite "pending" state #1795

@melv-n

Description

@melv-n

Describe the bug

If you throw notFound() in a loader after a short delay (e.g. when awaiting a request), the parent routes stay forever stuck in "pending" state.

This behaviour does not happen if the loader promise resolves very quick, e.g. within 100ms.

Simplest reproduction:

export const Route = createFileRoute('/posts/$postId')({
  loader: async ({ params }) => {
    try {
      // Simulate a slow request. Resolving this within 100ms works fine, but if it takes longer
      // the route state will be forever stuck in pending.
      await new Promise((resolve, reject) => {
        setTimeout(() => {
          reject();
        }, 1000);
      });
    } catch (err) {
      // Never shows
      throw notFound();
    }
  },

  notFoundComponent: () => <div>Post not found</div>,
});

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-jc3pyp?file=src%2Froutes%2Findex.tsx

Steps to Reproduce the Bug or Issue

  1. Go to /posts/whatever in Stackblitz (posts.$postId.tsx)

Expected behavior

A "Not found message"

Actual behavior

The matched root route is stuck in a pending state according to the dev tools, the final route correctly has "not found" state.

This causes the page to never load.

Platform

  • OS: macOS
  • Browser: Arc (Chrome)
  • Version: 1.47

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions