inngest / inngest-js

The developer platform for easily building reliable workflows with zero infrastructure for TypeScript & JavaScript
https://www.inngest.com/
GNU General Public License v3.0
395 stars 40 forks source link

Support Vercel Edge Functions #78

Closed DaleLJefferson closed 1 year ago

DaleLJefferson commented 1 year ago

When using NextJS * Vercel Edge functions fetch is not correctly detected.

ReferenceError: XMLHttpRequest is not defined

Looking at the code https://github.com/inngest/inngest-js/blob/main/src/components/Inngest.ts#L129 it looks like if you don't provide a fetch in the constructor it falls back to using cross-fetch, which I guess thinks the edge functions are a browser and tries to use XMLHttpRequest polyfil.

As a workaround supplying fetch seems to work and I'm able to send events.

new Inngest({fetch})
jpwilliams commented 1 year ago

Hi @DaleLJefferson!

Great job for finding this. 🙌 We have #61 in the pipeline to support most edge environments generically (see src/edge.ts:30 for that fetch binding), but this is exactly how to achieve this at present.

For this, we could probably try to bind to fetch if we find it as a global to avoid you having to do this manually.

61 has a todo to add a Vercel edge function example - I'll venture to add this to inngest/sdk-example-nextjs-vercel and then we can get this all sorted with tests.

DaleLJefferson commented 1 year ago

Great to see you are working on this. I'm planning on using inngest in edge and non edge functions if that confuses things.

serve - standard node function NextJS / Vercel client - node and edge functions NextJS / Vercel

I would suggest that while the serve function is platform specific, the client should not be, it work on anywhere by detecting the environment (it should just need fetch I guess). i.e. I should not have to import import {Inngest} from "inngest/edge"

jpwilliams commented 1 year ago

Aye, the client should definitely be environment agnostic.

Will reference this with a PR to fix!

DaleLJefferson commented 1 year ago

Thanks for sorting this out ⭐