solidjs / solid-start

SolidStart, the Solid app framework
https://start.solidjs.com
MIT License
4.93k stars 371 forks source link

[Bug?]: Cannot get response returned by a server function via a GET request #1550

Closed Tommypop2 closed 1 week ago

Tommypop2 commented 1 week ago

Duplicates

Latest version

Current behavior 😯

Making a GET request to the correct endpoint with serverFunction.url, as well as the args query param results in an error:

{
  "statusCode": 500,
  "stack": []
}

Expected behavior 🤔

The response from the server function should be sent verbatim to the client.

Steps to reproduce 🕹

Steps:

  1. Load https://stackblitz.com/edit/github-5jq8hh?file=src%2Froutes%2Findex.tsx
  2. Observe the logged server error

Context 🔦

I have attempted to fix this myself by patching Start: I rewrote this image To this image

This fixes the issue, but overwrites behaviour that I honestly don't fully understand.

Your environment 🌎

No response

ryansolid commented 1 week ago

There is a really awkward problem here and it is that somewhere lower in the stack.. trailing / versus not has different behavior most notable though is that we need to omit the trailing slash in POST, and include them in GET. The standard server function assumes the URL is for POST endpoint and returns without the trailing slash. So you need to grab the URL off the GET.. Either by using our wrapper function for TS (the public API).. GET(serverFunction).url with GET imported from "@solidjs/start" or by using the internal serverFunction.GET.url. I believe with this you don't need send args unless you intend to.

So the plan will be you could do something like:

const serverFunction = () => {
  'use server';
  return new Response('Hello', { headers: [["X-Content-Raw", "true"]]});
};
export default function Home() {
  onMount(async () => {
    const res = await fetch(serverFunction.GET.url);
    console.log(await res.text());
  });

Generally though I recommend wrapping the serverFunction itself with GET() as it is unlikely you expect different callers to change the HTTP method but just putting that out.

Anyway in the course of setting the raw response I found issues with GET being wrapped by HMR proxy which stopped URL from coming through. So yeah it won't work until the next release because of a number of bugs but I think I got this figured out now.