Skip to content

Commit bdd4d28

Browse files
add keep_names how-to page (#139)
Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent b3f1046 commit bdd4d28

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pages/cloudflare/howtos/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"dev-deploy": "Develop and Deploy",
55
"env-vars": "Environment Variables",
66
"image": "Image Optimization",
7-
"custom-worker": "Custom Worker"
7+
"custom-worker": "Custom Worker",
8+
"keep_names": "__name issues"
89
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

Comments
 (0)