Skip to content

Commit

Permalink
Updated docs for withastro/adapters#251
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlyjak committed May 7, 2024
1 parent fb4dda4 commit d52d4e8
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/content/docs/en/guides/integrations-guide/cloudflare.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -204,28 +204,16 @@ export default defineConfig({
});
```

### `wasmModuleImports`
### `cloudflareModules`

<p>
**Type:** `true | false`<br />
**Default:** `false`
**Default:** `true`
</p>

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

Expand Down Expand Up @@ -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) {
Expand All @@ -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

Expand Down

0 comments on commit d52d4e8

Please sign in to comment.