@opennextjs/cloudflare 1.0.0-beta.2 → 1.0.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/api/config.js +2 -0
  2. package/dist/api/overrides/incremental-cache/kv-incremental-cache.d.ts +5 -0
  3. package/dist/api/overrides/incremental-cache/kv-incremental-cache.js +10 -3
  4. package/dist/api/overrides/incremental-cache/r2-incremental-cache.d.ts +6 -0
  5. package/dist/api/overrides/incremental-cache/r2-incremental-cache.js +11 -2
  6. package/dist/api/overrides/incremental-cache/regional-cache.d.ts +17 -2
  7. package/dist/api/overrides/incremental-cache/regional-cache.js +52 -16
  8. package/dist/api/overrides/incremental-cache/static-assets-incremental-cache.js +0 -1
  9. package/dist/api/overrides/tag-cache/d1-next-tag-cache.d.ts +0 -1
  10. package/dist/api/overrides/tag-cache/d1-next-tag-cache.js +3 -8
  11. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.d.ts +1 -1
  12. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.js +13 -10
  13. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.spec.js +2 -4
  14. package/dist/api/overrides/tag-cache/tag-cache-filter.d.ts +26 -0
  15. package/dist/api/overrides/tag-cache/tag-cache-filter.js +41 -0
  16. package/dist/api/overrides/tag-cache/tag-cache-filter.spec.js +96 -0
  17. package/dist/cli/args.d.ts +1 -1
  18. package/dist/cli/args.js +2 -1
  19. package/dist/cli/build/build.js +4 -1
  20. package/dist/cli/build/bundle-server.js +3 -42
  21. package/dist/cli/build/open-next/compile-env-files.js +1 -1
  22. package/dist/cli/build/open-next/compile-init.d.ts +5 -0
  23. package/dist/cli/build/open-next/compile-init.js +27 -0
  24. package/dist/cli/build/open-next/createServerBundle.js +7 -2
  25. package/dist/cli/build/patches/plugins/pages-router-context.d.ts +11 -0
  26. package/dist/cli/build/patches/plugins/pages-router-context.js +32 -0
  27. package/dist/cli/build/utils/ensure-cf-config.js +2 -0
  28. package/dist/cli/commands/populate-cache.d.ts +7 -0
  29. package/dist/cli/commands/populate-cache.js +51 -23
  30. package/dist/cli/commands/populate-cache.spec.d.ts +1 -0
  31. package/dist/cli/commands/populate-cache.spec.js +61 -0
  32. package/dist/cli/commands/upload.d.ts +5 -0
  33. package/dist/cli/commands/upload.js +9 -0
  34. package/dist/cli/index.js +3 -0
  35. package/dist/cli/templates/init.d.ts +13 -0
  36. package/dist/cli/templates/init.js +105 -0
  37. package/dist/cli/templates/worker.d.ts +3 -1
  38. package/dist/cli/templates/worker.js +5 -53
  39. package/package.json +3 -3
  40. package/templates/open-next.config.ts +2 -2
  41. package/templates/wrangler.jsonc +9 -7
  42. package/dist/cli/build/patches/plugins/next-minimal.d.ts +0 -4
  43. package/dist/cli/build/patches/plugins/next-minimal.js +0 -86
  44. package/dist/cli/build/patches/plugins/next-minimal.spec.js +0 -71
  45. /package/dist/{cli/build/patches/plugins/next-minimal.spec.d.ts → api/overrides/tag-cache/tag-cache-filter.spec.d.ts} +0 -0
@@ -14,8 +14,8 @@ import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js";
14
14
  import { inlineFindDir } from "./patches/plugins/find-dir.js";
15
15
  import { patchInstrumentation } from "./patches/plugins/instrumentation.js";
16
16
  import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
17
- import { patchNextMinimal } from "./patches/plugins/next-minimal.js";
18
17
  import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
18
+ import { patchPagesRouterContext } from "./patches/plugins/pages-router-context.js";
19
19
  import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
20
20
  import { fixRequire } from "./patches/plugins/require.js";
21
21
  import { shimRequireHook } from "./patches/plugins/require-hook.js";
@@ -78,12 +78,12 @@ export async function bundleServer(buildOpts) {
78
78
  fixRequire(updater),
79
79
  handleOptionalDependencies(optionalDependencies),
80
80
  patchInstrumentation(updater, buildOpts),
81
+ patchPagesRouterContext(buildOpts),
81
82
  inlineEvalManifest(updater, buildOpts),
82
83
  inlineFindDir(updater, buildOpts),
83
84
  inlineLoadManifest(updater, buildOpts),
84
85
  inlineBuildId(updater),
85
86
  patchDepdDeprecations(updater),
86
- patchNextMinimal(updater),
87
87
  // Apply updater updates, must be the last plugin
88
88
  updater.plugin,
89
89
  ],
