From 114642128d8531f05781e811e480b66b98521d9e Mon Sep 17 00:00:00 2001 From: Adrian Lyjak Date: Tue, 7 May 2024 16:04:55 -0400 Subject: [PATCH] Updated docs for https://github.com/withastro/adapters/pull/251 --- .../guides/integrations-guide/cloudflare.mdx | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/content/docs/en/guides/integrations-guide/cloudflare.mdx b/src/content/docs/en/guides/integrations-guide/cloudflare.mdx index 482ad86839a27..d5e3bcc9feb3d 100644 --- a/src/content/docs/en/guides/integrations-guide/cloudflare.mdx +++ b/src/content/docs/en/guides/integrations-guide/cloudflare.mdx @@ -204,28 +204,16 @@ export default defineConfig({ }); ``` -### `wasmModuleImports` +### `cloudflareModules`

**Type:** `true | false`
-**Default:** `false` +**Default:** `true`

-Whether or not to import `.wasm` files [directly as ES modules](https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration) using the `.wasm?module` import syntax. +Enables support for [importing of `.wasm`, `.bin`, and `.txt` modules](#cloudflare-module-imports). -Add `wasmModuleImports: true` to `astro.config.mjs` to enable this functionality in both `astro build` & `astro dev`. Read more about [using Wasm modules](#use-wasm-modules). - -```js title="astro.config.mjs" ins={6} -import {defineConfig} from "astro/config"; -import cloudflare from '@astrojs/cloudflare'; - -export default defineConfig({ - adapter: cloudflare({ - wasmModuleImports: true - }), - output: 'server' -}) -``` +This functionality is enabled by default. If you'd like to disable it, set `cloudflareModules: false`. ## Cloudflare runtime @@ -338,14 +326,21 @@ You can [specify additional routing patterns to follow](#routesextend) in your a Creating a custom `public/_routes.json` will override the automatic generation. See [Cloudflare's documentation on creating a custom `_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) for more details. -## Use Wasm modules +## Cloudflare Module Imports + +Cloudflare functions support [importing non-standard module types](https://developers.cloudflare.com/pages/functions/module-support/). Beyond the standard JS modules, support for most additional types is available in astro, unless [`cloudflareModules`](#cloudflaremodules) is disabled. When enabled, these files can be imported both in server side rendered pages, and during static site generation. All supported modules export a single default export: + +- `.wasm` or `.wasm?module`: exports a [`WebAssembly.Module`](https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Module) that can then be instantiated +- `.bin`: exports an [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) of the raw binary contents of the file +- `.txt`: Exports a string of the file contents The following is an example of importing a Wasm module that then responds to requests by adding the request's number parameters together. ```js title="pages/add/[a]/[b].js" -import mod from '../util/add.wasm?module'; +// Import the WebAssembly module +import mod from '../util/add.wasm'; -// instantiate ahead of time to share module +// Instantiate first in order to use it const addModule: any = new WebAssembly.Instance(mod); export async function GET(context) { @@ -355,7 +350,7 @@ export async function GET(context) { } ``` -While this example is trivial, Wasm can be used to accelerate computationally intensive operations which do not involve significant I/O such as embedding an image processing library. +While this example is trivial, Wasm can be used to accelerate computationally intensive operations which do not involve significant I/O such as embedding an image processing library, or embedding a small pre-indexed database for search over a small read-only dataset. ## Node.js compatibility