【作業ログ】Cloudflare Workers + Next.js を構築してみた
はじめに
フロントエンドに Next.js を、インフラに Cloudflare を採用する場面がありました。チュートリアルを実施したので内容をメモします。
以前にバックエンドのチュートリアルを実施し、今回はフロントエンドのチュートリアルとなります。
次のチュートリアルを進めていきます。DeepL を使用して翻訳しながら進めました。
作業したリポジトリはこちらです。
What is Next.js?
はじめに、Next.js とは、フルスタックアプリケーションを構築するための React フレームワークである。
Next.jsはサーバーサイドレンダリングとクライアントサイドレンダリングをサポートするほか、部分プリレンダリング機能により、同一ルート内で静的コンポーネントと動的コンポーネントを組み合わせることが可能である。
OpenNext アダプターを使用して、Next.js アプリを Cloudflare Workers にデプロイすることができる。
Deploy a new Next.js project on Workers
新規 Next.js プロジェクトを作成し、Cloudflare にデプロイしよう。
Create a new project
まずはプロジェクトを作成しよう。ツールには create-cloudflare CLI (C3) を使用する。
❯ npm create cloudflare@latest -- cloudflare-nextjs-example --framework=next
Need to install the following packages:
create-cloudflare@2.62.3
Ok to proceed? (y) y
> npx
> "create-cloudflare" cloudflare-nextjs-example --framework=next
──────────────────────────────────────────────────────────────────────────────────────────────────────────
👋 Welcome to create-cloudflare v2.62.3!
🧡 Let's get started.
📊 Cloudflare collects telemetry about your usage of Create-Cloudflare.
Learn more at: https://github.com/cloudflare/workers-sdk/blob/main/packages/create-cloudflare/telemetry.md
──────────────────────────────────────────────────────────────────────────────────────────────────────────
╭ Create an application with Cloudflare Step 1 of 3
│
├ In which directory do you want to create your application?
│ dir ./cloudflare-nextjs-example
│
├ What would you like to start with?
│ category Framework Starter
│
├ Which development framework do you want to use?
│ framework Next.js
│
├ Cloning template from: https://github.com/opennextjs/opennextjs-cloudflare/tree/main/create-cloudflare/next
│
├ Updating name in `package.json`
│ updated `package.json`
│
├ Installing dependencies
│ installed via `npm install`
│
╰ Application created
╭ Configuring your application for Cloudflare Step 2 of 3
│
├ Installing wrangler A command line tool for building Cloudflare Workers
│ installed via `npm install wrangler --save-dev`
│
├ Retrieving current workerd compatibility date
│ compatibility date Could not find workerd date, falling back to 2025-09-27
│
├ Adding Wrangler files to the .gitignore file
│ updated .gitignore file
│
├ Generating types for your application
│ generated to `./cloudflare-env.d.ts` via `npm run cf-typegen`
│
├ Installing @types/node
│ installed via npm
│
├ You're in an existing git repository. Do you want to use git for version control?
│ yes git
│
╰ Application configured
╭ Deploy with Cloudflare Step 3 of 3
│
├ Do you want to deploy your application?
│ no deploy via `npm run deploy`
│
╰ Done
────────────────────────────────────────────────────────────
🎉 SUCCESS Application created successfully!
💻 Continue Developing
Change directories: cd cloudflare-nextjs-example
Deploy: npm run deploy
📖 Explore Documentation
https://developers.cloudflare.com/workers
🐛 Report an Issue
https://github.com/cloudflare/workers-sdk/issues/new/choose
💬 Join our Community
https://discord.cloudflare.com
────────────────────────────────────────────────────────────
「今すぐにデプロイするかどうか?」には「No」を選択した。
プロジェクトが作成されたようだ。
作成されたファイル
package.json の中身を覗いてみよう。
{
"name": "cloudflare-nextjs-example",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy",
"upload": "opennextjs-cloudflare build && opennextjs-cloudflare upload",
"preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview",
"cf-typegen": "wrangler types --env-interface CloudflareEnv ./cloudflare-env.d.ts"
},
"dependencies": {
"@opennextjs/cloudflare": "^1.15.1",
"next": "16.1.4",
"react": "19.1.4",
"react-dom": "19.1.4"
},
"devDependencies": {
"@eslint/eslintrc": "^3",
"@tailwindcss/postcss": "^4",
"@types/node": "^20.19.30",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "15.4.6",
"tailwindcss": "^4",
"typescript": "^5.7.4",
"wrangler": "^4.60.0"
}
}
冒頭の説明書きにもあった通り、デプロイ関連は OpenNext アダプターにより提供されているようだ。それ以外は普通の Next.js プロジェクトだ。
OpenNext に関連がありそうなファイルを見つけた。
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
export default defineCloudflareConfig({
// Uncomment to enable R2 cache,
// It should be imported as:
// `import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";`
// See https://opennext.js.org/cloudflare/caching for more details
// incrementalCache: r2IncrementalCache,
});
OpenNext の設定ファイルだろう。設定は何もない。「R2 キャッシュを有効にするにはコメントを外すこと」のコメントが書かれている。
Wrangler の設定ファイルも見つけた。
/**
* For more details on how to configure Wrangler, refer to:
* https://developers.cloudflare.com/workers/wrangler/configuration/
*/
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "cloudflare-nextjs-example",
"main": ".open-next/worker.js",
"compatibility_date": "2025-12-01",
"compatibility_flags": [
"nodejs_compat",
"global_fetch_strictly_public"
],
"assets": {
"binding": "ASSETS",
"directory": ".open-next/assets"
},
"images": {
// Enable image optimization
// see https://opennext.js.org/cloudflare/howtos/image
"binding": "IMAGES"
},
"services": [
{
// Self-reference service binding, the service name must match the worker name
// see https://opennext.js.org/cloudflare/caching
"binding": "WORKER_SELF_REFERENCE",
"service": "cloudflare-nextjs-example"
}
],
"observability": {
"enabled": true
}
/**
* Smart Placement
* https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
*/
// "placement": { "mode": "smart" }
/**
* Bindings
* Bindings allow your Worker to interact with resources on the Cloudflare Developer Platform, including
* databases, object storage, AI inference, real-time communication and more.
* https://developers.cloudflare.com/workers/runtime-apis/bindings/
*/
/**
* Environment Variables
* https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables
* Note: Use secrets to store sensitive data.
* https://developers.cloudflare.com/workers/configuration/secrets/
*/
// "vars": { "MY_VARIABLE": "production_value" }
}
Cloudflare の CLI ツールである Wrangler の設定が記述されている。
What's happening behind the scenes?
このコマンドにより次のことが実行される。
- C3 が新しいプロジェクトディレクトリを作成する
- Next.jsの公式セットアップツール を起動する
- Cloudflare向けにプロジェクトを設定する
- アプリケーションを Cloudflare に即時デプロイするオプションを提供する
Develop locally
ローカルで開発サーバーを起動しよう。
❯ npm run dev
> cloudflare-nextjs-example@0.1.0 dev
> next dev
▲ Next.js 16.1.4 (Turbopack)
- Local: http://localhost:3000
- Network: http://10.0.41.196:3000
✓ Starting...
Using vars defined in .dev.vars
✓ Ready in 844ms
開発サーバーを起動できた。localhost:3000 にアクセスしよう。

