|
| 1 | +import { Callout } from "nextra/components"; |
| 2 | + |
| 3 | +## `__name` issues |
| 4 | + |
| 5 | +When using the OpenNext adapter, Wrangler processes the worker's code with [esbuild](https://esbuild.github.io/), and by default |
| 6 | +enables the [`keep-names`](https://esbuild.github.io/api/#keep-names) option. While this is generally useful for debugging, it can |
| 7 | +cause issues with certain Next.js libraries (e.g. [next-themes](https://www.npmjs.com/package/next-themes)) that convert scripts |
| 8 | +to strings. This happens because `esbuild` introduces a `__name` function at the top of modules, which may inadvertently appear |
| 9 | +in the generated script strings. When these strings are evaluated at runtime, the `__name` function might therefore not be defined, |
| 10 | +leading to errors logged in the developper console: |
| 11 | + |
| 12 | +``` |
| 13 | +Uncaught ReferenceError: __name is not defined |
| 14 | +``` |
| 15 | + |
| 16 | +<Callout> |
| 17 | + Note that depending on your minification settings, the `__name` identifier might be minified, making the |
| 18 | + error message less clear and potentially not explicitly mentioning `__name` in such cases. |
| 19 | +</Callout> |
| 20 | + |
| 21 | +### How to fix such issues |
| 22 | + |
| 23 | +To fix the issue you can simply set the `keep_names` option to `false` in your [`wrangler.jsonc` file](https://developers.cloudflare.com/workers/wrangler/configuration), like in the following example: |
| 24 | + |
| 25 | +```jsonc |
| 26 | +{ |
| 27 | + "$schema": "node_modules/wrangler/config-schema.json", |
| 28 | + "main": ".open-next/worker.js", |
| 29 | + "name": "my-app", |
| 30 | + "keep_names": false, |
| 31 | + /* ... */ |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +One potential drawback of this solution is that, depending on your minification settings, you may lose the ability to view the original |
| 36 | +function names in debugging tools. |
| 37 | + |
| 38 | +<Callout>You must use Wrangler version `4.13.0` or later to use this option.</Callout> |
0 commit comments