TanStackT
TanStack10mo ago
11 replies
conventional-black

Setting/Getting headers does not work?

I've a simple component with a server Fn that will take data and then apply some of that data to the redirect request.

const serverFn = createServerFn({ method: 'GET' })
    .validator((data: PostMessageData) => data)
    .handler(async (ctx) => {
        throw redirect({
            to: '/screens/hmm',
            headers: { 'X-testing-with': 'tesing' },
            statusCode: 301,
        });
    });

function RouteComponent() {
    const srv = useServerFn(serverFn);
    const data = usePostMessage();

    useEffect(() => {
        if (!data) return;
        srv({ data });
    }, [data]);

    return <div>Hello "/screens/"!</div>;
}


Then I retrieve the headers from within the server Fn on the redirected page:
export const Route = createFileRoute('/screens/hmm')({
    loader(ctx) {
        serverFn();
    },
    component: RouteComponent,
});

const serverFn = createServerFn().handler(async (ctx) => {
    const headers = getHeaders();
    console.log(headers);
});


But no headers appear when I try get them, Am I misunderstanding the purpose of the redirect?
Was this page helpful?