Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/puny-coins-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

In `migrate` command, avoid adding unnecessary newlines when creating files
5 changes: 5 additions & 0 deletions .changeset/salty-spoons-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

fix `migrate` command incorrectly erroring if the target application doesn't have a `public` directory
5 changes: 5 additions & 0 deletions .changeset/upset-squids-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

Fix `migrate` command incorrectly not updating Next.js config files (by bumping `@opennextjs/aws`)
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"@ast-grep/napi": "^0.40.5",
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "3.9.15",
Comment thread
dario-piotrowicz marked this conversation as resolved.
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@1114",
"cloudflare": "^4.4.1",
"enquirer": "^2.4.1",
"glob": "catalog:",
Expand Down
12 changes: 9 additions & 3 deletions packages/cloudflare/src/cli/build/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "node:fs";
import { dirname } from "node:path";
Comment thread
dario-piotrowicz marked this conversation as resolved.
Outdated

/**
* Appends text to a file
Expand All @@ -9,15 +10,20 @@ import fs from "node:fs";
* @param filepath The path to the file.
* @param text The text to append to the file.
* @param condition A function that receives the current file content and returns `true` if the text should be appended to it, the condition is skipped when the file is being created.
* @param separator A string that will be inserted between the pre-existing file's content and the new text in case the file already existed.
*/
export function conditionalAppendFileSync(
Comment thread
dario-piotrowicz marked this conversation as resolved.
filepath: string,
text: string,
condition: (fileContent: string) => boolean
condition: (fileContent: string) => boolean,
separator = ""
): void {
const fileExists = fs.existsSync(filepath);
const fileContent = fileExists ? fs.readFileSync(filepath, "utf8") : "";
Comment thread
dario-piotrowicz marked this conversation as resolved.
Outdated

if (!fileExists || condition(fs.readFileSync(filepath, "utf8"))) {
fs.appendFileSync(filepath, text);
if (!fileExists || condition(fileContent)) {
const dir = dirname(filepath);
fs.mkdirSync(dir, { recursive: true });
fs.appendFileSync(filepath, `${fileContent.length > 0 ? separator : ""}${text}`);
}
Comment thread
dario-piotrowicz marked this conversation as resolved.
Outdated
}
22 changes: 13 additions & 9 deletions packages/cloudflare/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,20 @@ async function migrateCommand(args: { forceInstall: boolean }): Promise<void> {
printStepTitle(`${devVarsExists ? "Updating" : "Creating"} .dev.vars file`);
conditionalAppendFileSync(
".dev.vars",
"\nNEXTJS_ENV=development\n",
(content) => !/\bNEXTJS_ENV\b/.test(content)
"NEXTJS_ENV=development\n",
(content) => !/\bNEXTJS_ENV\b/.test(content),
"\n"
);

printStepTitle(`${fs.existsSync("public/_headers") ? "Updating" : "Creating"} public/_headers file`);
conditionalAppendFileSync(
"public/_headers",
"\n\n# https://developers.cloudflare.com/workers/static-assets/headers\n" +
"# https://developers.cloudflare.com/workers/static-assets/headers\n" +
"# https://opennext.js.org/cloudflare/caching#static-assets-caching\n" +
"/_next/static/*\n" +
" Cache-Control: public,max-age=31536000,immutable\n\n",
(content) => !/^\/_next\/static\/*\b/.test(content)
" Cache-Control: public,max-age=31536000,immutable\n",
(content) => !/^\/_next\/static\/*\b/.test(content),
"\n\n"
);

printStepTitle("Updating package.json scripts");
Expand Down Expand Up @@ -123,15 +125,17 @@ async function migrateCommand(args: { forceInstall: boolean }): Promise<void> {
printStepTitle(`${gitIgnoreExists ? "Updating" : "Creating"} .gitignore file`);
conditionalAppendFileSync(
".gitignore",
"\n# OpenNext\n.open-next\n",
(content) => !content.includes(".open-next")
"# OpenNext\n.open-next\n",
(content) => !content.includes(".open-next"),
"\n"
);

printStepTitle("Updating Next.js config");
conditionalAppendFileSync(
findNextConfig({ appPath: projectDir })!,
"\nimport('@opennextjs/cloudflare').then(m => m.initOpenNextCloudflareForDev());\n",
(content) => !content.includes("initOpenNextCloudflareForDev")
"import('@opennextjs/cloudflare').then(m => m.initOpenNextCloudflareForDev());\n",
(content) => !content.includes("initOpenNextCloudflareForDev"),
"\n"
);

printStepTitle("Checking for edge runtime usage");
Expand Down
Loading
Loading