nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Unable to use fetch API on Azure Static web apps

Open censujiang opened this issue 1 year ago • 7 comments

Environment

node:20 in azure static web apps

Reproduction

https://stackblitz.com/edit/github-p3avvp?file=server%2Froutes%2Findex.ts

Describe the bug

In this product environment of Azure, even if it is set to node:20, it still does not include the fetch api

https://github.com/nuxt/nuxt/issues/28611

Additional context

A valid Azure subscription is required to test Static web apps. This error only occurs in the cloud, not in the local simulator.

Logs

{
    "url": "/",
    "statusCode": 500,
    "statusMessage": "",
    "message": "[GET] \"https://www.baidu.com\": <no response> _fetch1 is not a function",
    "stack": ""
}
### Tasks

censujiang avatar Aug 20 '24 10:08 censujiang

When using the exact example from the docs, it also works

export default defineEventHandler(async (event) => {
  const data = await $fetch('https://ungh.cc/orgs/unjs/repos');
  return data;
});

image

When using ofetch directly it works

import { $fetch } from 'ofetch';
export default defineEventHandler(async (event) => {
  return await $fetch('https://unjs.io/');
});

image

I configured preset: 'azure' and executed build. The artifacts were then deployed using swa deploy .\.output\public\ --api-location .\.output\server\ --deployment-token TOKEN --api-language "node" --api-version "20" --env production.

I think you are not using fetch correct, as your reproduction already fails in stackblitz. Please check the docs about native fetch: https://nitro.unjs.io/guide/fetch

EDIT: Changed ordering of examples to make it more clear.

itpropro avatar Oct 01 '24 00:10 itpropro

$fetch (same as fetch) should work in every file without any import, this seems like a bug (but harder to reproduce, as rollup sometimes mess up with chunk orders in more complex builds)

@itpropro by your testing, if you avoid import, do you reproduce issue or not?

pi0 avatar Oct 01 '24 08:10 pi0

$fetch (same as fetch) should work in every file without any import, this seems like a bug (but harder to reproduce, as rollup sometimes mess up with chunk orders in more complex builds)

@itpropro by your testing, if you avoid import, do you reproduce issue or not?

It could also be a fetch keyword error caused by a packaging issue, but I may have time to track this down later

censujiang avatar Oct 01 '24 13:10 censujiang

$fetch (same as fetch) should work in every file without any import, this seems like a bug (but harder to reproduce, as rollup sometimes mess up with chunk orders in more complex builds)

@itpropro by your testing, if you avoid import, do you reproduce issue or not?

The second version I tested was without any imports, the ofetch one was only for the sake of testing, I should have ordered them the other way around. Both work in Static Web Apps.

itpropro avatar Oct 02 '24 07:10 itpropro

Okay, then it is most certainly a rollup chunk ordering issue.

@censujiang If can still reproduce, do you think you can share a public repository? (Nuxt setup is also acceptable in this case since it might be also needed for reproduction)

If it is not reproducible anymore I would suggest closing the issue in the meanwhile.

pi0 avatar Oct 02 '24 11:10 pi0

Okay, then it is most certainly a rollup chunk ordering issue.

@censujiang If can still reproduce, do you think you can share a public repository? (Nuxt setup is also acceptable in this case since it might be also needed for reproduction)

If it is not reproducible anymore I would suggest closing the issue in the meanwhile.

I'd like to provide a Nuxt/Nitro repository that can be used for reproduction, but I'm currently on the Chinese National Day public holiday, so it's not convenient for now. I'll talk about it after the holiday.

censujiang avatar Oct 02 '24 12:10 censujiang

No hurry thanks @censujiang!

pi0 avatar Oct 02 '24 13:10 pi0

Got it to work by passing the node 20 to the cli command:

--api-language "node" --api-version "20"

Additionally setting 20 to the preset config. Thanks everyone!

matteorchard avatar Dec 18 '24 04:12 matteorchard