diff --git a/.changeset/gold-mirrors-hide.md b/.changeset/gold-mirrors-hide.md new file mode 100644 index 0000000..db8830e --- /dev/null +++ b/.changeset/gold-mirrors-hide.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +TODO: add a proper changeset diff --git a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts index 4e82d50..2ac2179 100644 --- a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts +++ b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts @@ -2,6 +2,8 @@ import path from "node:path"; import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; +import { normalizePathForInlineCode } from "../../utils/normalize-path-for-inline-code.js"; + /** * Sets up the OpenNext cache handler in a Next.js build. * @@ -19,12 +21,12 @@ export async function patchCache(code: string, openNextOptions: BuildOptions): P // TODO: switch to cache.mjs const outputPath = path.join(outputDir, "server-functions", "default"); const packagePath = path.relative(monorepoRoot, appBuildOutputPath); - const cacheFile = path.join(outputPath, packagePath, "cache.cjs"); + const cacheFilePath = path.join(outputPath, packagePath, "cache.cjs"); return code.replace( "const { cacheHandler } = this.nextConfig;", `const cacheHandler = null; -CacheHandler = require('${cacheFile}').default; +CacheHandler = require('${normalizePathForInlineCode(cacheFilePath)}').default; ` ); } diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts new file mode 100644 index 0000000..78bc4b3 --- /dev/null +++ b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts @@ -0,0 +1,10 @@ +import { describe, expect, it } from "vitest"; + +import { normalizePathForInlineCode } from "./normalize-path-for-inline-code"; + +describe("normalizePathForInlineCode", () => { + it("should extract production env vars", () => { + const result = normalizePathForInlineCode("TODO"); + expect(result).toBeFalsy(); + }); +}); diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts new file mode 100644 index 0000000..624a946 --- /dev/null +++ b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts @@ -0,0 +1,16 @@ +import { normalizePath } from "./normalize-path.js"; + +/** + * TODO: add proper comment + * + * @param path + */ +export function normalizePathForInlineCode(path: string): string { + // let's normalize the path for good measure + const normalizedPath = normalizePath(path); + + // we need to escape + const doublyEscaped = normalizedPath.replaceAll("\\", "\\\\"); + + return doublyEscaped; +}