trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

[TRI-863] Support Vercel edge functions and Cloudflare functions

Open matt-aitken opened this issue 2 years ago • 9 comments

The way we currently fetch data isn't compatible (node-fetch).

More info here:

https://vercel.com/guides/library-sdk-compatible-with-vercel-edge-runtime-and-functions

From SyncLinear.com | TRI-863

matt-aitken avatar Aug 02 '23 20:08 matt-aitken

And Supabase/Deno edge functions

ericallam avatar Aug 21 '23 08:08 ericallam

Is the fetch the only thing holding back deno and the like? That sounds like a worthwhile change!

guy-borderless avatar Aug 25 '23 17:08 guy-borderless

Looking forward to this.

izakfilmalter avatar Oct 02 '23 15:10 izakfilmalter

I'm still having some issues with deploying on Cloudflare using edge runtime

When I run npx @cloudflare/next-on-pages@1 to simulate a build on Cloudflare I get the following error

Error
▲  Import trace for requested module:
▲  node:crypto
▲  ./node_modules/@trigger.dev/sdk/dist/index.mjs
▲  ./trigger.js
▲  ./app/api/trigger/route.js

It seems to be related to the crypto module.

Not too sure what i'm doing wrong.

My app/api/trigger/route.js looks like

export const runtime = 'edge';

import { createAppRoute } from "@trigger.dev/nextjs";
import { client } from "@/trigger";

import "@/jobs";

//this route is used to send and receive data with Trigger.dev
export const { POST, dynamic } = createAppRoute(client);

//uncomment this to set a higher max duration (it must be inside your plan limits). Full docs: https://vercel.com/docs/functions/serverless-functions/runtimes#max-duration
//export const maxDuration = 60;

jellohouse avatar Feb 15 '24 16:02 jellohouse

I'm observing the same error when using the trigger SDK in a Vercel edge function context:

Compiling /[id]/app
node:crypto
Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
Import trace for requested module:
node:crypto
../../node_modules/.pnpm/@[email protected]/node_modules/@trigger.dev/sdk/dist/index.mjs

Just guessing, but the root cause might be that Cloudflare Workers come with a built-in node:crypto polyfill that's missing in the Vercel edge runtime.

matthiasbayer avatar Feb 19 '24 15:02 matthiasbayer

node:process, node:crypto, node:events cannot be used with nextjs edge runtime

ImSingee avatar Mar 31 '24 11:03 ImSingee

For node:crypto, we can use https://unjs.io/packages/uncrypto (or simply the global crypto variable to use Web Crypto).

For node:process, since we only use process.env, can we just remove the import and use process.env.XXX directly?

For node:events, we can use events module instead.

ImSingee avatar Mar 31 '24 20:03 ImSingee

In a similar fashion, the https://github.com/unjs/std-env/ package exposes a Platform-Agnostic env.

maoosi avatar Mar 31 '24 23:03 maoosi

I made it works finally.

See https://github.com/ImSingee/trigger.dev/tree/v2.3.18

It works well on Cloudflare Pages and Vercel (both edge runtime).

You can modify your package.json file with this if you want to try:

{
   "dependencies": {
      "@trigger.dev/nextjs": "^2.3.18",
      "@trigger.dev/react": "^2.3.18",
      "@trigger.dev/sdk": "npm:@singee/[email protected]"
  },
  "pnpm": {
      "overrides": {
        "@trigger.dev/core-backend": "npm:@singee/[email protected]"
      }
  }
}

ImSingee avatar Apr 01 '24 01:04 ImSingee