跳到正文
博客

EvalError: Code generation from strings disallowed (Cloudflare)

Contentful works on Vercel and Netlify but throws this EvalError on Cloudflare Pages. Why the Workers runtime rejects eval, and the fixes to try.

The Liite team阅读约 7 分钟

Short answer

Something in your Cloudflare Pages code calls eval() or new Function(), and the Workers runtime blocks both. First check your Pages project's compatibility date: eval during startup is only allowed by default from 2025-06-01. If the call happens inside a request, keep the Contentful SDK out of the Workers runtime by fetching at build time or calling Contentful's REST API with plain fetch.

Your site pulls content from Contentful, it runs fine on Vercel or Netlify, and on Cloudflare Pages it fails with this:

EvalError: Code generation from strings disallowed for this context

Why the Workers runtime rejects it

Pages Functions run on Cloudflare Workers. Pages Functions allows you to build full-stack applications by executing code on the Cloudflare network with Cloudflare Workers. Cloudflare's docs say that, for security reasons, eval() and new Function are not allowed. Vercel and Netlify functions run on Node.js by default, where both work, so the same bundle passes there and fails here.

There are two situations, and the fix differs:

  • Eval at startup. Code that runs when the script first loads, such as a top-level new Function() inside a dependency. Cloudflare has a compatibility flag for this. The allow_eval_during_startup flag is the default as of 2025-06-01, and with it set, Workers can use eval() and new Function(text) during the startup phase. With disallow_eval_during_startup set, using them during startup throws an error. A Pages project with an older compatibility date gets the old behavior.
  • Eval at request time. Code that runs inside a handler or while rendering a route. The flag does not cover this. The call has to be removed or moved out of the Workers runtime.

Which package makes the call is the open question. A Cloudflare community thread from late 2024 describes someone who could not pull Contentful data on Pages while the project worked on Vercel and Netlify. Someone bumped it in January 2025 with "same issue". People in the thread suspect the browser build of the Contentful JavaScript SDK (contentful.js) or one of its dependencies, possibly through setImmediate or eval. We could not find a confirmed root cause or an official fix there, so treat the SDK as a likely suspect, not a proven one. Your own stack trace is the real evidence.

CauseSymptomFix
Startup-time eval, old compatibility dateFails on first request or at deploySet compatibility date to 2025-06-01 or later
Request-time eval in the SDK or a dependencyFails only when a route calls ContentfulFetch at build time, or use fetch directly
Wrong package build picked by the bundlerBrowser build appears in the server bundleCheck how your framework resolves the package
Missing Node API (such as setImmediate)Different error text, same routeTry nodejs_compat

Fixes, most likely first

1. Find the file that calls eval

  1. Run the project locally with wrangler pages dev, or open the failing request in your Pages deployment logs.
  2. Read the stack trace and note which package contains the eval or new Function call.
  3. Cloudflare notes that enabling source maps makes captured exception stacks point to the original files and lines, so turn them on if the trace shows only minified code. Framework builds must generate maps as well as upload them.

If the trace points at a package other than Contentful, the fix belongs to that package, and the steps below still apply.

2. Raise the compatibility date

  1. Open your Pages project settings and find the Functions compatibility date. If you use a Wrangler config file, check compatibility_date there.
  2. Set it to 2025-06-01 or later, or add the allow_eval_during_startup flag explicitly. The flag is documented on Cloudflare's compatibility flags page.
  3. Redeploy to a preview branch and test the route.

Test before production

A newer compatibility date turns on every compatibility change up to that date, not just this one. Check a preview deployment first.

If the error persists after this, the eval call is happening at request time, so go on.

3. Fetch at build time

If your content only needs to change when editors publish, call Contentful while the site builds and ship static HTML or JSON. The SDK then never runs on Workers. Most frameworks have a static output mode or a way to pre-render routes. Add a deploy hook or rebuild-on-publish so editors see their changes.

4. Call the Content Delivery API with fetch

Contentful's delivery API is plain HTTPS, and fetch works in Workers. The request shape is:

GET https://cdn.contentful.com/spaces/<SPACE_ID>/environments/<ENVIRONMENT>/entries?content_type=<TYPE>
Authorization: Bearer <DELIVERY_TOKEN>

Use the Content Delivery token, not a management token, and store it as an environment variable in the Pages project. Cloudflare's docs explain that environment variables attach text strings or JSON values to your Worker, but they also note that text strings and JSON values are not encrypted, so use a secret for anything sensitive. Without the SDK you lose its helper for resolving linked entries, so follow the includes in the response yourself.

5. Update the SDK and your framework adapter

Upgrade contentful and the adapter your framework uses for Cloudflare, then read the release notes for anything about Workers, edge runtimes or fetch. Behavior differs between SDK versions, so a fix may already exist for yours.

6. Try nodejs_compat only for missing Node APIs

Workers have a nodejs_compat flag that exposes many Node built-ins. It can help if the trace shows a missing API such as setImmediate. It does not lift the eval restriction, so skip it when the trace shows eval or new Function.

7. Check which build of the package your bundler picks

Some packages ship separate browser, Node and edge builds. If the browser build lands in your server bundle, the framework's resolution settings for the Cloudflare target are the place to look. This is framework-specific, so leave it for last.

What won't help

  • Changing the Contentful token or space. A bad token returns an HTTP 401 from Contentful, not an EvalError.
  • Changing the Node version or build command. The build succeeds. The failure happens when the code runs on Workers.
  • Looking for a switch that re-enables eval everywhere. The startup flag above is the only related setting we found in Cloudflare's docs, and it doesn't cover request-time calls.
  • Moving the Contentful call to another file. If it still runs inside a Pages Function or an edge-rendered route, it hits the same rule.

Checklist before you try again

  • The stack trace names the file that calls eval or new Function.
  • The compatibility date is 2025-06-01 or later, or the startup flag is set.
  • The Contentful call runs at build time, or uses fetch and the delivery token.
  • contentful and your Cloudflare adapter are on current versions.
  • You tested on a preview deployment before production.

If nothing works

Post a small repro to the Cloudflare community or open an issue on the contentful package's repository: one route that calls Contentful and nothing else. Include the exact error, the stack trace with source maps, your compatibility date and flags, the framework and adapter versions, and the contentful version. For a platform problem, check Cloudflare's status page first.

Subject: EvalError on Cloudflare Pages when fetching from Contentful

Error: EvalError: Code generation from strings disallowed for this context
Where: Pages Function / route <route name>, deployment ID <id>, time <UTC timestamp>
Setup: <framework + adapter version>, contentful <version>, compatibility date <date>, flags <flags>
Works on: Vercel and Netlify
Tried: raised compatibility date, updated SDK, nodejs_compat
Stack trace (source maps on): <paste>
Please advise which dependency calls eval and how to avoid it on Workers.

Common questions

Why does it work on Vercel and Netlify?

Their functions run on Node.js by default, which allows eval and new Function. Workers blocks them for security reasons, apart from the startup-phase allowance described above.

Can I call Contentful from the browser instead?

Yes, if the token you expose is the read-only Content Delivery token and you accept that visitors can see it. Browser code isn't subject to the Workers rule, but search engines may see an empty page, so build-time fetching is usually better.

Does the startup flag fix request-time eval?

No. It only covers the startup phase of a Worker script, per Cloudflare's docs. Request-time calls need a code change or a different fetch path.

Is the Contentful SDK definitely the cause?

Not proven. The community thread suspects it, but the stack trace in your own project decides. A different dependency in your framework could be the one calling eval.