Azure / Azure/azure-functions-nodejs-library

Azure Static Web Apps - Deploying Svelte App with SSR

Open
#290 3 comments 0 reactions 0 assignees View on GitHub
area:nodejs-functions
Dominant language
TypeScript
Stars
70
Forks
35
Avg merge
4d 18h
Merged PRs (30d)
6

Description

#### Investigative information

I have a Sveltekit app and I am using `https://github.com/geoffrich/svelte-adapter-azure-swa` adapter to deploy to Azure Static Web App. The app has an API built-in (`/chat/api`) and I am calling it from the front end. The API makes a call to Azure OpenAI and streams the data back to front end. The frontend loads fine, however when it makes the call to the API it returns an error (screenshot below). I am able to replicate the issue locally on the emulator using SWA CLI and I see below error.

![error](https://github.com/user-attachments/assets/a4fef631-b367-438f-90e3-b21d2888d5e0)

#### Repro steps

Under `src/routes/chat/api`, have a `+server.ts` file with a POST endpoint which is something like this.

```ts
export const POST: RequestHandler = async ({ request }) => {
const openAIClient = new OpenAIClient(env.AZ_OPENAI_ENDPOINT, new AzureKeyCredential(env.AZ_OPENAI_TOKEN));
const events = await.openAIClient.streamChatCompletions(env.AZ_OPENAI_DEPLOYMENT_NAME, messages, {
...
});
const stream = new ReadableStream({
async start(controller) {
...
controller.close();
}
});

return new Response(stream, {
status: 200,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
Connection: 'keep-alive'
}
});
}
```

Start the app using `swa` CLI (`swa start`), and using any REST Client, make a call to local API and you get the above error.

I have following SWA CLI config for local

```json
{
"$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
"configurations": {
"km-svelte": {
"appLocation": "./",
"apiLocation": "build/server",
"outputLocation": "build/static",
"apiLanguage": "node",
"apiVersion": "18",
"appBuildCommand": "npm run build",
"run": "npm run dev",
"verbose": "true",
"platform": {
"apiRuntime": "node:20"
}
}
}
}
```

#### Expected behavior

The data is streamed back to the front end/client.

#### Actual behavior

Error as below

```
[2024-08-10T11:39:47.676Z] Executing 'Functions.sk_render' (Reason='This function was programmatically called via the host APIs.', Id=915b21cb-c9b5-4992-b0b2-5ffd012c2f46)
[2024-08-10T11:39:49.299Z] Executed 'Functions.sk_render' (Failed, Id=915b21cb-c9b5-4992-b0b2-5ffd012c2f46, Duration=1629ms)
[2024-08-10T11:39:49.299Z] System.Private.CoreLib: Exception while executing function: Functions.sk_render. System.Private.CoreLib: Result: Failure
[2024-08-10T11:39:49.299Z] Exception: Received non-Uint8Array chunk
[2024-08-10T11:39:49.299Z] Stack: TypeError: Received non-Uint8Array chunk
[2024-08-10T11:39:49.299Z] at readAllBytes (node:internal/deps/undici/undici:4417:17)
[2024-08-10T11:39:49.299Z] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[2024-08-10T11:39:49.299Z] at async fullyReadBody (node:internal/deps/undici/undici:4381:22)
[2024-08-10T11:39:49.299Z] at async consumeBody (node:internal/deps/undici/undici:5463:7)
[2024-08-10T11:39:49.299Z] at async toResponse (/Users/utkarsh/_DEV/GitHub/onlyutkarsh/km/build/server/sk_render/index.js:162301:34)
[2024-08-10T11:39:49.299Z] at async Object.index4 (/Users/utkarsh/_DEV/GitHub/onlyutkarsh/km/build/server/sk_render/index.js:162277:20).
POST http://localhost:4280/api/__render - 500
```

#### Known workarounds

Provide a description of any known workarounds.

#### Related information

Provide any related information

- Static Web App Hosting Plan: Standard
- Programming language used: `SvelteKit and Typescript`
- Core Tools Version: `4.0.5907 Commit hash: N/A +807e89766a92b14fd07b9f0bc2bea1d8777ab209 (64-bit)`
- Function Runtime Version: `4.834.3.22875`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.