diff options
author | 2023-09-19 11:51:06 +0200 | |
---|---|---|
committer | 2023-09-19 11:51:06 +0200 | |
commit | 5b0b3c9a8e0c0e6b6c7472b82008ab57985f2a04 (patch) | |
tree | 168fd80a05a6a0f7ab114d99309c1ddf694cef29 | |
parent | 9b84a7ef96c27986399d989b6f14806b65c5a4e8 (diff) | |
download | astro-5b0b3c9a8e0c0e6b6c7472b82008ab57985f2a04.tar.gz astro-5b0b3c9a8e0c0e6b6c7472b82008ab57985f2a04.tar.zst astro-5b0b3c9a8e0c0e6b6c7472b82008ab57985f2a04.zip |
fix(@astrojs/cloudflare): support for nodejs_compat (#8595)
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Diffstat (limited to '')
-rw-r--r-- | .changeset/fifty-weeks-bake.md | 20 | ||||
-rw-r--r-- | packages/integrations/cloudflare/README.md | 27 | ||||
-rw-r--r-- | packages/integrations/cloudflare/src/index.ts | 24 |
3 files changed, 71 insertions, 0 deletions
diff --git a/.changeset/fifty-weeks-bake.md b/.changeset/fifty-weeks-bake.md new file mode 100644 index 000000000..714be802e --- /dev/null +++ b/.changeset/fifty-weeks-bake.md @@ -0,0 +1,20 @@ +--- +'@astrojs/cloudflare': minor +--- + +Add support for the following Node.js Runtime APIs, which are availabe in [Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/nodejs) using the `node:` syntax. + +- assert +- AsyncLocalStorage +- Buffer +- Diagnostics Channel +- EventEmitter +- path +- process +- Streams +- StringDecoder +- util + +```js +import { Buffer } from 'node:buffer'; +``` diff --git a/packages/integrations/cloudflare/README.md b/packages/integrations/cloudflare/README.md index 27e621853..0d3312ce8 100644 --- a/packages/integrations/cloudflare/README.md +++ b/packages/integrations/cloudflare/README.md @@ -202,6 +202,33 @@ This will enable Cloudflare to serve files and process static redirects without See [Cloudflare's documentation](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) for more details. +## Node.js compatibility + +Astro's Cloudflare adapter allows you to use any Node.js runtime API supported by Cloudflare: + +- assert +- AsyncLocalStorage +- Buffer +- Diagnostics Channel +- EventEmitter +- path +- process +- Streams +- StringDecoder +- util + +To use these APIs, your page or endpoint must be server-side rendered (not pre-rendered) and must use the the `import {} from 'node:*'` import syntax. + +```js +// pages/api/endpoint.js +export const prerender = false; +import { Buffer } from 'node:buffer'; +``` + +Additionally, you'll need to enable the Compatibility Flag in Cloudflare. The configuration for this flag may vary based on where you deploy your Astro site. + +For detailed guidance, please refer to the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs). + ## Troubleshooting For help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help! diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index b64d986af..24c22d8f1 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -296,6 +296,18 @@ export default function createIntegration(args?: Options): AstroIntegration { target: 'es2020', platform: 'browser', conditions: ['workerd', 'worker', 'browser'], + external: [ + 'node:assert', + 'node:async_hooks', + 'node:buffer', + 'node:diagnostics_channel', + 'node:events', + 'node:path', + 'node:process', + 'node:stream', + 'node:string_decoder', + 'node:util', + ], entryPoints: entryPaths, outdir: outputDir, allowOverwrite: true, @@ -357,6 +369,18 @@ export default function createIntegration(args?: Options): AstroIntegration { target: 'es2020', platform: 'browser', conditions: ['workerd', 'worker', 'browser'], + external: [ + 'node:assert', + 'node:async_hooks', + 'node:buffer', + 'node:diagnostics_channel', + 'node:events', + 'node:path', + 'node:process', + 'node:stream', + 'node:string_decoder', + 'node:util', + ], entryPoints: [entryPath], outfile: buildPath, allowOverwrite: true, |