Skip to content

Add migrate command for initializing open-next for existing Next.js projects#1083

Merged
vicb merged 75 commits intomainfrom
dario/DEVX-2383/init-command
Jan 26, 2026
Merged

Add migrate command for initializing open-next for existing Next.js projects#1083
vicb merged 75 commits intomainfrom
dario/DEVX-2383/init-command

Conversation

@dario-piotrowicz
Copy link
Copy Markdown
Contributor

@dario-piotrowicz dario-piotrowicz commented Jan 21, 2026

Fixes https://jira.cfdata.org/browse/DEVX-2383


Docs PR: opennextjs/docs#205


This PR addes a new migrate CLI command that adds the OpenNext adapter to standard Next.js projects (this basically automates the steps in https://opennext.js.org/cloudflare/get-started).


You can manually test this command by running:

npm create next-app  my-next-app

to create a new Next.js project, cd into the project:

cd my-next-app

install a compatible version of Next.js:

npm i next@16.0.x

and then run the init command:

npx https://pkg.pr.new/@opennextjs/cloudflare@1083 migrate

You should see something like the following:
Screenshot 2026-01-22 at 14 36 24


Thanks to @2u841r to spearheading this effort 🫶

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jan 21, 2026

🦋 Changeset detected

Latest commit: 608b497

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 21, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@1083

commit: 608b497

Comment thread .changeset/add-init-command.md Outdated
Comment thread pnpm-lock.yaml
@dario-piotrowicz dario-piotrowicz marked this pull request as ready for review January 22, 2026 11:27
Comment thread .changeset/add-init-command.md Outdated
@dario-piotrowicz
Copy link
Copy Markdown
Contributor Author

@vicb right now if an incorrect version of Next.js is used we get this:
Screenshot 2026-01-22 at 14 41 43

probably we should check the version and handle it more gracefully, right?

or shall we even try to bump the version? what do you think?

Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/migrate.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/utils/wrangler-config.ts
Copy link
Copy Markdown
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of nits.

Also I realize that not having a monorepo makes it much harder to discover utils.

Should hopefully be fixed soon

Comment thread .changeset/add-init-command.md Outdated
Comment thread packages/cloudflare/src/cli/build/utils/create-config-files.ts
Comment thread packages/cloudflare/src/cli/build/utils/create-config-files.ts Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about moving the detection than for the wrangler config

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work?

4886b79

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works.

I usually add a "maybe" in the mix if it can return undefined or throw when there is no "maybe"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also would be nice if the API is consistent for the different files

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually add a "maybe" in the mix if it can return undefined or throw when there is no "maybe"

The return type make it clear to me so adding a maybe feels unnecessary to me, but I'm happy to add that since it's your preference 👍

Also would be nice if the API is consistent for the different files

I thought the same when implementing the two solutions... but having a return from a function that is never actually used feels ugly/potentially confusing to me, but it's not a huge deal, I'm totally happy to use a consistent API 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type make it clear to me so adding a maybe feels unnecessary to me, but I'm happy to add that since it's your preference 👍

When you review code on GH, you don't get to see the return type easily

I thought the same when implementing the two solutions... but having a return from a function that is never actually used feels ugly/potentially confusing to me, but it's not a huge deal, I'm totally happy to use a consistent API 👍

I need to double check the code here, I thought it was used

Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/utils/create-open-next-config.ts Outdated
Comment thread packages/cloudflare/src/cli/build/utils/create-config-files.ts Outdated
Comment thread packages/cloudflare/src/cli/build/utils/create-config-files.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/migrate.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/migrate.ts
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/migrate.ts
Comment thread packages/cloudflare/src/cli/commands/init.ts Outdated
@vicb
Copy link
Copy Markdown
Contributor

vicb commented Jan 23, 2026

Could you please merge master in this PR instead of rebasing?
I realize that I have been commenting the same thing multiple time because it doesn't show up in the diff view.
GH is a pain here :(

@dario-piotrowicz dario-piotrowicz marked this pull request as draft January 23, 2026 11:09
@dario-piotrowicz dario-piotrowicz force-pushed the dario/DEVX-2383/init-command branch from 8bc4f63 to 87e3b01 Compare January 23, 2026 11:13
@dario-piotrowicz dario-piotrowicz changed the title Add init command for initializing open-next for existing Next.js projects Add migrate command for initializing open-next for existing Next.js projects Jan 24, 2026
@vicb vicb marked this pull request as ready for review January 24, 2026 21:21
Comment thread .changeset/add-init-command.md Outdated
@vicb vicb force-pushed the dario/DEVX-2383/init-command branch from fb72a97 to 608b497 Compare January 26, 2026 21:46
@vicb vicb merged commit b062597 into main Jan 26, 2026
7 checks passed
@vicb vicb deleted the dario/DEVX-2383/init-command branch January 26, 2026 21:57
conico974 added a commit to opennextjs/adapters-api that referenced this pull request Mar 21, 2026
conico974 added a commit to opennextjs/adapters-api that referenced this pull request Mar 29, 2026
* Add SKILL.md for porting PRs and AGENTS.md for coding guidelines

* update skill

* Port opennextjs/opennextjs-aws#1118 as a test

* Port opennextjs/opennextjs-aws#1117

* update skill

* Port opennextjs/opennextjs-aws#1114

* Port PR opennextjs/opennextjs-aws#1107

* update skills

* Port PR opennextjs/opennextjs-aws#1108

* Port PR opennextjs/opennextjs-aws#1104

* Port PR opennextjs/opennextjs-aws#1101

* Port PR opennextjs/opennextjs-aws#1098

* chore: port PR #1083 from source repository

opennextjs/opennextjs-cloudflare#1083

Changeset: .changeset/port-pr-1083.md

* chore: port PR #1105 from source repository

opennextjs/opennextjs-cloudflare#1105

Changeset: .changeset/port-pr-1105.md

* chore: port PR #1097 from source repository

opennextjs/opennextjs-cloudflare#1097

Changeset: .changeset/port-pr-1097.md

* chore: port PR #1122 from source repository

opennextjs/opennextjs-cloudflare#1122

Applied bugfixes and improvements to the 'migrate' command:
- Fixed extra newlines when appending to files (updated conditionalAppendFileSync signature)
- Fixed error when 'public' directory is missing (creates parent directories automatically)
- Fixed Next.js config file update to check if the file exists first
- Updated checkRunningInsideNextjsApp to accept { appPath: string } instead of full BuildOptions

Changesets:
- .changeset/port-pr-1122-cloudflare.md
- .changeset/port-pr-1122-aws.md

* chore: update port PR skill instructions for staging and committing changes

* chore: port PR #1126 from source repository

opennextjs/opennextjs-cloudflare#1126

Fix: prevent Worker hang on HEAD requests to static assets

When run_worker_first is enabled, HEAD requests to static assets hang
the Worker because response.body is null (per HTTP spec) and the
fallback new ReadableStream() creates a stream that never closes.

Changes:
- Return null body for HEAD requests instead of falling through to
the hanging ReadableStream fallback
- Add tests for maybeGetAssetResult covering GET, HEAD, 404, POST,
and run_worker_first=false cases

Changeset: .changeset/port-pr-1126.md

* linting

* chore: port PR #1127 from source repository

opennextjs/opennextjs-cloudflare#1127

Changeset: .changeset/port-pr-1127.md

* chore: port PR #1138 from source repository

opennextjs/opennextjs-cloudflare#1138

Changeset: .changeset/port-pr-1138.md

* chore: port PR #1133 from source repository

opennextjs/opennextjs-cloudflare#1133

Changeset: .changeset/port-pr-1133.md

Update the migrate command to attempt to create an R2 bucket for caching
as part of the migration process, if that is not possible an application
without caching enabled will be generated instead.

* chore: port PR #1142 from source repository

opennextjs/opennextjs-cloudflare#1142

Changeset: .changeset/port-pr-1142.md

* chore: port PR #1147 from source repository

opennextjs/opennextjs-cloudflare#1147

make dev /cdn-cgi/image behaves like prod for consistency

Changeset: .changeset/port-pr-1147.md

* chore: port PR #1150 from source repository

opennextjs/opennextjs-cloudflare#1150

Changeset: .changeset/port-pr-1150.md

* fix lockfile

* fix test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants