immer icon indicating copy to clipboard operation
immer copied to clipboard

Edge Runtime support

Open rpavlovs opened this issue 3 years ago • 1 comments

🙋‍♂ Question

I'm trying to use immer in Vercel's Edge Functions (not directly but as a redux-toolkit dependency) and getting the following error:

./node_modules/immer/dist/immer.esm.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Middleware middleware

Upon further look, it seems that Immer is using new Function call, which is not allowed in Edge Runtime

Is it possible to make Immer work in this runtime?

Environment

  • Immer version: 9.0.15

rpavlovs avatar Jul 05 '22 22:07 rpavlovs

Afaik we don't use new Function, couldn't find it in either source or compiled version, so probably something else is wrong, or something called by Immer, e.g. a recipe, is using it. https://unpkg.com/[email protected]/dist/immer.esm.js

mweststrate avatar Jul 06 '22 09:07 mweststrate

@mweststrate Here is a repo with a reproduction: https://github.com/revmischa/amplify-ssr-next-middleware-repro

Note that this repo is specifically pointing out the lack of Edge Runtime support for AWS Amplify and it's libraries (as filed here https://github.com/aws-amplify/amplify-js/issues/9145), but since immer is a dependencies of one or more libraries Amplify or the aws-sdk depends on, the error reported originally for this issue appears in the details of the README for the reproduction.

Screen Shot 2022-10-28 at 11 15 45 AM

A minimum reproduction would just be to include immer in the middleware.js of a Next.js project.

kevinold avatar Oct 28 '22 16:10 kevinold

Hey @kevinold, I'm not sure how to address the above issue, as, as you might check the referred immer.esm.js file, there is no eval, dynamic function construction or WebAssembly in there.

The only thing I can think of that errors, is that Immer by default uses Proxies, which are kinda dynamic. That can be disabled however by calling enableES5() at the start of your application and before first Immer usage: https://immerjs.github.io/immer/installation#pick-your-immer-version. Otherwise it would need a deeper investigation why your Edge runtime thinks there is dynamic code evaluation in there.

mweststrate avatar Oct 28 '22 20:10 mweststrate

FWIW, it seems the error can be suppressed with this config: https://nextjs.org/docs/api-reference/edge-runtime#unsupported-apis. If Immer still works after that, it might be a false positive.

mweststrate avatar Oct 28 '22 20:10 mweststrate