unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.85k stars 493 forks source link

vercel-edge functions error #1262

Open LarchLiu opened 1 year ago

LarchLiu commented 1 year ago

Environment

node: v18.15.0 nitro: 2.4.1 nitro.config.ts:

export default defineNitroConfig({
  esbuild: {
    options: {
      target: 'esnext',
    },
  },
  prerender: {
    crawlLinks: false,
    routes: ['/'],
  },
})

package.json

{
  "private": true,
  "packageManager": "pnpm@8.5.1",
  "scripts": {
    "dev": "nitro dev",
    "build": "nitro build",
    "build:vercel": "NITRO_PRESET=vercel-edge nitro build",
    "build:netlify": "NITRO_PRESET=netlify-edge nitro build",
    "build:deno": "NITRO_PRESET=deno nitro build",
    "preview": "node .output/server/index.mjs"
  },
  "dependencies": {
    "@starnexus/core": "^0.0.7-beta.4",
    "@supabase/supabase-js": "^2.22.0"
  },
  "devDependencies": {
    "@antfu/eslint-config": "^0.38.6",
    "eslint": "^8.40.0",
    "nitropack": "^2.4.1",
    "typescript": "^5.0.4"
  }
}

vercel.json

{
  "buildCommand": "pnpm build:vercel",
  "framework": "nuxtjs",
  "outputDirectory": ".vercel"
}

Reproduction

repo Deploy on Vercel and access /api/test.

error link

Describe the bug

I use the vercel-edge preset to build a nitro app, there are two api routes /api/test and /api/card. If i add some code to /api/card, all edge functions will crash. However, if i use the netlify-edge, it works well.

https://github.com/LarchLiu/nitro-starter/blob/f7aaa176b6f0e97219873af44cfd5023ffd4e295/routes/api/card.ts#L9

// if add this line, all edge functions will crash
const imgStorage = new SupabaseImageStorage(imgCfg)

Additional context

No response

Logs

TypeError: Cannot read properties of undefined (reading 'bind')
    at (index.mjs:1:201899)
    at (__nitro:middleware.js:1:17)
[GET] /api/test reason=EDGE_FUNCTION_INVOCATION_FAILED, status=500, user_error=true
1bye commented 1 year ago

Same error with: Nuxt Supabase

Hebilicious commented 1 year ago

Could that be related to supabase compatibility with workerd ? Ie you'd need to pass the correct fetch function to the supabase client https://github.com/supabase/supabase-js

import { createClient } from '@supabase/supabase-js'

// Provide a custom `fetch` implementation as an option
const supabase = createClient('https://xyzcompany.supabase.co/', 'public-anon-key', {
  global: {
    fetch: (...args) => fetch(...args),
  },
})
anthonyfranc commented 11 months ago

Could that be related to supabase compatibility with workerd ? Ie you'd need to pass the correct fetch function to the supabase client https://github.com/supabase/supabase-js

import { createClient } from '@supabase/supabase-js'

// Provide a custom `fetch` implementation as an option
const supabase = createClient('https://xyzcompany.supabase.co/', 'public-anon-key', {
  global: {
    fetch: (...args) => fetch(...args),
  },
})

Ran into the same issue, as soon as i removed

nitro: { preset: 'vercel-edge', },

From nuxt.config.ts the build ran fine, so I don't think it has to do with nuxt supabase?

alexmercier25 commented 11 months ago

same here, edge functions just does not work with nitro:

TypeError: Cannot read properties of undefined (reading 'fd')
    at (index.mjs:1:177034)
    at (index.mjs:1:170819)
    at (index.mjs:1:198941)
    at (index.mjs:1:199950)
    at (__nitro:middleware.js:1:17)

It works with serverless functions but i would like to use edge functions