immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Edge Runtime support

rpavlovs opened this issue Β· 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

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/immer@9.0.15/dist/immer.esm.js

@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 aws-amplify/amplify-js#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.

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.

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.