src/app/page.tsx の内容が表示された。
Test and preview your site
サイトのプレビューを表示してみよう。
❯ npm run preview
> cloudflare-nextjs-example@0.1.0 preview
> opennextjs-cloudflare build && opennextjs-cloudflare preview
┌─────────────────────────────┐
│ OpenNext — Cloudflare build │
└─────────────────────────────┘
App directory: /Users/ohira/ghq/github.com/HitoroOhria/cloudflare-nextjs-example/cloudflare-nextjs-example
Next.js version : 16.1.4
@opennextjs/cloudflare version: 1.15.1
@opennextjs/aws version: 3.9.12
┌─────────────────────────────────┐
│ OpenNext — Building Next.js app │
└─────────────────────────────────┘
> cloudflare-nextjs-example@0.1.0 build
> next build
▲ Next.js 16.1.4 (Turbopack)
Using vars defined in .dev.vars
Creating an optimized production build ...
Using vars defined in .dev.vars
✓ Compiled successfully in 2.3s
✓ Finished TypeScript in 1526.2ms
✓ Collecting page data using 7 workers in 168.7ms
✓ Generating static pages using 7 workers (3/3) in 151.3ms
✓ Finalizing page optimization in 169.8ms
Route (app)
┌ ○ /
└ ○ /_not-found
○ (Static) prerendered as static content
┌──────────────────────────────┐
│ OpenNext — Generating bundle │
└──────────────────────────────┘
Bundling middleware function...
Bundling static assets...
Bundling cache assets...
Building server function: default...
Applying code patches: 1.062s
# copyPackageTemplateFiles
⚙️ Bundling the OpenNext server...
Worker saved in `.open-next/worker.js` 🚀
OpenNext build complete.
┌───────────────────────────────┐
│ OpenNext — Cloudflare preview │
└───────────────────────────────┘
Using vars defined in .dev.vars
Incremental cache does not need populating
Tag cache does not need populating
Cloudflare collects anonymous telemetry about your usage of Wrangler. Learn more at https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler/telemetry.md
⛅️ wrangler 4.60.0
───────────────────
Using vars defined in .dev.vars
Your Worker has access to the following bindings:
Binding Resource Mode
env.WORKER_SELF_REFERENCE (cloudflare-nextjs-example) Worker local [connected]
env.IMAGES Images local
env.ASSETS Assets local
env.NEXTJS_ENV ("(hidden)") Environment Variable local
Service bindings, Durable Object bindings, and Tail consumers connect to other Wrangler or Vite dev processes running locally, with their connection status indicated by [connected] or [not connected]. For more details, refer to https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#local-development
╭──────────────────────────────────────────────────────────────────────╮
│ [b] open a browser [d] open devtools [c] clear console [x] to exit │
╰──────────────────────────────────────────────────────────────────────╯
⎔ Starting local server...
[wrangler:info] ✨ Parsed 1 valid header rule.
[wrangler:info] Ready on http://localhost:8787
プレビュー用のサーバーを起動できたようだ。localhost:8787 へアクセスしよう。

