Understanding status response with SvelteKit + Netlify functions

I’m doing some tests on https://staging.app.postscriptmedical.com using SvelteKit’s own “shadow” endpoints (their equivalent to Netlify’s function).

Reading their docs (kit/packages/adapter-netlify at master · sveltejs/kit · GitHub) one can see that one is able to use them just like Netlify’s own function and so far I can say that almost everything is working as expected. It’s only the HTTP status code response that I am a bit confused.

Netlify Functions require you to use statusCode on the response object, while Svelte is status. While they seem to work for a few codes: 200, 300, 400, if I try to return 204 after sucessfully deleting an Identity user, the server seems to not like it and the client receives a 500 with the following: “Response constructor: Invalid response status code. TypeError: Response constructor: Invalid response status code.”

If you look at the logs between 09:20am and 10:00am GMT today (July 15) you should see the different attempts I made with different status codes.

Does anyone have any information about this? Is this a limitation within Netlify? A potential bug in SvelteKit’s adaptor?

I understand I could just respond with 200 and it would work but 200 isn’t really the correct status for the response.

Many thanks in advance.

Hi @Postscript-Medical,

Netlify Functions support any valid HTTP Status Code, include 204. I am using it on my projects and they seem to work fine.

While, I cannot yet comment if this is a problem with SvelteKit (it’s worth confirming from them), are you able to log out the response that’s being sent? Not just the body, but the entire response object?

Hi @hrishikesh,

Thanks for looking into this. I am afraid I don’t follow when you ask me to log the response that’s being sent. The returned object the client receives is a rendered page with the error information. The endpoint that’s returning that is the endpoint where I am trying to return the 204. As the error is happening on the return part of the function I am not sure I can log the whole response out.

This is what is logged inside Netlify:

Jul 18, 11:06:41 AM: 97f1b5c8 ERROR  TypeError: Response constructor: Invalid response status code.
    at Object.webidl.errors.exception (/var/task/.netlify/shims.js:836:10)
    at initializeResponse (/var/task/.netlify/shims.js:8582:22)
    at new Response (/var/task/.netlify/shims.js:8265:6)
    at render_endpoint (/var/task/.netlify/server/index.js:215:10)
    at async resolve (/var/task/.netlify/server/index.js:1893:53)
    at async respond (/var/task/.netlify/server/index.js:1849:22)
    at async Runtime.handler (/var/task/.netlify/serverless.js:263:20)

While this is the response object:

return {
	status: 204
};

If I change that status to 200, all works correctly.

After digging around on the SvelteKit’s side I found the following: uppercase methods — export function GET, etc · Discussion #5359 · sveltejs/kit · GitHub

I made a quick test here and no longer get the 500 error but a 405 saying “Method not allowed”.

Then I made a barebones project to test the functions: https://splendorous-salamander-e8facf.netlify.app/ and here the endpoints work as expected. Which leaves me to believe something is wrong in my main project setup…

And, after further digging I got it all working. Pfew!

It turned out to be a mixture of wrong settings on config files with mistaching packaging versions. Updating SvelteKit and dependencies to their latest version as well as the config files has sorted the issue out.

@Postscript-Medical, wow! Thank you so much for coming back and sharing all of these updates as you worked through the debugging process. This will be beneficial for all future Forums members who encounter something similar.

Happy building :tada: