[TRI-863] Support Vercel edge functions and Cloudflare functions
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
And Supabase/Deno edge functions
Is the fetch the only thing holding back deno and the like? That sounds like a worthwhile change!
Looking forward to this.
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;
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.
node:process, node:crypto, node:events cannot be used with nextjs edge runtime
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.
In a similar fashion, the https://github.com/unjs/std-env/ package exposes a Platform-Agnostic env.
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]"
}
}
}