@opennextjs/cloudflare 1.0.0-beta.1 → 1.0.0-beta.3

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 (43) hide show
  1. package/dist/api/config.js +2 -0
  2. package/dist/api/overrides/incremental-cache/kv-incremental-cache.d.ts +8 -7
  3. package/dist/api/overrides/incremental-cache/kv-incremental-cache.js +50 -90
  4. package/dist/api/overrides/incremental-cache/r2-incremental-cache.d.ts +9 -0
  5. package/dist/api/overrides/incremental-cache/r2-incremental-cache.js +22 -9
  6. package/dist/api/overrides/incremental-cache/regional-cache.d.ts +18 -3
  7. package/dist/api/overrides/incremental-cache/regional-cache.js +55 -18
  8. package/dist/api/overrides/incremental-cache/static-assets-incremental-cache.d.ts +17 -0
  9. package/dist/api/overrides/incremental-cache/static-assets-incremental-cache.js +46 -0
  10. package/dist/api/overrides/{incremental-cache/internal.d.ts → internal.d.ts} +2 -0
  11. package/dist/api/overrides/internal.js +6 -0
  12. package/dist/api/overrides/queue/memory-queue.js +3 -2
  13. package/dist/api/overrides/tag-cache/d1-next-tag-cache.d.ts +1 -1
  14. package/dist/api/overrides/tag-cache/d1-next-tag-cache.js +9 -13
  15. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.js +4 -3
  16. package/dist/cli/args.d.ts +1 -1
  17. package/dist/cli/args.js +2 -1
  18. package/dist/cli/build/build.js +4 -3
  19. package/dist/cli/build/bundle-server.js +1 -42
  20. package/dist/cli/build/open-next/compile-env-files.js +1 -1
  21. package/dist/cli/build/open-next/compile-init.d.ts +5 -0
  22. package/dist/cli/build/open-next/compile-init.js +27 -0
  23. package/dist/cli/build/open-next/createServerBundle.js +7 -2
  24. package/dist/cli/build/utils/ensure-cf-config.js +2 -0
  25. package/dist/cli/commands/populate-cache.d.ts +7 -0
  26. package/dist/cli/commands/populate-cache.js +114 -46
  27. package/dist/cli/commands/populate-cache.spec.js +61 -0
  28. package/dist/cli/commands/upload.d.ts +5 -0
  29. package/dist/cli/commands/upload.js +9 -0
  30. package/dist/cli/index.js +3 -0
  31. package/dist/cli/templates/init.d.ts +13 -0
  32. package/dist/cli/templates/init.js +105 -0
  33. package/dist/cli/templates/worker.js +5 -53
  34. package/package.json +2 -2
  35. package/templates/open-next.config.ts +2 -2
  36. package/templates/wrangler.jsonc +9 -7
  37. package/dist/cli/build/open-next/copyCacheAssets.d.ts +0 -2
  38. package/dist/cli/build/open-next/copyCacheAssets.js +0 -10
  39. package/dist/cli/build/patches/plugins/next-minimal.d.ts +0 -4
  40. package/dist/cli/build/patches/plugins/next-minimal.js +0 -86
  41. package/dist/cli/build/patches/plugins/next-minimal.spec.d.ts +0 -1
  42. package/dist/cli/build/patches/plugins/next-minimal.spec.js +0 -71
  43. /package/dist/{api/overrides/incremental-cache/internal.js → cli/commands/populate-cache.spec.d.ts} +0 -0
@@ -1,7 +1,8 @@
1
- import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1
+ import { error } from "@opennextjs/aws/adapters/logger.js";
2
2
  import { generateShardId } from "@opennextjs/aws/core/routing/queue.js";
3
3
  import { IgnorableError } from "@opennextjs/aws/utils/error.js";
4
4
  import { getCloudflareContext } from "../../cloudflare-context";
5
+ import { debugCache } from "../internal";
5
6
  export const DEFAULT_WRITE_RETRIES = 3;
6
7
  export const DEFAULT_NUM_SHARDS = 4;
7
8
  export const NAME = "do-sharded-tag-cache";
