Skip to content

Commit 9c2af8e

Browse files
vicbconico974
andauthored
doc(cloudflare) add instructions for Workers Builds (#128)
Co-authored-by: conico974 <[email protected]>
1 parent 38a3cf3 commit 9c2af8e

File tree

3 files changed

+89
-48
lines changed

3 files changed

+89
-48
lines changed

pages/cloudflare/howtos/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"NextAuth": "NextAuth",
33
"stripeAPI": "Stripe API",
4-
"dev": "Development workflow",
4+
"dev-deploy": "Develop and Deploy",
55
"env-vars": "Enviroment Variables",
66
"image": "Image Optimization"
77
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { Callout } from "nextra/components";
2+
3+
## Develop and deploy
4+
5+
### Development workflow
6+
7+
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.
8+
9+
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.
10+
11+
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.
12+
13+
Let's explore, in more detail, the application development workflow we recommend for the best developer experience.
14+
15+
#### Create a new application based on a template
16+
17+
To create a new Next.js app, pre-configured to run on Cloudflare using `@opennextjs/cloudflare`, run:
18+
19+
```bash
20+
npm create cloudflare@latest -- my-next-app --framework=next --platform=workers
21+
```
22+
23+
#### Develop locally using `next dev`
24+
25+
We believe that the best development workflow uses the `next dev` command provided by Next.js.
26+
27+
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:
28+
29+
```ts
30+
// next.config.ts
31+
import type { NextConfig } from "next";
32+
33+
const nextConfig: NextConfig = {
34+
/* config options here */
35+
};
36+
37+
export default nextConfig;
38+
39+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
40+
initOpenNextCloudflareForDev();
41+
```
42+
43+
#### Use `opennextjs-cloudflare` to build and test on the Workers runtime
44+
45+
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 build` command. This will generate the Worker code in the `.open-next` directory.
46+
47+
You can then preview the app locally in the Cloudflare Workers runtime.
48+
49+
To preview your worker locally, run the `opennextjs-cloudflare preview` command. This will populate the cache and create 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.
50+
51+
### Deploy your application to Cloudflare Workers
52+
53+
Both the `deploy` and `upload` commands of `opennextjs-cloudflare` can be used to deploy your application to cloudflare Workers. Both commands will initialize the remote cache and upload your application to the Cloudflare infrastructure.
54+
55+
While `deploy` will start serving your application as soon as it is uploaded, `upload` only creates a new version of the application so that you can use [gradual deployments](https://developers.cloudflare.com/workers/configuration/versions-and-deployments/gradual-deployments/).
56+
57+
#### Local build
58+
59+
Use the `build` command followed by either `deploy` or `upload` to deploy your local build.
60+
61+
<Callout type="warning">
62+
When running the `build` command locally, `.dev.vars` and [Next `.env`
63+
files](https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables) might
64+
override your configuration. It is preferreable to use a CD system as [Workers
65+
Builds](https://developers.cloudflare.com/workers/ci-cd/builds/) to deploy your application for reproducible
66+
deployments.
67+
</Callout>
68+
69+
#### Workers Builds
70+
71+
When using Workers Builds, make sure to setup your environment variables as explained in [this guide](https://opennext.js.org/cloudflare/howtos/env-vars#production).
72+
73+
You can then connect your GitHub repository by following [the documentation](https://developers.cloudflare.com/workers/ci-cd/builds/git-integration/).
74+
75+
In the Build settings:
76+
77+
- The "Build command" should be set to `npx opennextjs-cloudflare build`.
78+
- The "Deploy command" should be set to `npx opennextjs-cloudflare deploy` (or `upload` to use gradual deployments).
79+
80+
<Callout type="info">
81+
The `deploy`, `upload`, and `populateCache` commands of `opennextjs-cloudflare` invoke `wrangler`.
82+
You can pass arguments to `wrangler` by specifying them after `--`:
83+
84+
```sh
85+
opennextjs-cloudflare deploy -- --env=prod
86+
```
87+
88+
</Callout>

pages/cloudflare/howtos/dev.mdx

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)