Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start: createServerFn cannot return raw response objects #2779

Open
nekochan0122 opened this issue Nov 16, 2024 · 2 comments
Open

Start: createServerFn cannot return raw response objects #2779

nekochan0122 opened this issue Nov 16, 2024 · 2 comments
Labels
start Everything about TanStack Start

Comments

@nekochan0122
Copy link
Contributor

Which project does this relate to?

Start

Describe the bug

Server functions should able to return Raw Response objects.

Your Example Website or App

https://github.com/nekochan0122/tanstack-start-return-response-object

Steps to Reproduce the Bug or Issue

  1. Open the dev server
  2. Click the "Trigger serverFn" button
  3. See the console output and cookies

Expected behavior

const serverFn = createServerFn({ method: "POST" }).handler(async () => {
  return new Response(JSON.stringify({ hello: "world" }), {
    status: 200,
    headers: {
      "Content-Type": "application/json",
      "Set-Cookie": "test=test",
    },
  });
});
  1. The console output is expected to contain { hello: "world" }
  2. The cookie should be set to test=test

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

@SeanCassiere SeanCassiere added types Changes to the typescript types start Everything about TanStack Start and removed types Changes to the typescript types labels Nov 16, 2024
@nekochan0122
Copy link
Contributor Author

nekochan0122 commented Nov 16, 2024

a temporary solution:

async function handleResponse<ResponseBody = unknown>(response: Response): Promise<ResponseBody> {
  const event = getEvent()

  setHeaders(event, Object.fromEntries(response.headers))

  switch (response.headers.get('Content-Type')) {
    case 'application/json':
      return response.json() as ResponseBody

    default:
      return response.body as ResponseBody
  }
}

@cpakken
Copy link

cpakken commented Dec 17, 2024

Tried https://discord.com/channels/719702312431386674/1310732156561260575/1310785299915346100

const exampleStreamingFn = createServerFn({ method: 'GET' }).handler(async () => {
  const encoder = new TextEncoder();

  // replace with ur actual stream
  const stream = new ReadableStream({
    async start(controller) {
      for (let i = 0; i < 10; i++) {
        const chunk = encoder.encode(`${i + 1}`);
        controller.enqueue(chunk);
        await new Promise((resolve) => setTimeout(resolve, 100));
      }
      controller.close();
    },
  });

  const res = new Response(stream, { status: 200 });

  return sendWebResponse(getEvent(), res);
});

and https://discord.com/channels/719702312431386674/1310732156561260575/1310785660948578355

const res = await fetch(exampleStreamingFn.url);

Which got streaming kindof working, but after stream is done im getting.

ServerFn Response: 200
 - Payload: {"result":{"$undefined":""},"context":{}}

node:_http_outgoing:699
    throw new ERR_HTTP_HEADERS_SENT('set');
          ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (node:_http_outgoing:699:11)  

and then vinxi crashes and I need to restart the server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
start Everything about TanStack Start
Projects
Status: Backlog
Development

No branches or pull requests

3 participants