@@ -102,7 +103,7 @@ class ShardedDOTagCache {
102
103
  const cfEnv = getCloudflareContext().env;
103
104
  const db = cfEnv.NEXT_TAG_CACHE_DO_SHARDED;
104
105
  if (!db)
105
- debug("No Durable object found");
106
+ debugCache("No Durable object found");
106
107
  const isDisabled = !!globalThis.openNextConfig
107
108
  .dangerous?.disableTagCache;
108
109
  return !db || isDisabled
@@ -234,7 +235,7 @@ class ShardedDOTagCache {
234
235
  await cache.delete(key);
235
236
  }
236
237
  catch (e) {
237
- debug("Error while deleting from regional cache", e);
238
+ debugCache("Error while deleting from regional cache", e);
238
239
  }
239
240
  }
240
241
  }
@@ -5,7 +5,7 @@ export type Arguments = ({
5
5
  skipWranglerConfigCheck: boolean;
6
6
  minify: boolean;
7
7
  } | {
8
- command: "preview" | "deploy";
8
+ command: "preview" | "deploy" | "upload";
9
9
  passthroughArgs: string[];
10
10
  } | {
11
11
  command: "populateCache";
package/dist/cli/args.js CHANGED
@@ -28,6 +28,7 @@ export function getArgs() {
28
28
  };
29
29
  case "preview":
30
30
  case "deploy":
31
+ case "upload":
31
32
  return {
32
33
  command: positionals[0],
33
34
  outputDir,
@@ -44,7 +45,7 @@ export function getArgs() {
44
45
  environment: getWranglerEnvironmentFlag(passthroughArgs),
45
46
  };
46
47
  default:
47
- throw new Error("Error: invalid command, expected 'build' | 'preview' | 'deploy' | 'populateCache'");
48
+ throw new Error("Error: invalid command, expected 'build' | 'preview' | 'deploy' | 'upload' | 'populateCache'");
48
49
  }
49
50
  }
50
51
  function getPassthroughArgs() {
@@ -8,8 +8,8 @@ import logger from "@opennextjs/aws/logger.js";
8
8
  import { bundleServer } from "./bundle-server.js";
9
9
  import { compileCacheAssetsManifestSqlFile } from "./open-next/compile-cache-assets-manifest.js";
10
10
  import { compileEnvFiles } from "./open-next/compile-env-files.js";
11
+ import { compileInit } from "./open-next/compile-init.js";
11
12
  import { compileDurableObjects } from "./open-next/compileDurableObjects.js";
12
- import { copyCacheAssets } from "./open-next/copyCacheAssets.js";
13
13
  import { createServerBundle } from "./open-next/createServerBundle.js";
14
14
  import { createWranglerConfigIfNotExistent } from "./utils/index.js";
15
15
  import { getVersion } from "./utils/version.js";
@@ -50,12 +50,13 @@ export async function build(options, config, projectOpts) {
50
50
  compileCache(options);
51
51
  // Compile .env files
52
52
  compileEnvFiles(options);
53
+ // Compile workerd init
54
+ compileInit(options);
53
55
  // Compile middleware
54
56
  await createMiddleware(options, { forceOnlyBuildOnce: true });
55
- createStaticAssets(options);
57
+ createStaticAssets(options, { useBasePath: true });
56
58
  if (config.dangerous?.disableIncrementalCache !== true) {
57
59
  const { useTagCache, metaFiles } = createCacheAssets(options);
58
- copyCacheAssets(options);
59
60
  if (useTagCache) {
60
61
  compileCacheAssetsManifestSqlFile(options, metaFiles);
61
62
  }
@@ -14,7 +14,6 @@ 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";
19
18
  import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
20
19
  import { fixRequire } from "./patches/plugins/require.js";
@@ -83,7 +82,6 @@ export async function bundleServer(buildOpts) {
83
82
  inlineLoadManifest(updater, buildOpts),
84
83
  inlineBuildId(updater),
85
84
  patchDepdDeprecations(updater),
86
- patchNextMinimal(updater),
87
85
  // Apply updater updates, must be the last plugin
88
86
  updater.plugin,
89
87
  ],
@@ -125,46 +123,6 @@ export async function bundleServer(buildOpts) {
125
123
  "process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
126
124
  },
127
125
  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
126
  });
169
127
  fs.writeFileSync(openNextServerBundle + ".meta.json", JSON.stringify(result.metafile, null, 2));
170
128
  await updateWorkerBundledCode(openNextServerBundle, buildOpts);
@@ -185,6 +143,7 @@ export async function updateWorkerBundledCode(workerOutputFile, buildOpts) {
185
143
  [
186
144
  "'require(this.middlewareManifestPath)'",
187
145
  (code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
146
+ { isOptional: true },
188
147
  ],
189
148
  [
190
149
  "`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({
@@ -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;
@@ -1,30 +1,118 @@
1
- import { existsSync } from "node:fs";
1
+ import { cpSync, existsSync } from "node:fs";
2
2
  import path from "node:path";
3
3
  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 { NAME as R2_CACHE_NAME } from "../../api/overrides/incremental-cache/r2-incremental-cache.js";
8
- import { NAME as D1_TAG_NAME } from "../../api/overrides/tag-cache/d1-next-tag-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
+ 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
+ import { BINDING_NAME as D1_TAG_BINDING_NAME, NAME as D1_TAG_NAME, } from "../../api/overrides/tag-cache/d1-next-tag-cache.js";
9
11
  import { runWrangler } from "../utils/run-wrangler.js";
10
12
  async function resolveCacheName(value) {
11
13
  return typeof value === "function" ? (await value()).name : value;
12
14
  }
13
- function getCacheAssetPaths(opts) {
14
- return globSync(path.join(opts.outputDir, "cache/**/*"), {
15
+ export function getCacheAssets(opts) {
16
+ const allFiles = globSync(path.join(opts.outputDir, "cache/**/*"), {
15
17
  withFileTypes: true,
16
18
  windowsPathsNoEscape: true,
17
- })
18
- .filter((f) => f.isFile())
19
- .map((f) => {
20
- const relativePath = path.relative(path.join(opts.outputDir, "cache"), f.fullpathPosix());
21
- return {
22
- fsPath: f.fullpathPosix(),
23
- destPath: relativePath.startsWith("__fetch")
24
- ? `${relativePath.replace("__fetch/", "")}.fetch`
25
- : relativePath,
26
- };
27
- });
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;
50
+ }
51
+ function populateR2IncrementalCache(options, populateCacheOptions) {
52
+ logger.info("\nPopulating R2 incremental cache...");
53
+ const config = unstable_readConfig({ env: populateCacheOptions.environment });
54
+ const binding = config.r2_buckets.find(({ binding }) => binding === R2_CACHE_BINDING_NAME);
55
+ if (!binding) {
56
+ throw new Error(`No R2 binding ${JSON.stringify(R2_CACHE_BINDING_NAME)} found!`);
57
+ }
58
+ const bucket = binding.bucket_name;
59
+ if (!bucket) {
60
+ throw new Error(`R2 binding ${JSON.stringify(R2_CACHE_BINDING_NAME)} should have a 'bucket_name'`);
61
+ }
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)}`],
70
+ // NOTE: R2 does not support the environment flag and results in the following error:
71
+ // Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
72
+ { target: populateCacheOptions.target, excludeRemoteFlag: true, logging: "error" });
73
+ }
74
+ logger.info(`Successfully populated cache with ${assets.length} assets`);
75
+ }
76
+ function populateKVIncrementalCache(options, populateCacheOptions) {
77
+ logger.info("\nPopulating KV incremental cache...");
78
+ const config = unstable_readConfig({ env: populateCacheOptions.environment });
79
+ const binding = config.kv_namespaces.find(({ binding }) => binding === KV_CACHE_BINDING_NAME);
80
+ if (!binding) {
81
+ throw new Error(`No KV binding ${JSON.stringify(KV_CACHE_BINDING_NAME)} found!`);
82
+ }
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
+ });
89
+ runWrangler(options, [
90
+ "kv key put",
91
+ JSON.stringify(cacheKey),
92
+ `--binding ${JSON.stringify(KV_CACHE_BINDING_NAME)}`,
93
+ `--path ${JSON.stringify(fullPath)}`,
94
+ ], { ...populateCacheOptions, logging: "error" });
95
+ }
96
+ logger.info(`Successfully populated cache with ${assets.length} assets`);
97
+ }
98
+ function populateD1TagCache(options, populateCacheOptions) {
99
+ logger.info("\nCreating D1 table if necessary...");
100
+ const config = unstable_readConfig({ env: populateCacheOptions.environment });
101
+ const binding = config.d1_databases.find(({ binding }) => binding === D1_TAG_BINDING_NAME);
102
+ if (!binding) {
103
+ throw new Error(`No D1 binding ${JSON.stringify(D1_TAG_BINDING_NAME)} found!`);
104
+ }
105
+ runWrangler(options, [
106
+ "d1 execute",
107
+ JSON.stringify(D1_TAG_BINDING_NAME),
108
+ `--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`,
109
+ ], { ...populateCacheOptions, logging: "error" });
110
+ logger.info("\nSuccessfully created D1 table");
111
+ }
112
+ function populateStaticAssetsIncrementalCache(options) {
113
+ logger.info("\nPopulating Workers static assets...");
114
+ cpSync(path.join(options.outputDir, "cache"), path.join(options.outputDir, "assets", STATIC_ASSETS_CACHE_DIR), { recursive: true });
115
+ logger.info(`Successfully populated static assets cache`);
28
116
  }
29
117
  export async function populateCache(options, config, populateCacheOptions) {
30
118
  const { incrementalCache, tagCache } = config.default.override ?? {};
@@ -35,28 +123,15 @@ export async function populateCache(options, config, populateCacheOptions) {
35
123
  if (!config.dangerous?.disableIncrementalCache && incrementalCache) {
36
124
  const name = await resolveCacheName(incrementalCache);
37
125
  switch (name) {
38
- case R2_CACHE_NAME: {
39
- const config = unstable_readConfig({ env: populateCacheOptions.environment });
40
- const binding = (config.r2_buckets ?? []).find(({ binding }) => binding === "NEXT_INC_CACHE_R2_BUCKET");
41
- if (!binding) {
42
- throw new Error("No R2 binding 'NEXT_INC_CACHE_R2_BUCKET' found!");
43
- }
44
- const bucket = binding.bucket_name;
45
- if (!bucket) {
46
- throw new Error("R2 binding 'NEXT_INC_CACHE_R2_BUCKET' should have a 'bucket_name'");
47
- }
48
- logger.info("\nPopulating R2 incremental cache...");
49
- const assets = getCacheAssetPaths(options);
50
- for (const { fsPath, destPath } of tqdm(assets)) {
51
- const fullDestPath = path.join(bucket, process.env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache", destPath);
52
- runWrangler(options, ["r2 object put", JSON.stringify(fullDestPath), `--file ${JSON.stringify(fsPath)}`],
53
- // NOTE: R2 does not support the environment flag and results in the following error:
54
- // Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
55
- { target: populateCacheOptions.target, excludeRemoteFlag: true, logging: "error" });
56
- }
57
- logger.info(`Successfully populated cache with ${assets.length} assets`);
126
+ case R2_CACHE_NAME:
127
+ populateR2IncrementalCache(options, populateCacheOptions);
128
+ break;
129
+ case KV_CACHE_NAME:
130
+ populateKVIncrementalCache(options, populateCacheOptions);
131
+ break;
132
+ case STATIC_ASSETS_CACHE_NAME:
133
+ populateStaticAssetsIncrementalCache(options);
58
134
  break;
59
- }
60
135
  default:
61
136
  logger.info("Incremental cache does not need populating");
62
137
  }
@@ -64,16 +139,9 @@ export async function populateCache(options, config, populateCacheOptions) {
64
139
  if (!config.dangerous?.disableTagCache && !config.dangerous?.disableIncrementalCache && tagCache) {
65
140
  const name = await resolveCacheName(tagCache);
66
141
  switch (name) {
67
- case D1_TAG_NAME: {
68
- logger.info("\nCreating D1 table if necessary...");
69
- runWrangler(options, [
70
- "d1 execute",
71
- "NEXT_TAG_CACHE_D1",
72
- `--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`,
73
- ], { ...populateCacheOptions, logging: "error" });
74
- logger.info("\nSuccessfully created D1 table");
142
+ case D1_TAG_NAME:
143
+ populateD1TagCache(options, populateCacheOptions);
75
144
  break;
76
- }
77
145
  default:
78
146
  logger.info("Tag cache does not need populating");
79
147
  }
@@ -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
+ }