TanStackT
TanStack2mo ago
16 replies
welcome-indigo

getRequestHeaders() returns empty object {} when using Cloudflare plugin

Hi, I'm running into an issue with TanStack Start and the Cloudflare adapter.

I'm trying to access request headers inside my server middleware (for Better Auth), but getRequestHeaders() returns an empty object {} whenever the Cloudflare plugin is enabled in vite.config.ts.

If I comment out the cloudflare() plugin, getRequestHeaders() works perfectly and returns the correct headers.

My Setup:

* TanStack Start
* @cloudflare/vite-plugin
* Better Auth

middleware.ts

export const authMiddleware = createMiddleware().server(async ({ next }) => {
  console.log(getRequestHeaders()); // Returns {} when Cloudflare plugin is active
  
  const { data: session } = await getSession({
    fetchOptions: {
      headers: getRequestHeaders(), // Fails because headers are empty
    },
  });

  return next({ context: { user: session?.user } });
});


vite.config.ts

plugins: [
  // ... other plugins
  // If I comment this out, getRequestHeaders works fine:
  cloudflare({ viteEnvironment: { name: 'ssr' } }), 
],


Has anyone else experienced getRequestHeaders context being lost in the Cloudflare SSR environment? Is there a different way I should be accessing the request object when deploying to Cloudflare?

Thanks!
image.png
image.png
image.png
Was this page helpful?