Skip to content

middleware server is not sending context to loader and beforeLoad #6395

@cjoecker

Description

@cjoecker

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

I couldn't finde a stackblitz with Tanstack Start. When I tried to install it, I get a lot of type errors.

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 log that isAuthenticated=false

Expected behavior

The 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

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