Skip to main content
0 reactions

Fixed: “Error in renderToReadableStream: { isNotFound: true }” in TanStack Start

Card titled Error in renderToReadableStream: { isNotFound: true }, labelled Fixed, TanStack Start, with a warning sign and two facts: in the component, status 200; in the loader, status 404.
Fixed: Error in renderToReadableStream: { isNotFound: true } in TanStack Start

If your TanStack Start dev server prints “Error in renderToReadableStream: { isNotFound: true }” when you open a page, a page component threw notFound() while the server was rendering it. The browser still shows your 404 page, but the server answers with the status code 200. This post shows the error, the docs line that explains it, and the fix: throw notFound() from the route’s loader.

What I Was Doing

I was giving every book its own page in a TanStack Start app, the Bookshop, while writing TanStack Start Tutorial — Part 4: Dynamic Routes and Path Params. The route file looked for the book in its component and threw notFound() when there was none:

src/routes/books/$bookId.tsxTypeScript
import { createFileRoute, notFound } from "@tanstack/react-router";
import { books } from "#/lib/books";

export const Route = createFileRoute("/books/$bookId")({ component: BookPage });

function BookPage() {
	const { bookId } = Route.useParams();
	const book = books.find((book) => book.id === bookId);

	if (book === undefined) {
		throw notFound();
	}

	return (
		<main className="mx-auto max-w-2xl p-8">
			<h1 className="text-4xl font-bold">{book.title}</h1>
			<p className="mt-4 text-lg">by {book.author}</p>
		</main>
	);
}

Then I opened an address with a book ID that doesn’t exist: http://localhost:3000/books/99.

What I Expected

The app’s 404 page, from the root route’s notFoundComponent, with the status code 404, so that browsers and search engines know the page doesn’t exist.

What I Got

The browser showed the 404 page, but the terminal running the dev server printed an error:

Output (the lines that matter)Plain text
Error in renderToReadableStream: { isNotFound: true } { componentStack: [Getter] }
4:18:02 PM [vite] (client) [console.error] { isNotFound: true, routeId: "__root__" }

The above error occurred in the <BookPage> component.

React will try to recreate this component tree from scratch using the error boundary you provided, CatchBoundary.
A terminal window with the lines Error in renderToReadableStream: { isNotFound: true }, a client console error with routeId __root__, and the React message that the error occurred in the BookPage component.
The dev server’s terminal after opening a book page that throws notFound() in its component.

What I Checked

The page looked right, so the one check that mattered was the status code the server sent. curl -I asks for the response’s headers only:

TerminalBash
curl -I http://localhost:3000/books/99
Output (the lines that matter)Plain text
HTTP/1.1 200
Vary: Origin
content-type: text/html; charset=utf-8

The server answered 200, as for a page that exists. TanStack Router’s Not Found Errors guide (opens in a new tab) allows throwing notFound() in components, but recommends throwing it in loaders instead, “to correctly type loader data and prevent flickering”.

Why It Happens

The loader and the component run at different times. TanStack Start runs a route’s loader before it renders the page, so a notFound() thrown there is known before any HTML goes out, and the server sends the 404 page with the status code 404.

A component runs while React renders the page into HTML on the server, which is what renderToReadableStream (opens in a new tab), named in the error, does. A notFound() thrown there breaks off that render: the server’s page arrives with the status code 200 and only the header, and React then rebuilds the page in the browser from the nearest error boundary, as its message says. That’s how the 404 page still appears.

The Fix

Move the lookup and notFound() into the route’s loader, and read the book in the component with Route.useLoaderData():

src/routes/books/$bookId.tsxTypeScript
import { createFileRoute, notFound } from "@tanstack/react-router";
import { books } from "#/lib/books";

export const Route = createFileRoute("/books/$bookId")({
	loader: ({ params }) => {
		const book = books.find((book) => book.id === params.bookId);

		if (book === undefined) {
			throw notFound();
		}

		return book;
	},
	component: BookPage,
});

function BookPage() {
	const book = Route.useLoaderData();

	return (
		<main className="mx-auto max-w-2xl p-8">
			<h1 className="text-4xl font-bold">{book.title}</h1>
			<p className="mt-4 text-lg">by {book.author}</p>
		</main>
	);
}
  • The loader gets the path params as params, so the component no longer needs Route.useParams().
  • The loader throws before it could return nothing, so TypeScript knows the component always gets a book.

After the Fix

The terminal prints no error for /books/99, and the server answers with the status code 404:

TerminalBash
curl -I http://localhost:3000/books/99
Output (the lines that matter)Plain text
HTTP/1.1 404
Vary: Origin
content-type: text/html; charset=utf-8
A terminal window with the response lines HTTP/1.1 404, Vary: Origin and content-type: text/html; charset=utf-8.
After the fix, the server answers an unknown book with the status 404.

The browser shows the same 404 page as before, now with the right status code.

Common Mistakes

MistakeWhat happensDo this instead
Throwing notFound() in the componentThe page looks right, but the status code is 200 and the terminal logs the errorThrow it in the route’s loader
Judging the fix by the page aloneThe 404 page looks the same with either status codeCheck the status code with curl -I
Leaving the root route without a notFoundComponentTanStack Router shows its plain default page, which only says “Not Found”Add one, as in How to Add a Custom 404 Page in TanStack Start

Troubleshooting

ProblemFix
The error comes back on another pageLook for notFound() in that page’s component, and move it to the route’s loader.
The terminal still shows the error after the changeOpen the page again: the error belongs to the request that rendered it.

FAQ

Is it ever right to throw notFound() in a component? TanStack Router allows it, and its CatchNotFound component can catch it there. Its docs still recommend loaders, which give the right status code and the right types for the loader’s data.

Can beforeLoad throw it too? Yes. The Not Found Errors guide (opens in a new tab) names both beforeLoad and loader as places to throw notFound().

Conclusion

Throw notFound() from the route’s loader, where TanStack Start learns about it before rendering. The 404 page then comes with the status code 404, the terminal stays quiet, and the component always gets a book.

Additional Resources

TanStack, React and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by TanStack.

0 comments