開発サーバーと同じ内容が表示されている。
What's the difference between dev and preview?
開発サーバーとプレビューサーバーの違いは次のとおりだ。
- 実行環境の違い
-
npm run dev: Next.js 開発サーバー -
npm run preview: Node.js
-
また、統合テストの実行やアプリケーションのプレビュー時には、プレビューサーバーを使用するべきだと書かれている。
- デプロイされたアプリケーションは Cloudflare Workers 上で実行され、
workerdランタイムを使用する -
wrangler devはworkerdランタイムを使用する -
npm run previewはwrangler devを使用する
Deploy your project
アプリケーションをデプロイしよう。
- デプロイに使用できるもの
- ローカルマシン
-
Workers Builds
- 「ビルド変数とシークレット」 セクションで環境変数を設定すること
- Next ビルドが、Next.js のパブリック変数と非パブリック変数へのアクセス権を確保できる
- 詳細は OpenNext 環境変数ガイド を確認すること
- 「ビルド変数とシークレット」 セクションで環境変数を設定すること
- 任意の CI/CD システム
- 必ずデプロイコマンドを更新すること
- デプロイ可能なドメイン
-
*.workers.devサブドメイン - カスタムドメイン
-
❯ npm run deploy
> cloudflare-nextjs-example@0.1.0 deploy
> opennextjs-cloudflare build && opennextjs-cloudflare deploy
┌─────────────────────────────┐
│ OpenNext — Cloudflare build │
└─────────────────────────────┘
App directory: /Users/ohira/ghq/github.com/HitoroOhria/cloudflare-nextjs-example/cloudflare-nextjs-example
Next.js version : 16.1.4
@opennextjs/cloudflare version: 1.15.1
@opennextjs/aws version: 3.9.12
┌─────────────────────────────────┐
│ OpenNext — Building Next.js app │
└─────────────────────────────────┘
> cloudflare-nextjs-example@0.1.0 build
> next build
▲ Next.js 16.1.4 (Turbopack)
Using vars defined in .dev.vars
Creating an optimized production build ...
Using vars defined in .dev.vars
✓ Compiled successfully in 2.6s
✓ Finished TypeScript in 1481.0ms
✓ Collecting page data using 7 workers in 165.9ms
✓ Generating static pages using 7 workers (3/3) in 154.2ms
✓ Finalizing page optimization in 153.6ms
Route (app)
┌ ○ /
└ ○ /_not-found
○ (Static) prerendered as static content
┌──────────────────────────────┐
│ OpenNext — Generating bundle │
└──────────────────────────────┘
Bundling middleware function...
Bundling static assets...
Bundling cache assets...
Building server function: default...
Applying code patches: 1.036s
# copyPackageTemplateFiles
⚙️ Bundling the OpenNext server...
Worker saved in `.open-next/worker.js` 🚀
OpenNext build complete.
┌──────────────────────────────┐
│ OpenNext — Cloudflare deploy │
└──────────────────────────────┘
Using vars defined in .dev.vars
Incremental cache does not need populating
Tag cache does not need populating
⛅️ wrangler 4.60.0 (update available 4.61.1)
─────────────────────────────────────────────
🌀 Building list of assets...
✨ Read 31 files from the assets directory /Users/ohira/ghq/github.com/HitoroOhria/cloudflare-nextjs-example/cloudflare-nextjs-example/.open-next/assets
🌀 Starting asset upload...
🌀 Found 25 new or modified static assets to upload. Proceeding with upload...
+ /_next/static/Fr08bALf2oNywLq61OMOm/_clientMiddlewareManifest.json
+ /_next/static/Fr08bALf2oNywLq61OMOm/_buildManifest.js
+ /file.svg
+ /favicon.svg
+ /_next/static/media/bbc41e54d2fcbd21-s.799d8ef8.woff2
+ /_next/static/chunks/2cd4ebbd91fba4de.js
+ /_next/static/media/caa3a2e1cccd8315-s.p.853070df.woff2
+ /_next/static/chunks/a6dad97d9634a72d.js
+ /_next/static/chunks/773abe17875a49cc.js
+ /BUILD_ID
+ /_next/static/chunks/ff1a16fafef87110.js
+ /globe.svg
+ /_next/static/chunks/turbopack-8664c4d15bbb2ffb.js
+ /_next/static/media/8a480f0b521d4e75-s.8e0177b5.woff2
+ /_next/static/media/7178b3e590c64307-s.b97b3418.woff2
+ /_next/static/chunks/d2be314c3ece3fbe.js
+ /_next/static/chunks/a6dad97d9634a72d.js.map
+ /_next/static/Fr08bALf2oNywLq61OMOm/_ssgManifest.js
+ /window.svg
+ /next.svg
+ /_next/static/media/4fa387ec64143e14-s.c1fdd6c2.woff2
+ /_next/static/chunks/691d94dd82a9a30b.css
+ /_next/static/chunks/21a0a4061f3ab64d.js
+ /_next/static/media/797e433ab948586e-s.p.dbea232f.woff2
+ /_next/static/chunks/fb6efb343d9465e5.js
Uploaded 8 of 25 assets
Uploaded 16 of 25 assets
Uploaded 25 of 25 assets
✨ Success! Uploaded 25 files (2.05 sec)
Total Upload: 6106.24 KiB / gzip: 1530.52 KiB
Worker Startup Time: 31 ms
Your Worker has access to the following bindings:
Binding Resource
env.WORKER_SELF_REFERENCE (cloudflare-nextjs-example) Worker
env.IMAGES Images
env.ASSETS Assets
Uploaded cloudflare-nextjs-example (9.15 sec)
Deployed cloudflare-nextjs-example triggers (1.09 sec)
https://cloudflare-nextjs-example.hiroto-ohira-freelance.workers.dev
Current Version ID: 4b0d3527-f490-4b21-ad4e-e733dc2f666b
https://cloudflare-nextjs-example.hiroto-ohira-freelance.workers.dev/ にデプロイされたようだ。アクセスしてみよう。

