TanStack / TanStack/router

middleware server is not sending context to loader and beforeLoad

Open
#6,395 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Router

Describe the bug

Server middleware returning context values cannot be accessed on the loader or beforeLoad

Your Example Website or App

https://github.com/cjoecker/tanstack-error-reproduction

Steps to Reproduce the Bug or Issue

Create a route with context like this:

import { createFileRoute } from "@tanstack/react-router";
import { createMiddleware } from "@tanstack/react-start";

const authMiddleware = createMiddleware().server(async ({ next }) => {
	return next({
		context: {
			auth: {
				isAuthenticated: true,
			},
		},
	});
});

export const Route = createFileRoute("/$locale/_auth")({
	server: {
		middleware: [authMiddleware],
	},
	loader: ({ context }) => {
		console.log(context);
	},
});

See in the server log that isAuthenticated=false

Expected behavior

The server log shows isAuthenticated=true

Screenshots or Videos

No response

Platform
    "@tanstack/react-devtools": "^0.7.0",
    "@tanstack/react-form": "^1.0.0",
    "@tanstack/react-router": "^1.132.0",
    "@tanstack/react-router-devtools": "^1.132.0",
    "@tanstack/react-router-ssr-query": "^1.131.7",
    "@tanstack/react-start": "^1.132.0",
    "@tanstack/router-plugin": "^1.132.0",
Additional context

PS: I couldn't find in the docs if beforeLoad runs in the client or the server. It would be nice to have better docs for that function

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction app and trace the server middleware context into the route loader and beforeLoad entry points. Confirm how beforeLoad runs, then verify that the middleware's auth.isAuthenticated value reaches both consumers and that the behavior is covered by a test or documented reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
full-stack
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.