Skip to content

createServerFn cannot read cookie in same request after setting it #5615

Description

@axuj

Which project does this relate to?

Start

When using createServerFn, if I set a cookie using setCookie from @tanstack/react-start/server and then immediately try to read it back using getCookie within the same server function call, the cookie is not found. This behavior occurs because cookies are only available to the browser after the response is sent to the client. However, for certain use cases where you need to set and immediately validate a cookie within the same request context, this creates a limitation.

The expected behavior would be that once a cookie is set in the current request context, subsequent calls to getCookie within the same request should be able to retrieve the recently set value.

Next.js supports reading cookies immediately after setting them within the same request context.

Your Example Website or App

https://tanstack.com/start/latest/docs/framework/react/examples/start-basic?panel=code

Steps to Reproduce the Bug or Issue

  1. clone example
  2. write
export const setCookieFn = createServerFn({ method: "POST" })
	.inputValidator((d: { name: string; value: string }) => d)
	.handler(async ({ data }) => {
		const { name, value } = data;
		setCookie(name, value, {
			httpOnly: true,
			path: "/",
		});
		return { success: true, message: `Cookie ${name} set to ${value}` };
	});

export const getCookieValue = createServerFn({ method: "GET" })
	.inputValidator((d: { name: string }) => d)
	.handler(async ({ data }) => {
		const { name } = data;
		const value = getCookie(name);
		console.log(`Getting cookie ${name}:`, value);
		return { name, value: value || null };
	});

export const tsetFull = createServerFn({ method: "POST" })
	.inputValidator((name: string) => name)
	.handler(async ({ data }) => {
		await setCookieFn({
			data: {
				name: "name",
				value: data,
			},
		});
		const res = await getCookieValue({
			data: {
				name: "name",
			},
		});
		return res;
	});

Expected behavior

When calling tsetFull with input value "test1", it should return { name: "name", value: "test1" }. The cookie set in the setCookieFn call should be immediately readable by the subsequent getCookieValue call within the same server function execution context.

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.133.27
  • OS: Windows
  • Browser: edge
  • Browser Version: 141.0.3537.92
  • Bundler: Vite
  • Bundler Version: 7.1.12

Additional context

Anonymous login system that sets a cookie when entering the page, which is then used for authentication in subsequent workflow operations to read/write data for the anonymous user.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requeststartEverything about TanStack Start

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions