nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Returning Web Response in Azure SWA is not working

Open targetlucked69 opened this issue 1 year ago • 8 comments

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

targetlucked69 avatar Jul 04 '24 07:07 targetlucked69

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?

itpropro avatar Oct 01 '24 00:10 itpropro

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.

pi0 avatar Oct 01 '24 08:10 pi0

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.

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.

itpropro avatar Oct 02 '24 08:10 itpropro

thanks for investigation @itpropro. Does runtime v4 support accept ReadableStream as valid body?

pi0 avatar Oct 02 '24 11:10 pi0

thanks for investigation @itpropro. Does runtime v4 support accept ReadableStream as valid body?

No, we can also release the bug fix beforehand independently of the v4 update.

itpropro avatar Oct 02 '24 12:10 itpropro

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 avatar Sep 17 '25 15:09 jvpelt

If you can please feel free to open a PR to upgrade SWA in v3 branch.

pi0 avatar Sep 17 '25 17:09 pi0

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.

bishsbytes avatar Oct 22 '25 08:10 bishsbytes