fauna/fauna-js

Support Vercel Edge environment

BrunoQuaresma opened this issue · 6 comments

Hey folks, I'm developing an app using NextJS and I have a function that runs on the edge, and when I try to use the fauna client in this function, I get the following error:

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Import trace for requested module:
./node_modules/fauna/dist/browser/index.js
Error [ReferenceError]: navigator is not defined

I guess it is because it tries to access some browser feature that is not available 🤔

To reproduce this you create a NextJS project and inside of the app folder, create a /api/route.ts with something like:

export const runtime = "edge";

export function GET() {
    const fauna = new Client({ ... })
    fauna.query(fql`[]`)
}

Sorry for not having too much more info to help with debug 😞

macmv commented

For some extra debug context: looks like src/util/environment.ts needs to be a bit smarter, and this looks related: vercel/next.js#27006.

Thanks for the bug report!

Our internal ticket for this is: FE-3729

The heart of the issue is that we assume all "server" environments will be Node. If it's not Node, then it must be a browser or service worker. We need to catch the neither Node nor Browser case here:

if (isNode()) {
driverEnv.runtime = ["nodejs", process.version].join("-");
driverEnv.env = getNodeRuntimeEnv();
driverEnv.os = [os.platform(), os.release()].join("-");
} else if (isServiceWorker) {
driverEnv.runtime = getBrowserDetails(navigator);
driverEnv.env = "Service Worker";
driverEnv.os = getBrowserOsDetails(navigator);
} else {
driverEnv.runtime = getBrowserDetails(navigator);
driverEnv.env = "browser";
driverEnv.os = getBrowserOsDetails(navigator);
}

Reopening this until we release (should be in the next couple hours).

@BrunoQuaresma this is released you can pick it up in 0.9.2 should be on NPM and there's a Git Hub release as well.

Thank you, folks!