Skip to content

Commit 485d9b2

Browse files
vicbconico974
andauthored
docs(cloudflare): workerd build condition (#143)
Co-authored-by: conico974 <[email protected]>
1 parent ee3bb9c commit 485d9b2

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

pages/cloudflare/howtos/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"env-vars": "Environment Variables",
66
"image": "Image Optimization",
77
"custom-worker": "Custom Worker",
8-
"keep_names": "__name issues"
8+
"keep_names": "__name issues",
9+
"workerd": "workerd specific packages"
910
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## `workerd` specific code
2+
3+
### Configuration
4+
5+
[`workerd`](https://github.com/cloudflare/workerd) is the runtime cloudflare uses to run Workers code.
6+
7+
While the [`nodejs_compat`](https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag) flag makes `workerd` mostly compatible with Node.js,
8+
there are still minor differences. Some packages publish code for different runtimes to account for those differences. For example, `postgres` has [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) in its [`package.json`](https://github.com/porsager/postgres/blob/ad0ed4476e09f41f147859cb5a42971d2b99e9c7/package.json#L8-L13):
9+
10+
```json
11+
"exports": {
12+
"types": "./types/index.d.ts",
13+
"bun": "./src/index.js",
14+
"workerd": "./cf/src/index.js",
15+
"import": "./src/index.js",
16+
"default": "./cjs/src/index.js"
17+
},
18+
```
19+
20+
With such exports, Node.js applications use either `src/index.js` or `cjs/src/index.js` depending if the app use ESM or CJS.
21+
22+
However we want to use the `workerd` specific entrypoint when using the Cloudflare adapter.
23+
For that, you need to instruct Next.js not to bundle packages as it would use the node conditions by default.
24+
25+
To do that, add those packages in the `serverExternalPackages` key of your `next.config.ts`:
26+
27+
```ts
28+
// node.config.ts
29+
import type { NextConfig } from "next";
30+
31+
const nextConfig: NextConfig = {
32+
serverExternalPackages: ["@prisma/client", ".prisma/client", "postgres"],
33+
};
34+
35+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
36+
initOpenNextCloudflareForDev();
37+
38+
export default nextConfig;
39+
```
40+
41+
### Packages known to have `workerd` specific code
42+
43+
- `postgres`
44+
- `@prisma/client` (and the generated `.prisma/client`)
45+
46+
Please report an issue on [the adapter GH repository](https://github.com/opennextjs/opennextjs-cloudflare/issues) to have packages added to this list.

0 commit comments

Comments
 (0)