Skip to content

Commit 6b1aabc

Browse files
[cloudflare] add howto dev workflow
Co-authored-by: Dario Piotrowicz <dario@cloudflare.com>
1 parent e100c75 commit 6b1aabc

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

pages/cloudflare/get-started.mdx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Callout } from "nextra/components";
77

88
To create a new Next.js app, pre-configured to run on Cloudflare using `@opennextjs/cloudflare`, run:
99

10-
```
10+
```sh
1111
npm create cloudflare@latest -- my-next-app --framework=next --experimental
1212
```
1313

@@ -25,7 +25,7 @@ npm install --save-dev @opennextjs/cloudflare@latest
2525

2626
Install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) as a devDependency:
2727

28-
```npm
28+
```sh
2929
npm install --save-dev wrangler@latest
3030
```
3131

@@ -157,26 +157,25 @@ This includes:
157157
You can continue to run `next dev` when developing locally.
158158

159159
Modify your Next.js configuration file to import and call the `initOpenNextCloudflareForDev` utility
160-
from the `@opennextjs/cloudflare` package. This makes sure that the Next.js dev server can optimally integrate with the open-next
161-
cloudflare adapter and it is necessary for using bindings during local development.
160+
from the `@opennextjs/cloudflare` package. This makes sure that the Next.js dev server can optimally integrate with the open-next cloudflare adapter and it is necessary for using bindings during local development.
162161

163162
This is an example of a Next.js configuration file calling the utility:
164163

165-
```js
166-
// next.config.mjs
167-
168-
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
169-
170-
initOpenNextCloudflareForDev();
164+
```ts
165+
// next.config.ts
166+
import type { NextConfig } from "next";
171167

172-
/** @type {import('next').NextConfig} */
173-
const nextConfig = {};
168+
const nextConfig: NextConfig = {
169+
/* config options here */
170+
};
174171

175172
export default nextConfig;
173+
174+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
175+
initOpenNextCloudflareForDev();
176176
```
177177

178-
After having added the `initOpenNextCloudflareForDev()` call in your Next.js configuration file, you will be able, during local
179-
development, to access in any of your server code, local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings).
178+
After having added the `initOpenNextCloudflareForDev()` call in your Next.js configuration file, you will be able, during local development, to access in any of your server code, local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings).
180179

181180
In step 3, we also added the `npm run preview`, which allows you to quickly preview your app running locally in the Workers runtime,
182181
rather than in Node.js. This allows you to test changes in the same runtime as your app will run in when deployed to Cloudflare.

pages/cloudflare/howtos/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"NextAuth": "NextAuth",
3-
"stripeAPI": "Stripe API"
3+
"stripeAPI": "Stripe API",
4+
"dev": "Development workflow"
45
}

pages/cloudflare/howtos/dev.mdx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Development Workflow
2+
3+
The primary purpose of `@opennextjs/cloudflare` is to take a Next.js application, built with standard Next.js tooling, and convert it into a format compatible with Cloudflare Workers.
4+
5+
This code transformation process takes some time, making the adapter less than ideal for active application development, where a very fast feedback loop and other quality-of-life features, such as Hot Module Replacement (HMR), are crucial. Fortunately, Vercel already provides excellent tooling for this workflow, which Next.js developers are likely already familiar with.
6+
7+
We recommend that developers continue using the tools they are already comfortable with for local development and then use `@opennextjs/cloudflare` when they are ready to deploy their applications to the Cloudflare platform.
8+
9+
Let's explore, in more detail, the application development workflow we recommend for the best developer experience.
10+
11+
### Create a new application based on a template
12+
13+
To create a new Next.js app, pre-configured to run on Cloudflare using `@opennextjs/cloudflare`, run:
14+
15+
```bash
16+
npm create cloudflare@latest -- my-next-app --framework=next --experimental
17+
```
18+
19+
### Develop locally using `next dev`
20+
21+
We believe that the best development workflow uses the `next dev` command provided by Next.js.
22+
23+
To access Cloudflare resources using the `getCloudflareContext` API while running `next dev`, you will need to update the Next.js configuration to call `initOpenNextCloudflareForDev`, as shown in the following example:
24+
25+
```ts
26+
// next.config.ts
27+
import type { NextConfig } from "next";
28+
29+
const nextConfig: NextConfig = {
30+
/* config options here */
31+
};
32+
33+
export default nextConfig;
34+
35+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
36+
initOpenNextCloudflareForDev();
37+
```
38+
39+
### `wrangler dev` and `wrangler deploy`
40+
41+
After you've finished iterating on your Next.js application with `next dev`, you can convert it to a Cloudflare Worker by running the `opennextjs-cloudflare` command. This will generate the Worker code in the `.open-next` directory.
42+
43+
You can then preview the app locally in the Cloudflare Workers runtime or deploy it to the Cloudflare network.
44+
45+
To preview your worker locally, run the `wrangler dev` command. This creates a local server that runs your worker in the Cloudflare Workers runtime. Testing your worker is important to ensure that it has been properly built and is working as expected.
46+
47+
Once you've tested your worker, You can run `wrangler deploy`, and in a matter of seconds, your masterpiece will be available to all your users around the world.

0 commit comments

Comments
 (0)