Returning Web Response in Azure SWA is not working
Environment
Nuxt: 3.12.3 Node: 20
Reproduction
Deploy a basic Nuxt app with an api that returns a web response like
export default function eventHandler((event) => {
return new Response(JSON.stringify({ username: 'test' }), { headers: { 'Content-Type': 'application/json' } })
})
Describe the bug
Instead of the actual json, Im getting an empty object returned when deployed to Azure SWA:
// /api/user
{}
Additional context
No response
Logs
No response
I was able to reproduce this. But there are alternative options to construct and send a response that work with SWA and are more "Nitro like":
Directly returning the object (Nitro automatically sets the headers):
export default defineEventHandler(() => {
return { username: 'test' }
})
Using the send function:
export default defineEventHandler((event) => {
return send(event, JSON.stringify({ username: 'test' }), 'application/json')
// also possible
// return send(event, { username: 'test' }, 'application/json')
})
Both are tested and confirm to work with Azure Static Web Apps.
Do you want to keep this issue open @pi0 or would you agree not to use the native Response object and instead use the Nitro utilities?
Returning a new Response should be supported in handlers. We should investigate why body (which can be a stream) is not handled, it is a bug.
Returning a new
Responseshould be supported in handlers. We should investigate whybody(which can be a stream) is not handled, it is a bug.
I verified that this is also a bug in the azure-function preset.
The actual return value from API call is this, but Azure Static Web App filters that out:
Body: [object ReadableStream]
Content-Type: application/json; charset=utf-8
It is definitely cause by Nitro handling the response binding for Azure Functions.
context.res = {
status,
cookies: getAzureParsedCookiesFromHeaders(headers),
headers: normalizeLambdaOutgoingHeaders(headers, true),
body,
};
We should address that with the update to Azure Functions runtime 4.
thanks for investigation @itpropro. Does runtime v4 support accept ReadableStream as valid body?
thanks for investigation @itpropro. Does runtime v4 support accept
ReadableStreamas validbody?
No, we can also release the bug fix beforehand independently of the v4 update.
Hi @pi0,
I’m running into something similar with a “tanstack start” app I’m trying to deploy to a SWA. Right now it returns a ReadableStream, but the azure functions v3 programming model doesn’t support that. A quick workaround could be to check the body type and turn the stream into a string. I can open a PR for that if you’d like.
On the other hand, v4 does support streams. I’ve seen a few issues about upgrading to v4, but I’m not really sure what the current status is.
If you can please feel free to open a PR to upgrade SWA in v3 branch.
Hi @pi0,
I’m running into something similar with a “tanstack start” app I’m trying to deploy to a SWA. Right now it returns a ReadableStream, but the azure functions v3 programming model doesn’t support that. A quick workaround could be to check the body type and turn the stream into a string. I can open a PR for that if you’d like.
On the other hand, v4 does support streams. I’ve seen a few issues about upgrading to v4, but I’m not really sure what the current status is.
@jvpelt did you find a workaround for this? I'm hitting the same issue with Tanstack Start. I'm just getting a {} response.