KINDERAS.COM

Sat Oct 08 2022

Quicktip: Integrating with Sanity from a project running on Netlify Edge

When querying Sanity data in a website project like Next.js or Sveltekit you would in most cases use the official Sanity Javascript client. If you however want to run your website on Netlify Edge functions, you can't acctually use the official Sanity Javascript client.

Background

The reason for this is that Netlify workers is running on Deno, not Node like "regular" serverless functions. The official Sanity client builds in support for using stuff like XHR for older browsers, features that Netlify Edge functions doesn't know about. Hence the official Sanity client will cause a Netlify Edge function to crash.

You can read more about what Netlify Edge function supports here

Note that you might be able to polyfill XHR and other js features not supported in Netlify Edge functions, but for this use case you don't need to.

The solution

Swap the official Sanity js client for picosanity. This client is written for modern browsers only and works well with both the Netlify Edge runtime and the Cloudflare workers runtime in my experience. Most notably it doesn't rely on XHR.

Note that picosanity only supports performing queries.

Links