@@ -125,46 +125,6 @@ export async function bundleServer(buildOpts) {
125
125
  "process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
126
126
  },
127
127
  platform: "node",
128
- banner: {
129
- js: `
130
- // Used by unbundled js files (which don't inherit the __dirname present in the define field)
131
- // so we also need to set it on the global scope
132
- // Note: this was hit in the next/dist/compiled/@opentelemetry/api module
133
- globalThis.__dirname ??= "";
134
- globalThis.__filename ??= "";
135
-
136
- // Do not crash on cache not supported
137
- // https://github.com/cloudflare/workerd/pull/2434
138
- // compatibility flag "cache_option_enabled" -> does not support "force-cache"
139
- const curFetch = globalThis.fetch;
140
- globalThis.fetch = (input, init) => {
141
- if (init) {
142
- delete init.cache;
143
- }
144
- return curFetch(input, init);
145
- };
146
- import __cf_stream from 'node:stream';
147
- fetch = globalThis.fetch;
148
- const CustomRequest = class extends globalThis.Request {
149
- constructor(input, init) {
150
- if (init) {
151
- delete init.cache;
152
- // https://github.com/cloudflare/workerd/issues/2746
153
- // https://github.com/cloudflare/workerd/issues/3245
154
- Object.defineProperty(init, "body", {
155
- value: init.body instanceof __cf_stream.Readable ? ReadableStream.from(init.body) : init.body
156
- });
157
- }
158
- super(input, init);
159
- }
160
- };
161
- globalThis.Request = CustomRequest;
162
- Request = globalThis.Request;
163
- // Makes the edge converter returns either a Response or a Request.
164
- globalThis.__dangerous_ON_edge_converter_returns_request = true;
165
- globalThis.__BUILD_TIMESTAMP_MS__ = ${Date.now()};
166
- `,
167
- },
168
128
  });
169
129
  fs.writeFileSync(openNextServerBundle + ".meta.json", JSON.stringify(result.metafile, null, 2));
170
130
  await updateWorkerBundledCode(openNextServerBundle, buildOpts);