良さそうである!🎉
Deploy an existing Next.js project on Workers
既存の Next.js プロジェクトを Workers にデプロイする方法も紹介されていた。通常の Next.js プロジェクトと Workers + Next.js プロジェクトの違いがわかりやすいため、紹介する。
Install @opennextjs/cloudflare
OpenNext アダプターをインストールする。
npm i @opennextjs/cloudflare@latest
Install wrangler CLI
Wrangler をインストールする。
npm i -D wrangler@latest
Add a Wrangler configuration file
プロジェクトルートに Wrangler 設定ファイルを作成する。
{
"$schema": "./node_modules/wrangler/config-schema.json",
"main": ".open-next/worker.js",
"name": "my-app",
"compatibility_date": "2026-01-24",
"compatibility_flags": [
"nodejs_compat"
],
"assets": {
"directory": ".open-next/assets",
"binding": "ASSETS"
}
}
-
nodejs_compat互換性フラグ を有効化する - 互換性日付を
2024-09-23以降に設定する
シンプルな設定だ。バインディングには assets のみが指定されている。
Add a configuration file for OpenNext
プロジェクトのルートディレクトリに OpenNext 設定ファイルを作成する。
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
export default defineCloudflareConfig();
- キャッシュ設定 が可能である
Update package.json
以下のスクリプトを package.json に追加する。
{
"preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview",
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy",
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
}
本チュートリアルでは cf-typegen コマンドは出てこなかった。これは Cloudflare Workers の型定義ファイルを生成するコマンドだ。
Develop locally
開発サーバーを起動する。
❯ npm run dev
Test your site with the Cloudflare adapter
プレビューサーバーを起動する。
❯ npm run preview
Deploy your project
アプリケーションをデプロイする。
❯ npm run deploy
おわりに
意外と簡単にデプロイできてビックリしました。Cloudflare はデプロイまで簡単で良いですね。
Workers は workerd というランタイムを使用しているため、実行時に Next.js との親和性がどの程度あるかは未知数です。ここは開発しながら体感できればと思います。(「おまけ」セクションで深堀りしています。)
どなたかのお役に立てましたら幸いです。
おまけ
副次的に、Workers では workerd というランタイムを使用していることが判明しました。次の記事で深堀りしました。
Discussion