Node and native npm compatibility
We’re excited to announce that Edge Functions now natively helps npm modules and Node built-in APIs. You’ll be able to straight import thousands and thousands of fashionable, generally used npm modules into your Edge Capabilities.
_10
import { drizzle } from 'npm:drizzle-orm/node-postgres'
Migrate current Node apps to Edge Capabilities
You’ll be able to migrate your current Node apps to Supabase Edge Capabilities with minimal adjustments.
We created a demo to point out methods to migrate a Node app that makes use of Specific, Node Postgres, and Drizzle. For extra info on utilizing npm modules and Node built-ins inside your Edge Capabilities, see the Managing dependencies guide.
How npm modules work underneath the hood
We run an open supply Deno server for internet hosting Edge Capabilities referred to as Supabase Edge Runtime. This practice model helps us hold Edge Capabilities working the identical method regardless of the place it’s deployed – on our hosted platform, in native improvement, or in your self-hosted setting.
The largest problem when including npm help was discovering an strategy that may work throughout all environments. We wished to maintain the workflow near the Deno CLI expertise. It ought to be attainable to import npm modules straight in your supply code with out an additional construct step.
When deploying a Operate, we serialize its module graph right into a single file format (an eszip). Within the hosted setting, all module references are then loaded from the eszip. This prevents any additional latency in fetching modules and potential conflicts between module dependencies.
We used the eszip module loader within the native and self-hosted environments too, so we solely must implement one module-loading technique for all environments. As a further profit for native improvement, this strategy avoids potential conflicts with npm modules put in within the consumer’s system for the reason that Edge Operate’s npm modules are self-contained inside the eszip.
Refactoring the module loader fixes a number of different bugs, such edge functions erroring out when an deno.lock
file is already current within the undertaking.
Just a few different stuff you’ve requested for…
Regional Invocations
You now have the choice to specify a area when working an Edge Operate (maybe we should always change the identify sooner or later). Often, Edge Capabilities run within the area closest to the consumer invoking the Operate. Nevertheless, generally you wish to run it nearer to your Postgres database or one other third get together API for optimum efficiency.
Capabilities are nonetheless deployed to all areas. Nevertheless, throughout invocation, you possibly can present the x-region
header to limit the execution to a selected area.
_10
# https://supabase.com/docs/guides/capabilities/deploy#invoking-remote-functions
_10
curl --request POST 'https://<project_ref>.supabase.co/capabilities/v1/hello-world'
_10
--header 'Authorization: Bearer ANON_KEY'
_10
--header 'Content material-Sort: utility/json'
_10
--header 'x-region: eu-west-3'
_10
--data '{ "identify":"Capabilities" }'
Higher Metrics
We have added extra metrics within the Edge Capabilities part of the Supabase Dashboard: it now exhibits CPU time and reminiscence used. We have additionally damaged down invocations by HTTP standing codes.
These adjustments show you how to spot any points together with your Edge Capabilities and act on them.
Monitor errors with Sentry
Our mates at Sentry lately shipped an official Sentry SDK for Deno. With this, it is now simple to trace errors and exceptions in your edge capabilities in Sentry.
Right here is an easy instance of methods to deal with exceptions inside your operate and ship them to Sentry.
_31
import * as Sentry from 'https://deno.land/x/sentry/index.mjs'
_31
// Efficiency Monitoring
_31
tracesSampleRate: 1.0,
_31
// Set sampling fee for profiling - that is relative to tracesSampleRate
_31
profilesSampleRate: 1.0,
_31
// Set area and execution_id as customized tags
_31
Sentry.setTag('area', Deno.env.get('SB_REGION'))
_31
Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'))
_31
Deno.serve(async (req) => {
_31
const { identify } = await req.json()
_31
message: `Howdy ${identify}!`,
_31
return new Response(JSON.stringify(information), { headers: { 'Content material-Sort': 'utility/json' } })
_31
Sentry.captureException(e)
_31
return new Response(JSON.stringify({ msg: 'error' }), {
_31
headers: { 'Content material-Sort': 'utility/json' },
What’s subsequent
NPM help was one of the crucial requested options for Edge Capabilities. When you could not use Edge Capabilities beforehand due to the dearth of help, we hope this replace will entice you to try it again. When you run into any points, we’re simply one support request away.
For current Edge Capabilities customers, regional invocations, higher metrics, and error dealing with are only a glimpse of what is going to come subsequent. We proceed to iterate on platform stability and setting customized limits on assets Edge Capabilities can use. Be careful for one more weblog publish within the new yr.