@@ -185,6 +145,7 @@ export async function updateWorkerBundledCode(workerOutputFile, buildOpts) {
185
145
  [
186
146
  "'require(this.middlewareManifestPath)'",
187
147
  (code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
148
+ { isOptional: true },
188
149
  ],
189
150
  [
190
151
  "`require.resolve` call",
@@ -5,7 +5,7 @@ import { extractProjectEnvVars } from "../utils/index.js";
5
5
  * Compiles the values extracted from the project's env files to the output directory for use in the worker.
6
6
  */
7
7
  export function compileEnvFiles(buildOpts) {
8
- const envDir = path.join(buildOpts.outputDir, "env");
8
+ const envDir = path.join(buildOpts.outputDir, "cloudflare");
9
9
  fs.mkdirSync(envDir, { recursive: true });
10
10
  ["production", "development", "test"].forEach((mode) => fs.appendFileSync(path.join(envDir, `next-env.mjs`), `export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, buildOpts))};\n`));
11
11
  }
@@ -0,0 +1,5 @@
1
+ import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
2
+ /**
3
+ * Compiles the initialization code for the workerd runtime
4
+ */
5
+ export declare function compileInit(options: BuildOptions): Promise<void>;
@@ -0,0 +1,27 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ import { loadConfig } from "@opennextjs/aws/adapters/config/util.js";
4
+ import { build } from "esbuild";
5
+ /**
6
+ * Compiles the initialization code for the workerd runtime
7
+ */
8
+ export async function compileInit(options) {
9
+ const currentDir = path.join(path.dirname(fileURLToPath(import.meta.url)));
10
+ const templatesDir = path.join(currentDir, "../../templates");
11
+ const initPath = path.join(templatesDir, "init.js");
12
+ const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
13
+ const basePath = nextConfig.basePath ?? "";
14
+ await build({
15
+ entryPoints: [initPath],
16
+ outdir: path.join(options.outputDir, "cloudflare"),
17
+ bundle: false,
18
+ minify: false,
19
+ format: "esm",
20
+ target: "esnext",
21
+ platform: "node",
22
+ define: {
23
+ __BUILD_TIMESTAMP_MS__: JSON.stringify(Date.now()),
24
+ __NEXT_BASE_PATH__: JSON.stringify(basePath),
25
+ },
26
+ });
27
+ }
@@ -10,8 +10,9 @@ import { copyMiddlewareResources, generateEdgeBundle } from "@opennextjs/aws/bui
10
10
  import * as buildHelper from "@opennextjs/aws/build/helper.js";
11
11
  import { installDependencies } from "@opennextjs/aws/build/installDeps.js";
12
12
  import { applyCodePatches } from "@opennextjs/aws/build/patch/codePatcher.js";
13
- import { patchFetchCacheForISR, patchUnstableCacheForISR, } from "@opennextjs/aws/build/patch/patchFetchCacheISR.js";
14
- import { patchFetchCacheSetMissingWaitUntil } from "@opennextjs/aws/build/patch/patchFetchCacheWaitUntil.js";
13
+ import { patchEnvVars, patchFetchCacheForISR, patchFetchCacheSetMissingWaitUntil, patchNextServer, patchUnstableCacheForISR, } from "@opennextjs/aws/build/patch/patches/index.js";
14
+ // TODO: import from patches/index.js when https://github.com/opennextjs/opennextjs-aws/pull/827 is released
15
+ import { patchBackgroundRevalidation } from "@opennextjs/aws/build/patch/patches/patchBackgroundRevalidation.js";
15
16
  import logger from "@opennextjs/aws/logger.js";
16
17
  import { minifyAll } from "@opennextjs/aws/minimize-js.js";
17
18
  import { openNextEdgePlugins } from "@opennextjs/aws/plugins/edge.js";
@@ -124,6 +125,9 @@ async function generateBundle(name, options, fnOptions, codeCustomization) {
124
125
  patchFetchCacheSetMissingWaitUntil,
125
126
  patchFetchCacheForISR,
126
127
  patchUnstableCacheForISR,
128
+ patchNextServer,
129
+ patchEnvVars,
130
+ patchBackgroundRevalidation,
127
131
  // Cloudflare specific patches
128
132
  patchResRevalidate,
129
133
  ...additionalCodePatches,
@@ -147,6 +151,7 @@ async function generateBundle(name, options, fnOptions, codeCustomization) {
147
151
  ...(disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : []),
148
152
  ...(disableRouting ? ["withRouting"] : []),
149
153
  ...(isAfter142 ? ["patchAsyncStorage"] : []),
154
+ ...(isAfter141 ? ["appendPrefetch"] : []),
150
155
  ],
151
156
  }),
152
157
  openNextReplacementPlugin({
@@ -0,0 +1,11 @@
1
+ /**
2
+ * ESBuild plugin to handle pages router context.
3
+ *
4
+ * We need to change the import path for the pages router context to use the one provided in `pages-runtime.prod.js`
5
+ */
6
+ import { BuildOptions } from "@opennextjs/aws/build/helper.js";
7
+ import type { PluginBuild } from "esbuild";
8
+ export declare function patchPagesRouterContext(buildOpts: BuildOptions): {
9
+ name: string;
10
+ setup: (build: PluginBuild) => void;
11
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * ESBuild plugin to handle pages router context.
3
+ *
4
+ * We need to change the import path for the pages router context to use the one provided in `pages-runtime.prod.js`
5
+ */
6
+ import { compareSemver } from "@opennextjs/aws/build/helper.js";
7
+ export function patchPagesRouterContext(buildOpts) {
8
+ const pathRegex = /^.*\/(?<CONTEXT>.*)\.shared-runtime$/;
9
+ const isAfter15 = compareSemver(buildOpts.nextVersion, ">=", "15.0.0");
10
+ const isAfter153 = compareSemver(buildOpts.nextVersion, ">=", "15.3.0");
11
+ const basePath = `next/dist/server/${isAfter15 ? "" : "future/"}route-modules/pages/vendored/contexts/`;
12
+ return {
13
+ name: "pages-router-context",
14
+ setup: (build) => {
15
+ // If we are after 15.3, we don't need to patch the context anymore
16
+ if (isAfter153) {
17
+ return;
18
+ }
19
+ // We need to modify some imports (i.e. https://github.com/vercel/next.js/blob/48540b836642525b38a2cba40a92b4532c553a52/packages/next/src/server/require-hook.ts#L59-L68)
20
+ build.onResolve({ filter: /.*shared-runtime/ }, async ({ path, resolveDir, ...options }) => {
21
+ const match = path.match(pathRegex);
22
+ if (match && match.groups?.CONTEXT) {
23
+ const newPath = `${basePath}${match.groups.CONTEXT}.js`;
24
+ return await build.resolve(newPath, {
25
+ resolveDir,
26
+ ...options,
27
+ });
28
+ }
29
+ });
30
+ },
31
+ };
32
+ }
@@ -17,6 +17,7 @@ export function ensureCloudflareConfig(config) {
17
17
  config.default?.override?.queue === "direct" ||
18
18
  typeof config.default?.override?.queue === "function",
19
19
  mwIsMiddlewareIntegrated: config.middleware === undefined,
20
+ hasCryptoExternal: config.edgeExternals?.includes("node:crypto"),
20
21
  };
21
22
  if (config.default?.override?.queue === "direct") {
22
23
  logger.warn("The direct mode queue is not recommended for use in production.");
@@ -34,6 +35,7 @@ export function ensureCloudflareConfig(config) {
34
35
  queue: "dummy" | "direct" | function,
35
36
  },
36
37
  },
38
+ edgeExternals: ["node:crypto"],
37
39
  }\n\n`.replace(/^ {8}/gm, ""));
38
40
  }
39
41
  }
@@ -1,6 +1,13 @@
1
1
  import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
2
2
  import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
3
3
  import type { WranglerTarget } from "../utils/run-wrangler.js";
4
+ export type CacheAsset = {
5
+ isFetch: boolean;
6
+ fullPath: string;
7
+ key: string;
8
+ buildId: string;
9
+ };
10
+ export declare function getCacheAssets(opts: BuildOptions): CacheAsset[];
4
11
  export declare function populateCache(options: BuildOptions, config: OpenNextConfig, populateCacheOptions: {
5
12
  target: WranglerTarget;
6
13
  environment?: string;
@@ -4,29 +4,49 @@ import logger from "@opennextjs/aws/logger.js";
4
4
  import { globSync } from "glob";
5
5
  import { tqdm } from "ts-tqdm";
6
6
  import { unstable_readConfig } from "wrangler";
7
- import { BINDING_NAME as KV_CACHE_BINDING_NAME, NAME as KV_CACHE_NAME, } from "../../api/overrides/incremental-cache/kv-incremental-cache.js";
8
- import { BINDING_NAME as R2_CACHE_BINDING_NAME, DEFAULT_PREFIX as R2_CACHE_DEFAULT_PREFIX, NAME as R2_CACHE_NAME, PREFIX_ENV_NAME as R2_CACHE_PREFIX_ENV_NAME, } from "../../api/overrides/incremental-cache/r2-incremental-cache.js";
7
+ import { BINDING_NAME as KV_CACHE_BINDING_NAME, computeCacheKey as computeKVCacheKey, NAME as KV_CACHE_NAME, } from "../../api/overrides/incremental-cache/kv-incremental-cache.js";
8
+ import { BINDING_NAME as R2_CACHE_BINDING_NAME, computeCacheKey as computeR2CacheKey, NAME as R2_CACHE_NAME, PREFIX_ENV_NAME as R2_CACHE_PREFIX_ENV_NAME, } from "../../api/overrides/incremental-cache/r2-incremental-cache.js";
9
9
  import { CACHE_DIR as STATIC_ASSETS_CACHE_DIR, NAME as STATIC_ASSETS_CACHE_NAME, } from "../../api/overrides/incremental-cache/static-assets-incremental-cache.js";
10
10
  import { BINDING_NAME as D1_TAG_BINDING_NAME, NAME as D1_TAG_NAME, } from "../../api/overrides/tag-cache/d1-next-tag-cache.js";
11
11
  import { runWrangler } from "../utils/run-wrangler.js";
12
12
  async function resolveCacheName(value) {
13
13
  return typeof value === "function" ? (await value()).name : value;
14
14
  }
15
- function getCacheAssetPaths(opts) {
16
- return globSync(path.join(opts.outputDir, "cache/**/*"), {
15
+ export function getCacheAssets(opts) {
16
+ const allFiles = globSync(path.join(opts.outputDir, "cache/**/*"), {
17
17
  withFileTypes: true,
18
18
  windowsPathsNoEscape: true,
19
- })
20
- .filter((f) => f.isFile())
21
- .map((f) => {
22
- const relativePath = path.relative(path.join(opts.outputDir, "cache"), f.fullpathPosix());
23
- return {
24
- fsPath: f.fullpathPosix(),
25
- destPath: relativePath.startsWith("__fetch")
26
- ? `${relativePath.replace("__fetch/", "")}.fetch`
27
- : relativePath,
28
- };
29
- });
19
+ }).filter((f) => f.isFile());
20
+ const assets = [];
21
+ for (const file of allFiles) {
22
+ const fullPath = file.fullpathPosix();
23
+ const relativePath = path.relative(path.join(opts.outputDir, "cache"), fullPath);
24
+ if (relativePath.startsWith("__fetch")) {
25
+ const [__fetch, buildId, ...keyParts] = relativePath.split("/");
26
+ if (__fetch !== "__fetch" || buildId === undefined || keyParts.length === 0) {
27
+ throw new Error(`Invalid path for a Cache Asset file: ${relativePath}`);
28
+ }
29
+ assets.push({
30
+ isFetch: true,
31
+ fullPath,
32
+ key: `/${keyParts.join("/")}`,
33
+ buildId,
34
+ });
35
+ }
36
+ else {
37
+ const [buildId, ...keyParts] = relativePath.slice(0, -".cache".length).split("/");
38
+ if (!relativePath.endsWith(".cache") || buildId === undefined || keyParts.length === 0) {
39
+ throw new Error(`Invalid path for a Cache Asset file: ${relativePath}`);
40
+ }
41
+ assets.push({
42
+ isFetch: false,
43
+ fullPath,
44
+ key: `/${keyParts.join("/")}`,
45
+ buildId,
46
+ });
47
+ }
48
+ }
49
+ return assets;
30
50
  }
31
51
  function populateR2IncrementalCache(options, populateCacheOptions) {
32
52
  logger.info("\nPopulating R2 incremental cache...");
@@ -39,10 +59,14 @@ function populateR2IncrementalCache(options, populateCacheOptions) {
39
59
  if (!bucket) {
40
60
  throw new Error(`R2 binding ${JSON.stringify(R2_CACHE_BINDING_NAME)} should have a 'bucket_name'`);
41
61
  }
42
- const assets = getCacheAssetPaths(options);
43
- for (const { fsPath, destPath } of tqdm(assets)) {
44
- const fullDestPath = path.join(bucket, process.env[R2_CACHE_PREFIX_ENV_NAME] ?? R2_CACHE_DEFAULT_PREFIX, destPath);
45
- runWrangler(options, ["r2 object put", JSON.stringify(fullDestPath), `--file ${JSON.stringify(fsPath)}`],
62
+ const assets = getCacheAssets(options);
63
+ for (const { fullPath, key, buildId, isFetch } of tqdm(assets)) {
64
+ const cacheKey = computeR2CacheKey(key, {
65
+ directory: process.env[R2_CACHE_PREFIX_ENV_NAME],
66
+ buildId,
67
+ isFetch,
68
+ });
69
+ runWrangler(options, ["r2 object put", JSON.stringify(path.join(bucket, cacheKey)), `--file ${JSON.stringify(fullPath)}`],
46
70
  // NOTE: R2 does not support the environment flag and results in the following error:
47
71
  // Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
48
72
  { target: populateCacheOptions.target, excludeRemoteFlag: true, logging: "error" });
@@ -56,13 +80,17 @@ function populateKVIncrementalCache(options, populateCacheOptions) {
56
80
  if (!binding) {
57
81
  throw new Error(`No KV binding ${JSON.stringify(KV_CACHE_BINDING_NAME)} found!`);
58
82
  }
59
- const assets = getCacheAssetPaths(options);
60
- for (const { fsPath, destPath } of tqdm(assets)) {
83
+ const assets = getCacheAssets(options);
84
+ for (const { fullPath, key, buildId, isFetch } of tqdm(assets)) {
85
+ const cacheKey = computeKVCacheKey(key, {
86
+ buildId,
87
+ isFetch,
88
+ });
61
89
  runWrangler(options, [
62
90
  "kv key put",
63
- JSON.stringify(destPath),
91
+ JSON.stringify(cacheKey),
64
92
  `--binding ${JSON.stringify(KV_CACHE_BINDING_NAME)}`,
65
- `--path ${JSON.stringify(fsPath)}`,
93
+ `--path ${JSON.stringify(fullPath)}`,
66
94
  ], { ...populateCacheOptions, logging: "error" });
67
95
  }
68
96
  logger.info(`Successfully populated cache with ${assets.length} assets`);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,61 @@
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ import mockFs from "mock-fs";
4
+ import { afterAll, beforeAll, describe, expect, test } from "vitest";
5
+ import { getCacheAssets } from "./populate-cache";
6
+ describe("getCacheAssets", () => {
7
+ beforeAll(() => {
8
+ mockFs();
9
+ const fetchBaseDir = "/base/path/cache/__fetch/buildID";
10
+ const cacheDir = "/base/path/cache/buildID/path/to";
11
+ mkdirSync(fetchBaseDir, { recursive: true });
12
+ mkdirSync(cacheDir, { recursive: true });
13
+ for (let i = 0; i < 3; i++) {
14
+ writeFileSync(path.join(fetchBaseDir, `${i}`), "", { encoding: "utf-8" });
15
+ writeFileSync(path.join(cacheDir, `${i}.cache`), "", { encoding: "utf-8" });
16
+ }
17
+ });
18
+ afterAll(() => mockFs.restore());
19
+ test("list cache assets", () => {
20
+ expect(getCacheAssets({ outputDir: "/base/path" })).toMatchInlineSnapshot(`
21
+ [
22
+ {
23
+ "buildId": "buildID",
24
+ "fullPath": "/base/path/cache/buildID/path/to/2.cache",
25
+ "isFetch": false,
26
+ "key": "/path/to/2",
27
+ },
28
+ {
29
+ "buildId": "buildID",
30
+ "fullPath": "/base/path/cache/buildID/path/to/1.cache",
31
+ "isFetch": false,
32
+ "key": "/path/to/1",
33
+ },
34
+ {
35
+ "buildId": "buildID",
36
+ "fullPath": "/base/path/cache/buildID/path/to/0.cache",
37
+ "isFetch": false,
38
+ "key": "/path/to/0",
39
+ },
40
+ {
41
+ "buildId": "buildID",
42
+ "fullPath": "/base/path/cache/__fetch/buildID/2",
43
+ "isFetch": true,
44
+ "key": "/2",
45
+ },
46
+ {
47
+ "buildId": "buildID",
48
+ "fullPath": "/base/path/cache/__fetch/buildID/1",
49
+ "isFetch": true,
50
+ "key": "/1",
51
+ },
52
+ {
53
+ "buildId": "buildID",
54
+ "fullPath": "/base/path/cache/__fetch/buildID/0",
55
+ "isFetch": true,
56
+ "key": "/0",
57
+ },
58
+ ]
59
+ `);
60
+ });
61
+ });
@@ -0,0 +1,5 @@
1
+ import { BuildOptions } from "@opennextjs/aws/build/helper.js";
2
+ import { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
3
+ export declare function upload(options: BuildOptions, config: OpenNextConfig, uploadOptions: {
4
+ passthroughArgs: string[];
5
+ }): Promise<void>;
@@ -0,0 +1,9 @@
1
+ import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
2
+ import { populateCache } from "./populate-cache.js";
3
+ export async function upload(options, config, uploadOptions) {
4
+ await populateCache(options, config, {
5
+ target: "remote",
6
+ environment: getWranglerEnvironmentFlag(uploadOptions.passthroughArgs),
7
+ });
8
+ runWrangler(options, ["versions upload", ...uploadOptions.passthroughArgs], { logging: "all" });
9
+ }
package/dist/cli/index.js CHANGED
@@ -11,6 +11,7 @@ import { createOpenNextConfigIfNotExistent, ensureCloudflareConfig } from "./bui
11
11
  import { deploy } from "./commands/deploy.js";
12
12
  import { populateCache } from "./commands/populate-cache.js";
13
13
  import { preview } from "./commands/preview.js";
14
+ import { upload } from "./commands/upload.js";
14
15
  const nextAppDir = process.cwd();
15
16
  async function runCommand(args) {
16
17
  printHeader(`Cloudflare ${args.command}`);
@@ -33,6 +34,8 @@ async function runCommand(args) {
33
34
  return preview(options, config, args);
34
35
  case "deploy":
35
36
  return deploy(options, config, args);
37
+ case "upload":
38
+ return upload(options, config, args);
36
39
  case "populateCache":
37
40
  return populateCache(options, config, args);
38
41
  }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Initialization for the workerd runtime.
3
+ *
4
+ * The file must be imported at the top level the worker.
5
+ */
6
+ /**
7
+ * Executes the handler with the Cloudflare context.
8
+ */
9
+ export declare function runWithCloudflareRequestContext(request: Request, env: CloudflareEnv, ctx: ExecutionContext, handler: () => Promise<Response>): Promise<Response>;
10
+ declare global {
11
+ var __BUILD_TIMESTAMP_MS__: number;
12
+ var __NEXT_BASE_PATH__: string;
13
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Initialization for the workerd runtime.
3
+ *
4
+ * The file must be imported at the top level the worker.
5
+ */
6
+ import { AsyncLocalStorage } from "node:async_hooks";
7
+ import process from "node:process";
8
+ import stream from "node:stream";
9
+ // @ts-expect-error: resolved by wrangler build
10
+ import * as nextEnvVars from "./next-env.mjs";
11
+ const cloudflareContextALS = new AsyncLocalStorage();
12
+ // Note: this symbol needs to be kept in sync with `src/api/get-cloudflare-context.ts`
13
+ Object.defineProperty(globalThis, Symbol.for("__cloudflare-context__"), {
14
+ get() {
15
+ return cloudflareContextALS.getStore();
16
+ },
17
+ });
18
+ /**
19
+ * Executes the handler with the Cloudflare context.
20
+ */
21
+ export async function runWithCloudflareRequestContext(request, env, ctx, handler) {
22
+ init(request, env);
23
+ return cloudflareContextALS.run({ env, ctx, cf: request.cf }, handler);
24
+ }
25
+ let initialized = false;
26
+ /**
27
+ * Initializes the runtime on the first call,
28
+ * no-op on subsequent invocations.
29
+ */
30
+ function init(request, env) {
31
+ if (initialized) {
32
+ return;
33
+ }
34
+ initialized = true;
35
+ const url = new URL(request.url);
36
+ initRuntime();
37
+ populateProcessEnv(url, env);
38
+ }
39
+ function initRuntime() {
40
+ // Some packages rely on `process.version` and `process.versions.node` (i.e. Jose@4)
41
+ // TODO: Remove when https://github.com/unjs/unenv/pull/493 is merged
42
+ Object.assign(process, { version: process.version || "v22.14.0" });
43
+ // @ts-expect-error Node type does not match workerd
44
+ Object.assign(process.versions, { node: "22.14.0", ...process.versions });
45
+ globalThis.__dirname ??= "";
46
+ globalThis.__filename ??= "";
47
+ // Do not crash on cache not supported
48
+ // https://github.com/cloudflare/workerd/pull/2434
49
+ // compatibility flag "cache_option_enabled" -> does not support "force-cache"
50
+ const __original_fetch = globalThis.fetch;
51
+ globalThis.fetch = (input, init) => {
52
+ if (init) {
53
+ delete init.cache;
54
+ }
55
+ return __original_fetch(input, init);
56
+ };
57
+ const CustomRequest = class extends globalThis.Request {
58
+ constructor(input, init) {
59
+ if (init) {
60
+ delete init.cache;
61
+ // https://github.com/cloudflare/workerd/issues/2746
62
+ // https://github.com/cloudflare/workerd/issues/3245
63
+ Object.defineProperty(init, "body", {
64
+ // @ts-ignore
65
+ value: init.body instanceof stream.Readable ? ReadableStream.from(init.body) : init.body,
66
+ });
67
+ }
68
+ super(input, init);
69
+ }
70
+ };
71
+ Object.assign(globalThis, {
72
+ Request: CustomRequest,
73
+ __BUILD_TIMESTAMP_MS__: __BUILD_TIMESTAMP_MS__,
74
+ __NEXT_BASE_PATH__: __NEXT_BASE_PATH__,
75
+ });
76
+ }
77
+ /**
78
+ * Populate process.env with:
79
+ * - the environment variables and secrets from the cloudflare platform
80
+ * - the variables from Next .env* files
81
+ * - the origin resolver information
82
+ */
83
+ function populateProcessEnv(url, env) {
84
+ for (const [key, value] of Object.entries(env)) {
85
+ if (typeof value === "string") {
86
+ process.env[key] = value;
87
+ }
88
+ }
89
+ const mode = env.NEXTJS_ENV ?? "production";
90
+ if (nextEnvVars[mode]) {
91
+ for (const key in nextEnvVars[mode]) {
92
+ process.env[key] ??= nextEnvVars[mode][key];
93
+ }
94
+ }
95
+ // Set the default Origin for the origin resolver.
96
+ // This is only needed for an external middleware bundle
97
+ process.env.OPEN_NEXT_ORIGIN = JSON.stringify({
98
+ default: {
99
+ host: url.hostname,
100
+ protocol: url.protocol.slice(0, -1),
101
+ port: url.port,
102
+ },
103
+ });
104
+ }
105
+ /* eslint-enable no-var */
@@ -1,4 +1,6 @@
1
1
  export { DOQueueHandler } from "./.build/durable-objects/queue.js";
2
2
  export { DOShardedTagCache } from "./.build/durable-objects/sharded-tag-cache.js";
3
- declare const _default: ExportedHandler<CloudflareEnv>;
3
+ declare const _default: {
4
+ fetch(request: Request<unknown, IncomingRequestCfProperties<unknown>>, env: CloudflareEnv, ctx: ExecutionContext): Promise<any>;
5
+ };
4
6
  export default _default;
@@ -1,25 +1,13 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
- import process from "node:process";
3
- // @ts-expect-error: resolved by wrangler build
4
- import * as nextEnvVars from "./env/next-env.mjs";
5
- const cloudflareContextALS = new AsyncLocalStorage();
6
- // Note: this symbol needs to be kept in sync with `src/api/get-cloudflare-context.ts`
7
- Object.defineProperty(globalThis, Symbol.for("__cloudflare-context__"), {
8
- get() {
9
- return cloudflareContextALS.getStore();
10
- },
11
- });
1
+ //@ts-expect-error: Will be resolved by wrangler build
2
+ import { runWithCloudflareRequestContext } from "./cloudflare/init.js";
12
3
  //@ts-expect-error: Will be resolved by wrangler build
13
4
  export { DOQueueHandler } from "./.build/durable-objects/queue.js";
14
5
  //@ts-expect-error: Will be resolved by wrangler build
15
6
  export { DOShardedTagCache } from "./.build/durable-objects/sharded-tag-cache.js";
16
- // Populate process.env on the first request
17
- let processEnvPopulated = false;
18
7
  export default {
19
8
  async fetch(request, env, ctx) {
20
- return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
9
+ return runWithCloudflareRequestContext(request, env, ctx, async () => {
21
10
  const url = new URL(request.url);
22
- populateProcessEnv(url, env);
23
11
  // Serve images in development.
24
12
  // Note: "/cdn-cgi/image/..." requests do not reach production workers.
25
13
  if (url.pathname.startsWith("/cdn-cgi/image/")) {
@@ -33,10 +21,10 @@ export default {
33
21
  : env.ASSETS?.fetch(new URL(`/${imageUrl}`, url));
34
22
  }
35
23
  // Fallback for the Next default image loader.
36
- if (url.pathname === "/_next/image") {
24
+ if (url.pathname === `${globalThis.__NEXT_BASE_PATH__}/_next/image`) {
37
25
  const imageUrl = url.searchParams.get("url") ?? "";
38
26
  return imageUrl.startsWith("/")
39
- ? env.ASSETS?.fetch(new URL(imageUrl, request.url))
27
+ ? env.ASSETS?.fetch(`http://assets.local${imageUrl}`)
40
28
  : fetch(imageUrl, { cf: { cacheEverything: true } });
41
29
  }
42
30
  // @ts-expect-error: resolved by wrangler build
@@ -45,39 +33,3 @@ export default {
45
33
  });
46
34
  },
47
35
  };
