Skip to content

Commit

Permalink
feat(create-remix): Update cloudflare worker README
Browse files Browse the repository at this point in the history
This adds a section explaining how to make the worker work in ESM mode.
  • Loading branch information
ekosz committed Dec 23, 2021
1 parent b86d96e commit e0f246e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/create-remix/templates/cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,29 @@ Once that's done, you should be able to deploy your app:
```sh
npm run deploy
```

## Using [ES Module workers](https://blog.cloudflare.com/workers-javascript-modules/)

If you would like to use features like [Durable
Objects](https://developers.cloudflare.com/workers/learning/using-durable-objects)
you will need to slightly change the [`worker/index.js`](./worker/index.js)
file. In ES Module workers we no longer have a `FetchEvent` object and will
need to generate something similar ourselves instead.

```js
import { createEventHandler } from "@remix-run/cloudflare-workers";

import * as build from "../build";

const handler = createEventHandler({ build })

export default {
fetch(request, env, context) {
const event = { request, env, waitUntil: context.waitUntil.bind(waitUntil) };
return handler(event);
}
}
```

**Note**: This will now be the new object passed to you if you add
a `getLoadContext` function to the `createEventHandler`.

0 comments on commit e0f246e

Please sign in to comment.