diff --git a/pages/cloudflare/0.2/_meta.json b/pages/cloudflare/0.2/_meta.json new file mode 100644 index 0000000..9a7cfe5 --- /dev/null +++ b/pages/cloudflare/0.2/_meta.json @@ -0,0 +1,7 @@ +{ + "index": "Overview", + "get-started": "", + "bindings": "", + "caching": "", + "examples": "" +} \ No newline at end of file diff --git a/pages/cloudflare/0.2/bindings.mdx b/pages/cloudflare/0.2/bindings.mdx new file mode 100644 index 0000000..900e009 --- /dev/null +++ b/pages/cloudflare/0.2/bindings.mdx @@ -0,0 +1,65 @@ +import { SITE } from '../../../config'; +import { Callout } from 'nextra/components'; + +### Bindings + +[Bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) allow your Worker to interact with resources on the Cloudflare Developer Platform. When you declare a binding on your Worker, you grant it a specific capability, such as being able to read and write files to an [R2](https://developers.cloudflare.com/r2/) bucket. + +#### How to configure your Next.js app so it can access bindings + +Install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare), and then add a `wrangler.toml` file in the root directory of your Next.js app, as described in [Get Started](/cloudflare/get-started). + +#### How to access bindings in your Next.js app + +You can access [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) from any route of your Next.js app via `getCloudflareContext`: + +```js +import { getCloudflareContext } from "@opennextjs/cloudflare"; + +export async function GET(request) { + let responseText = "Hello World"; + + const myKv = (await getCloudflareContext()).env.MY_KV_NAMESPACE; + await myKv.put("foo", "bar"); + const foo = await myKv.get("foo"); + + return new Response(foo); +} +``` + +#### How to add bindings to your Worker + +Add bindings to your Worker by [adding them to your `wrangler.toml` configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/). + +## TypeScript type declarations for bindings + +To ensure that the `env` object from `(await getCloudflareContext()).env` above has accurate TypeScript types, run the following Wrangler command to [generate types that match your Worker's configuration](https://developers.cloudflare.com/workers/languages/typescript/#generate-types-that-match-your-workers-configuration-experimental): + +``` +npx wrangler types --experimental-include-runtime +``` + +This will generate a `d.ts` file and (by default) save it to `.wrangler/types/runtime.d.ts`. You will be prompted in the command's output to add that file to your `tsconfig.json`'s `compilerOptions.types` array. + +If you would like to commit the file to git, you can provide a custom path. Here, for instance, the `runtime.d.ts` file will be saved to the root of your project: + +```bash +npx wrangler types --experimental-include-runtime="./runtime.d.ts" +``` + +To ensure that your types are always up-to-date, make sure to run `wrangler types --experimental-include-runtime` after any changes to your config file. + +## Other Cloudflare APIs (`cf`, `ctx`) + +You can access context about the incoming request from the [`cf` object](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties), as well as lifecycle methods from the [`ctx` object](https://developers.cloudflare.com/workers/runtime-apis/context) from the return value of [`getCloudflareContext()`](https://github.com/opennextjs/opennextjs-cloudflare/blob/main/packages/cloudflare/src/api/get-cloudflare-context.ts): + +```js +import { getCloudflareContext } from "@opennextjs/cloudflare"; + + +export async function GET(request) { + const { env, cf, ctx } = await getCloudflareContext(); + + // ... +} +``` diff --git a/pages/cloudflare/0.2/caching.mdx b/pages/cloudflare/0.2/caching.mdx new file mode 100644 index 0000000..cbeb544 --- /dev/null +++ b/pages/cloudflare/0.2/caching.mdx @@ -0,0 +1,36 @@ +import { SITE } from '../../../config'; +import { Callout } from 'nextra/components'; + +## Caching + +`@opennextjs/cloudflare` supports [caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#caching-data) and [revalidating](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) data returned by subrequests you make in your app by calling [`fetch()`](https://developers.cloudflare.com/workers/runtime-apis/fetch/). + +By default, all `fetch()` subrequests made in your Next.js app are cached. Refer to the [Next.js documentation](https://nextjs.org/docs/app/building-your-application/caching#opting-out-1) for information about how to disable caching for an individual subrequest, or for an entire route. + +[The cache persists across deployments](https://nextjs.org/docs/app/building-your-application/caching#data-cache). You are responsible for revalidating/purging this cache. + + +Workers KV is eventually consistent, which means that it can take up to 60 seconds for updates to be reflected globally, when using the default TTL of 60 seconds. + + +### How to enable caching + +`@opennextjs/cloudflare` uses [Workers KV](https://developers.cloudflare.com/kv/) as the cache for your Next.js app. Workers KV is [fast](https://blog.cloudflare.com/faster-workers-kv) and uses Cloudflare's [Tiered Cache](https://developers.cloudflare.com/cache/how-to/tiered-cache/) to increase cache hit rates. When you write cached data to Workers KV, you write to storage that can be read by any Cloudflare location. This means your app can fetch data, cache it in KV, and then subsequent requests anywhere around the world can read from this cache. + +To enable caching, you must: + +#### 1. Create a KV namespace + +``` +npx wrangler@latest kv namespace create +``` + +#### 2. Add the KV namespace to your Worker + +The binding name used in your app's worker will be `NEXT_CACHE_WORKERS_KV` by default. This is configurable and can be changed by setting the `__OPENNEXT_KV_BINDING_NAME` build-time environment variable. + +``` +[[kv_namespaces]] +binding = "NEXT_CACHE_WORKERS_KV" +id = "" +``` diff --git a/pages/cloudflare/0.2/examples.mdx b/pages/cloudflare/0.2/examples.mdx new file mode 100644 index 0000000..2cdecbb --- /dev/null +++ b/pages/cloudflare/0.2/examples.mdx @@ -0,0 +1,23 @@ +import { SITE } from '../../../config'; +import { Callout } from 'nextra/components'; + +## Examples + +To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: + +``` +npm create cloudflare@latest -- my-next-app --framework=next --experimental +``` + +### Basic starter projects + +Two basic example apps are included in the repository for `@opennextjs/cloudflare` package: + +- [*`create-next-app`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/create-next-app) — a Next.js project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). +- [*`api`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/api) — a minimal Next.js project with a single API route + +You can use these to understand how to configure your Next.js app to use `@opennextjs/cloudflare`, or refer to [Get Started](/cloudflare/get-started). + +### Next.js Commerce Demo + +The [Next.js Commerce demo app](https://github.com/vercel/commerce/tree/v1) works with `@opennextjs/cloudflare`. You can view a deployed version of it [here](https://vercel-commerce-on-workers.web-experiments.workers.dev/). diff --git a/pages/cloudflare/0.2/get-started.mdx b/pages/cloudflare/0.2/get-started.mdx new file mode 100644 index 0000000..803fbe4 --- /dev/null +++ b/pages/cloudflare/0.2/get-started.mdx @@ -0,0 +1,138 @@ +import { SITE } from '../../../config'; +import { Callout } from 'nextra/components'; + +### Get Started + +#### New apps + +To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: + +``` +npm create cloudflare@latest -- my-next-app --framework=next --experimental +``` + +#### Existing Next.js apps + +##### 1. Install @opennextjs/cloudflare + +First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare): + +```sh +npm install --save-dev @opennextjs/cloudflare +``` + +##### 2. Install Wrangler, and add a `wrangler.toml` file + +Install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) as a devDependency: + +```npm +npm install -D wrangler@latest +``` + + +You must use Wrangler version `3.78.10` or later to deploy Next.js apps using `@opennextjs/cloudflare`. + + +Then, add a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) file to the root directory of your Next.js app: + +```toml +main = ".worker-next/index.mjs" +name = "my-app" +compatibility_date = "2024-09-23" +compatibility_flags = ["nodejs_compat"] +assets = { directory = ".worker-next/assets", binding = "ASSETS" } +``` + + +As shown above, you must enable the [`nodejs_compat` compatibility flag](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) *and* set your [compatibility date](https://developers.cloudflare.com/workers/configuration/compatibility-dates/) to `2024-09-23` or later, in order for your Next.js app to work with @opennextjs/cloudflare. + + +`wrangler.toml` is where you configure your Worker and define what resources it can access via [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings). + +##### 3. Update `package.json` + +Add the following to the scripts field of your `package.json` file: + +```json +"build:worker": "cloudflare", +"dev:worker": "wrangler dev --port 8771", +"preview:worker": "npm run build:worker && npm run dev:worker", +"deploy:worker": "npm run build:worker && wrangler deploy" +``` + +- `npm run build:worker`: Runs the [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) adapter. This first builds your app by running `next build` behind the scenes, and then transforms the build output to a format that you can run locally using [Wrangler](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare. +- `npm run dev:worker`: Takes the output generated by `build:worker` and runs it locally in [workerd](https://github.com/cloudflare/workerd), the open-source Workers Runtime, allowing you to run the app locally in the same environment that it will run in production. If you instead run `next dev`, your app will run in Node.js, which is a different JavaScript runtime from the Workers runtime, with differences in behavior and APIs. +- `npm run preview:worker`: Runs `build:worker` and then `dev:worker`, allowing you to quickly preview your app running locally in the Workers runtime, via a single command. +- `npm run deploy`: Builds your app, and then deploys it to Cloudflare + +### 4. Add caching with Workers KV + +See the [Caching docs](/cloudflare/0.2/caching) for information on enabling Next.js caching in your OpenNext project. + +### 5. Remove `@cloudflare/next-on-pages` (if necessary) + +If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes. + +#### Remove `export const runtime = "edge";` + +Before deploying your app, remove the `export const runtime = "edge";` line from your `next.config.js` file. This line is not needed when using `@opennextjs/cloudflare`. + +#### Add `.worker-next` to `.gitignore` + +You should add `.worker-next` to your `.gitignore` file to prevent the build output from being committed to your repository. + +#### Uninstall `@cloudflare/next-on-pages` + +You should uninstall `@cloudflare/next-on-pages` and remove any references to it. + +In `package.json`: + +```diff +"scripts": { +- "pages:build": "npx @cloudflare/next-on-pages", +- "preview": "npm run pages:build && wrangler pages dev", +- "deploy": "npm run pages:build && wrangler pages deploy" + +"devDependencies": { +- "@cloudflare/next-on-pages": "*", +``` + +(remember to also remove [eslint-plugin-next-on-pages](https://www.npmjs.com/package/eslint-plugin-next-on-pages) from your `.eslintrc.js` file) + +You no longer need to call `setupDevPlatform()` in your `next.config.mjs` file: + +```diff title="next.config.mjs" +- import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev'; + +/** @type {import('next').NextConfig} */ +const nextConfig = {}; + +- if (process.env.NODE_ENV === 'development') { +- await setupDevPlatform(); +- } +``` + +And you'll want to replace any uses of `getRequestContext` from `@cloudflare/next-on-pages` with `getCloudflareContext` from `@opennextjs/cloudflare`: + +```diff +- import { getRequestContext } from "@cloudflare/next-on-pages"; ++ import { getCloudflareContext } from "@opennextjs/cloudflare"; +``` + +##### 6. Develop locally + +You can continue to run `next dev` when developing locally. + +During local development, you can access local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings). + +In step 3, we also added the `npm run preview:worker`, which allows you to quickly preview your app running locally in the Workers runtime, 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. + +##### 7. Deploy to Cloudflare Workers + +Either deploy via the command line: + +```sh +npm run deploy:worker +``` + +Or [connect a Github or Gitlab repository](https://developers.cloudflare.com/workers/ci-cd/), and Cloudflare will automatically build and deploy each pull request you merge to your production branch. diff --git a/pages/cloudflare/0.2/index.mdx b/pages/cloudflare/0.2/index.mdx new file mode 100644 index 0000000..02ba3dd --- /dev/null +++ b/pages/cloudflare/0.2/index.mdx @@ -0,0 +1,66 @@ +import { SITE } from '../../../config'; +import { Callout } from 'nextra/components'; + +## Cloudflare + +The [`@opennextjs/cloudflare`](https://www.npmjs.com/package/@opennextjs/cloudflare) adapter lets you deploy Next.js apps to [Cloudflare Workers](https://developers.cloudflare.com/workers) using the [Node.js "runtime" from Next.js](https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes). + + +[`@opennextjs/cloudflare`](https://www.npmjs.com/package/@opennextjs/cloudflare) is pre 1.0, and still in active development. You should try it, [report bugs](https://github.com/opennextjs/opennextjs-cloudflare/issues), [share feedback](https://github.com/opennextjs/opennextjs-cloudflare/discussions), and contribute code to help make running Next.js apps on Cloudflare easier. We don't quite yet recommend using it for mission-critical production apps. + +You can also use [`@cloudflare/next-on-pages`](https://www.npmjs.com/package/@cloudflare/next-on-pages) to deploy Next.js apps to Cloudflare Pages. You can review the differences in supported Next.js features below and by reviewing [the docs for `@cloudflare/next-on-pages`](https://developers.cloudflare.com/pages/framework-guides/nextjs/ssr/supported-features/), and understand the differences between Workers and Pages [here](https://developers.cloudflare.com/workers/static-assets/compatibility-matrix/). + + + +### Get Started + +##### New apps + +To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: + +``` +npm create cloudflare@latest -- my-next-app --framework=next --experimental +``` + +##### Existing Next.js apps + +Follow the guide [here](/cloudflare/0.2/get-started) to use [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) with an existing Next.js app. + +### Supported Next.js runtimes + +Next.js has [two "runtimes"](https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes) — "Edge" and "Node.js". When you use `@opennextjs/cloudflare`, your app can use the Node.js runtime, which is more fully featured, and allows you to use the [Node.js APIs](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) that are provided by the Cloudflare Workers runtime. + +This is an important difference from `@cloudflare/next-on-pages`, which only supports the "Edge" runtime. The Edge Runtime code in Next.js [intentionally constrains which APIs from Node.js can be used](https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/plugins/middleware-plugin.ts#L820), and the "Edge" runtime does not support all Next.js features. + +### Supported Next.js versions + +`@opennextjs/cloudflare` is pre 1.0, and still in active development. We intend to support all minor and patch version of Next.js 13 and 14, as well as Next.js 15 when it is released. (currently a release candidate) + +To help improve compatibility, we encourage you to [report bugs](https://github.com/opennextjs/opennextjs-cloudflare/issues) and contribute code! + +### Supported Next.js features + +- [x] [App Router](https://nextjs.org/docs/app) +- [x] [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) +- [x] [Dynamic routes](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes) +- [x] [Static Site Generation (SSG)](https://nextjs.org/docs/app/building-your-application/rendering/server-components#static-rendering-default) +- [x] [Server-Side Rendering (SSR)](https://nextjs.org/docs/app/building-your-application/rendering/server-components) + +### Not Yet Supported Next.js features + +The following Next.js features are not yet supported — but we welcome both contributions and feedback! Tell us what you'd like to see, or what you'd like to add support for: + +- [ ] [Pages Router](https://nextjs.org/docs/pages) (you should use the App Router instead, which was introduced in Next.js 13) +- [ ] [Incremental Static Regeneration (ISR)](https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration) +- [ ] [Partial Prerendering (PPR)](https://nextjs.org/docs/app/building-your-application/rendering/partial-prerendering) +- [ ] [Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) +- [ ] [Image optimization](https://nextjs.org/docs/app/building-your-application/optimizing/images) (you can integrate Cloudflare Images with Next.js by following [this guide](https://developers.cloudflare.com/images/transform-images/integrate-with-frameworks/)) +- [ ] [Experimental streaming support](https://nextjs.org/blog/next-15-rc#executing-code-after-a-response-with-nextafter-experimental) + +### How @opennextjs/cloudflare Works + +The OpenNext Cloudflare adapter works by taking the Next.js build output and transforming it, so that it can run in Cloudflare Workers. + +When you add [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) as a dependency to your Next.js app, and then run `npx cloudflare` the adapter first builds your app by running `next build`, and then transforms the build output to a format that you can run locally using [Wrangler](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare. + +You can view the code for @opennextjs/cloudflare [here](https://github.com/opennextjs/opennextjs-cloudflare/blob/main/packages/cloudflare/src) to understand what it does under the hood. diff --git a/pages/cloudflare/_meta.json b/pages/cloudflare/_meta.json index 2c667df..8d0c8fa 100644 --- a/pages/cloudflare/_meta.json +++ b/pages/cloudflare/_meta.json @@ -4,5 +4,7 @@ "bindings": "", "caching": "", "examples": "", - "troubleshooting": "" + "troubleshooting": "", + "migrate-from-0.2": "", + "0.2": "Release 0.2" } diff --git a/pages/cloudflare/caching.mdx b/pages/cloudflare/caching.mdx index b50eeab..50e02f3 100644 --- a/pages/cloudflare/caching.mdx +++ b/pages/cloudflare/caching.mdx @@ -3,12 +3,16 @@ import { Callout } from 'nextra/components'; ## Caching -`@opennextjs/cloudflare` supports [caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#caching-data) and [revalidating](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) data returned by subrequests you make in your app by calling [`fetch()`](https://developers.cloudflare.com/workers/runtime-apis/fetch/). +`@opennextjs/cloudflare` supports [caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#caching-data). By default, all `fetch()` subrequests made in your Next.js app are cached. Refer to the [Next.js documentation](https://nextjs.org/docs/app/building-your-application/caching#opting-out-1) for information about how to disable caching for an individual subrequest, or for an entire route. [The cache persists across deployments](https://nextjs.org/docs/app/building-your-application/caching#data-cache). You are responsible for revalidating/purging this cache. +Note that [Revalidating](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) is not yet supported. + +Next.js primes the cache at build time. The build time values are serverd by the [Workers Assets](https://developers.cloudflare.com/workers/static-assets/). + Workers KV is eventually consistent, which means that it can take up to 60 seconds for updates to be reflected globally, when using the default TTL of 60 seconds. @@ -27,7 +31,7 @@ npx wrangler@latest kv namespace create #### 2. Add the KV namespace to your Worker -The binding name used in your app's worker will be `NEXT_CACHE_WORKERS_KV` by default. This is configurable and can be changed by setting the `__OPENNEXT_KV_BINDING_NAME` build-time environment variable. +The binding name used in your app's worker is `NEXT_CACHE_WORKERS_KV`. ``` [[kv_namespaces]] diff --git a/pages/cloudflare/examples.mdx b/pages/cloudflare/examples.mdx index aba8420..ff0eb03 100644 --- a/pages/cloudflare/examples.mdx +++ b/pages/cloudflare/examples.mdx @@ -6,15 +6,22 @@ import { Callout } from 'nextra/components'; To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: ``` -npm create cloudflare@latest -- my-next-app --framework=next --experimental +npm create cloudflare@latest -- --template https://github.com/flarelabs-net/workers-next ``` + +The experimental template for the Next framework is still based on 0.2. +Use the template from `flarelabs-net/workers-next` as indicated above to use 0.3. + + ### Basic starter projects -Two basic example apps are included in the repository for `@opennextjs/cloudflare` package: +Basic example apps are included in the repository for `@opennextjs/cloudflare` package: - [*`create-next-app`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/create-next-app) — a Next.js project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). - [*`api`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/api) — a minimal Next.js project with a single API route +- [*`middleware`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/middleware) — a minimal Next.js project using middleware +- [*`vercel-blog-starter`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/vercel-blog-starter) — a blog project using SSG You can use these to understand how to configure your Next.js app to use `@opennextjs/cloudflare`, or refer to [Get Started](/cloudflare/get-started). diff --git a/pages/cloudflare/get-started.mdx b/pages/cloudflare/get-started.mdx index 3e47e5e..bcce2aa 100644 --- a/pages/cloudflare/get-started.mdx +++ b/pages/cloudflare/get-started.mdx @@ -8,9 +8,14 @@ import { Callout } from 'nextra/components'; To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: ``` -npm create cloudflare@latest -- my-next-app --framework=next --experimental +npm create cloudflare@latest -- --template https://github.com/flarelabs-net/workers-next ``` + +The experimental template for the Next framework is still based on 0.2. +Use the template from `flarelabs-net/workers-next` as indicated above to use 0.3. + + #### Existing Next.js apps ##### 1. Install @opennextjs/cloudflare @@ -30,16 +35,19 @@ npm install -D wrangler@latest ``` -You must use Wrangler version `3.78.10` or later to deploy Next.js apps using `@opennextjs/cloudflare`. +You must use Wrangler version `3.99.0` or later to deploy Next.js apps using `@opennextjs/cloudflare`. Then, add a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) file to the root directory of your Next.js app: ```toml -main = ".worker-next/index.mjs" +main = ".open-next/worker.js" name = "my-app" + compatibility_date = "2024-09-23" compatibility_flags = ["nodejs_compat"] + +# The binding name must be "ASSETS" when the cache is enabled assets = { directory = ".worker-next/assets", binding = "ASSETS" } ``` @@ -49,12 +57,57 @@ As shown above, you must enable the [`nodejs_compat` compatibility flag](https:/ `wrangler.toml` is where you configure your Worker and define what resources it can access via [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings). -##### 3. Update `package.json` +##### 3. Add a `open-next.config.ts` file + +Then, add a [`open-next.config.ts`](https://opennext.js.org/aws/config) file to the root directory of your Next.js app: + +```ts +import type { OpenNextConfig } from "@opennextjs/aws/types/open-next"; +import cache from "@opennextjs/cloudflare/kvCache"; + +const config: OpenNextConfig = { + default: { + override: { + wrapper: "cloudflare-node", + converter: "edge", + // Set `incrementalCache` to "dummy" to disable KV cache + incrementalCache: async () => cache, + tagCache: "dummy", + queue: "dummy", + }, + }, + + middleware: { + external: true, + override: { + wrapper: "cloudflare-edge", + converter: "edge", + proxyExternalRequest: "fetch", + }, + }, +}; + +export default config; +``` + +You can either install the `@opennextjs/aws` NPM package to get the types or `open-next.config.ts` to the [`exclude`](https://www.typescriptlang.org/tsconfig/#exclude) configuration key of your `tsconfig.json`. + +##### 4. Add a `.dev.vars` file + +Then, add a [`.dev.vars`](https://developers.cloudflare.com/workers/testing/local-development/#local-only-environment-variables) file to the root directory of your Next.js app: + +```text +NEXTJS_ENV=development +``` + +The `NEXTJS_ENV` variable defines the environment to use when loading Next.js `.env` files. It defaults to "production" when not defined. + +##### 5. Update `package.json` Add the following to the scripts field of your `package.json` file: ```json -"build:worker": "cloudflare", +"build:worker": "opennextjs-cloudflare", "dev:worker": "wrangler dev --port 8771", "preview:worker": "npm run build:worker && npm run dev:worker", "deploy:worker": "npm run build:worker && wrangler deploy" @@ -65,21 +118,22 @@ Add the following to the scripts field of your `package.json` file: - `npm run preview:worker`: Runs `build:worker` and then `dev:worker`, allowing you to quickly preview your app running locally in the Workers runtime, via a single command. - `npm run deploy`: Builds your app, and then deploys it to Cloudflare -### 4. Add caching with Workers KV +### 6. Add caching with Workers KV See the [Caching docs](/cloudflare/caching) for information on enabling Next.js caching in your OpenNext project. -### 5. Remove `@cloudflare/next-on-pages` (if necessary) +### 7. Remove `@cloudflare/next-on-pages` (if necessary) If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes. #### Remove `export const runtime = "edge";` -Before deploying your app, remove the `export const runtime = "edge";` line from your `next.config.js` file. This line is not needed when using `@opennextjs/cloudflare`. +Before deploying your app, remove the `export const runtime = "edge";` line from your `next.config.js` file. +The edge runtime is not supported yet with `@opennextjs/cloudflare`. -#### Add `.worker-next` to `.gitignore` +#### Add `.open-next` to `.gitignore` -You should add `.worker-next` to your `.gitignore` file to prevent the build output from being committed to your repository. +You should add `.open-next` to your `.gitignore` file to prevent the build output from being committed to your repository. #### Uninstall `@cloudflare/next-on-pages` @@ -119,7 +173,7 @@ And you'll want to replace any uses of `getRequestContext` from `@cloudflare/nex + import { getCloudflareContext } from "@opennextjs/cloudflare"; ``` -##### 6. Develop locally +##### 8. Develop locally You can continue to run `next dev` when developing locally. @@ -127,7 +181,7 @@ During local development, you can access local versions of Cloudflare bindings a In step 3, we also added the `npm run preview:worker`, which allows you to quickly preview your app running locally in the Workers runtime, 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. -##### 7. Deploy to Cloudflare Workers +##### 9. Deploy to Cloudflare Workers Either deploy via the command line: diff --git a/pages/cloudflare/index.mdx b/pages/cloudflare/index.mdx index a1368ed..1e3ee1f 100644 --- a/pages/cloudflare/index.mdx +++ b/pages/cloudflare/index.mdx @@ -19,9 +19,14 @@ You can also use [`@cloudflare/next-on-pages`](https://www.npmjs.com/package/@cl To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: ``` -npm create cloudflare@latest -- my-next-app --framework=next --experimental +npm create cloudflare@latest -- --template https://github.com/flarelabs-net/workers-next ``` + +The experimental template for the Next framework is still based on 0.2. +Use the template from `flarelabs-net/workers-next` as indicated above to use 0.3. + + ##### Existing Next.js apps Follow the guide [here](/cloudflare/get-started) to use [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) with an existing Next.js app. @@ -34,33 +39,33 @@ This is an important difference from `@cloudflare/next-on-pages`, which only sup ### Supported Next.js versions -`@opennextjs/cloudflare` is pre 1.0, and still in active development. We intend to support all minor and patch version of Next.js 13 and 14, as well as Next.js 15 when it is released. (currently a release candidate) +`@opennextjs/cloudflare` is pre 1.0, and still in active development. We intend to support all minor and patch version of Next.js 13, 14, and 15. To help improve compatibility, we encourage you to [report bugs](https://github.com/opennextjs/opennextjs-cloudflare/issues) and contribute code! ### Supported Next.js features +Some Next.js features are not yet supported are not fully tested. +We will update the list as we progress towards releasing 1.0. + - [x] [App Router](https://nextjs.org/docs/app) - [x] [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) - [x] [Dynamic routes](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes) - [x] [Static Site Generation (SSG)](https://nextjs.org/docs/app/building-your-application/rendering/server-components#static-rendering-default) - [x] [Server-Side Rendering (SSR)](https://nextjs.org/docs/app/building-your-application/rendering/server-components) - -### Not Yet Supported Next.js features - -The following Next.js features are not yet supported — but we welcome both contributions and feedback! Tell us what you'd like to see, or what you'd like to add support for: - +- [x] [Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) +- [x] [Image optimization](https://nextjs.org/docs/app/building-your-application/optimizing/images) (you can integrate Cloudflare Images with Next.js by following [this guide](https://developers.cloudflare.com/images/transform-images/integrate-with-frameworks/)) - [ ] [Pages Router](https://nextjs.org/docs/pages) (you should use the App Router instead, which was introduced in Next.js 13) - [ ] [Incremental Static Regeneration (ISR)](https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration) - [ ] [Partial Prerendering (PPR)](https://nextjs.org/docs/app/building-your-application/rendering/partial-prerendering) -- [ ] [Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) -- [ ] [Image optimization](https://nextjs.org/docs/app/building-your-application/optimizing/images) (you can integrate Cloudflare Images with Next.js by following [this guide](https://developers.cloudflare.com/images/transform-images/integrate-with-frameworks/)) - [ ] [Experimental streaming support](https://nextjs.org/blog/next-15-rc#executing-code-after-a-response-with-nextafter-experimental) +We welcome both contributions and feedback! + ### How @opennextjs/cloudflare Works The OpenNext Cloudflare adapter works by taking the Next.js build output and transforming it, so that it can run in Cloudflare Workers. -When you add [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) as a dependency to your Next.js app, and then run `npx cloudflare` the adapter first builds your app by running `next build`, and then transforms the build output to a format that you can run locally using [Wrangler](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare. +When you add [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) as a dependency to your Next.js app, and then run `npx opennextjs-cloudflare` the adapter first builds your app by running `next build`, and then transforms the build output to a format that you can run locally using [Wrangler](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare. You can view the code for @opennextjs/cloudflare [here](https://github.com/opennextjs/opennextjs-cloudflare/blob/main/packages/cloudflare/src) to understand what it does under the hood. diff --git a/pages/cloudflare/migrate-from-0.2.mdx b/pages/cloudflare/migrate-from-0.2.mdx new file mode 100644 index 0000000..e5ce039 --- /dev/null +++ b/pages/cloudflare/migrate-from-0.2.mdx @@ -0,0 +1,83 @@ +import { SITE } from '../../config'; +import { Callout } from 'nextra/components'; + +### Migrate from 0.2 + +The `@opennextjs/cloudflare` adapter is now more closely intgrated with `@opennextjs/aws`. + +You will need to update your projects based on the 0.2 release as described in the following sections. + +##### 1. Update the `wrangler.toml` file + +The entry point is now `.open-next/worker.js`, update `wrangler.toml` accordingly: + +```toml +# CHANGED: new entry point location +main = ".open-next/worker.js" +name = "my-app" + +compatibility_date = "2024-09-23" +compatibility_flags = ["nodejs_compat"] + +# The binding name must be "ASSETS" when the cache is enabled +# CHANGED: output folder location +assets = { directory = ".open-next/assets", binding = "ASSETS" } +``` + +##### 2. Add a `open-next.config.ts` file + +Add a [`open-next.config.ts`](https://opennext.js.org/aws/config) file to the root directory of your Next.js app: + +```ts +import type { OpenNextConfig } from "@opennextjs/aws/types/open-next"; +import cache from "@opennextjs/cloudflare/kvCache"; + +const config: OpenNextConfig = { + default: { + override: { + wrapper: "cloudflare-node", + converter: "edge", + // Set `incrementalCache` to "dummy" to disable KV cache + incrementalCache: async () => cache, + tagCache: "dummy", + queue: "dummy", + }, + }, + + middleware: { + external: true, + override: { + wrapper: "cloudflare-edge", + converter: "edge", + proxyExternalRequest: "fetch", + }, + }, +}; + +export default config; +``` + +You can either install the `@opennextjs/aws` NPM package to get the types or `open-next.config.ts` to the [`exclude`](https://www.typescriptlang.org/tsconfig/#exclude) configuration key of your `tsconfig.json`. + +##### 3. Add a `.dev.vars` file + +Add a [`.dev.vars`](https://developers.cloudflare.com/workers/testing/local-development/#local-only-environment-variables) file to the root directory of your Next.js app: + +```text +NEXTJS_ENV=development +``` + +The `NEXTJS_ENV` variable defines the environment to use when loading Next.js `.env` files. It defaults to "production" when not defined. + +##### 4. Update `package.json` + +The name of the CLI was changed to `opennextjs-cloudflare`: + +```json +"build:worker": "opennextjs-cloudflare", +``` + +##### 5 Add `.open-next` to `.gitignore` + +You should change `.worker-next` to `.open-next` in your `.gitignore` file to prevent the build output from being committed to your repository. +You can safely delete the content of the now unused `.worker-next`. \ No newline at end of file