48
- /**
49
- * Populate process.env with:
50
- * - the environment variables and secrets from the cloudflare platform
51
- * - the variables from Next .env* files
52
- * - the origin resolver information
53
- */
54
- function populateProcessEnv(url, env) {
55
- if (processEnvPopulated) {
56
- return;
57
- }
58
- // Some packages rely on `process.version` and `process.versions.node` (i.e. Jose@4)
59
- // TODO: Remove when https://github.com/unjs/unenv/pull/493 is merged
60
- Object.assign(process, { version: process.version || "v22.14.0" });
61
- // @ts-expect-error Node type does not match workerd
62
- Object.assign(process.versions, { node: "22.14.0", ...process.versions });
63
- processEnvPopulated = true;
64
- for (const [key, value] of Object.entries(env)) {
65
- if (typeof value === "string") {
66
- process.env[key] = value;
67
- }
68
- }
69
- const mode = env.NEXTJS_ENV ?? "production";
70
- if (nextEnvVars[mode]) {
71
- for (const key in nextEnvVars[mode]) {
72
- process.env[key] ??= nextEnvVars[mode][key];
73
- }
74
- }
75
- // Set the default Origin for the origin resolver.
76
- process.env.OPEN_NEXT_ORIGIN = JSON.stringify({
77
- default: {
78
- host: url.hostname,
79
- protocol: url.protocol.slice(0, -1),
80
- port: url.port,
81
- },
82
- });
83
- }