@opennextjs/cloudflare 1.0.0 → 1.0.2

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.
@@ -26,6 +26,17 @@ export function defineCloudflareConfig(config = {}) {
26
26
  dangerous: {
27
27
  enableCacheInterception,
28
28
  },
29
+ middleware: {
30
+ external: true,
31
+ override: {
32
+ wrapper: "cloudflare-edge",
33
+ converter: "edge",
34
+ proxyExternalRequest: "fetch",
35
+ incrementalCache: resolveIncrementalCache(incrementalCache),
36
+ tagCache: resolveTagCache(tagCache),
37
+ queue: resolveQueue(queue),
38
+ },
39
+ },
29
40
  };
30
41
  }
31
42
  function resolveIncrementalCache(value = "dummy") {
@@ -6,6 +6,7 @@ import logger from "@opennextjs/aws/logger.js";
6
6
  */
7
7
  export function ensureCloudflareConfig(config) {
8
8
  const requirements = {
9
+ // Check for the default function
9
10
  dftUseCloudflareWrapper: config.default?.override?.wrapper === "cloudflare-node",
10
11
  dftUseEdgeConverter: config.default?.override?.converter === "edge",
11
12
  dftUseFetchProxy: config.default?.override?.proxyExternalRequest === "fetch",
@@ -16,7 +17,11 @@ export function ensureCloudflareConfig(config) {
16
17
  dftMaybeUseQueue: config.default?.override?.queue === "dummy" ||
17
18
  config.default?.override?.queue === "direct" ||
18
19
  typeof config.default?.override?.queue === "function",
19
- mwIsMiddlewareIntegrated: config.middleware === undefined,
20
+ // Check for the middleware function
21
+ mwIsMiddlewareExternal: config.middleware?.external === true,
22
+ mwUseCloudflareWrapper: config.middleware?.override?.wrapper === "cloudflare-edge",
23
+ mwUseEdgeConverter: config.middleware?.override?.converter === "edge",
24
+ mwUseFetchProxy: config.middleware?.override?.proxyExternalRequest === "fetch",
20
25
  hasCryptoExternal: config.edgeExternals?.includes("node:crypto"),
21
26
  };
22
27
  if (config.default?.override?.queue === "direct") {
@@ -31,11 +36,22 @@ export function ensureCloudflareConfig(config) {
31
36
  converter: "edge",
32
37
  proxyExternalRequest: "fetch",
33
38
  incrementalCache: "dummy" | function,
34
- tagCache: "dummy",
39
+ tagCache: "dummy" | function,
35
40
  queue: "dummy" | "direct" | function,
36
41
  },
37
42
  },
38
43
  edgeExternals: ["node:crypto"],
44
+ middleware: {
45
+ external: true,
46
+ override: {
47
+ wrapper: "cloudflare-edge",
48
+ converter: "edge",
49
+ proxyExternalRequest: "fetch",
50
+ incrementalCache: "dummy" | function,
51
+ tagCache: "dummy" | function,
52
+ queue: "dummy" | "direct" | function,
53
+ },
54
+ },
39
55
  }\n\n`.replace(/^ {8}/gm, ""));
40
56
  }
41
57
  }
@@ -49,10 +49,15 @@ export function getCacheAssets(opts) {
49
49
  }
50
50
  return assets;
51
51
  }
52
+ async function getPlatformProxyEnv(options, key) {
53
+ const proxy = await getPlatformProxy(options);
54
+ const prefix = proxy.env[key];
55
+ await proxy.dispose();
56
+ return prefix;
57
+ }
52
58
  async function populateR2IncrementalCache(options, populateCacheOptions) {
53
59
  logger.info("\nPopulating R2 incremental cache...");
54
60
  const config = unstable_readConfig({ env: populateCacheOptions.environment });
55
- const proxy = await getPlatformProxy(populateCacheOptions);
56
61
  const binding = config.r2_buckets.find(({ binding }) => binding === R2_CACHE_BINDING_NAME);
57
62
  if (!binding) {
58
63
  throw new Error(`No R2 binding ${JSON.stringify(R2_CACHE_BINDING_NAME)} found!`);
@@ -61,10 +66,11 @@ async function populateR2IncrementalCache(options, populateCacheOptions) {
61
66
  if (!bucket) {
62
67
  throw new Error(`R2 binding ${JSON.stringify(R2_CACHE_BINDING_NAME)} should have a 'bucket_name'`);
63
68
  }
69
+ const prefix = await getPlatformProxyEnv(populateCacheOptions, R2_CACHE_PREFIX_ENV_NAME);
64
70
  const assets = getCacheAssets(options);
65
71
  for (const { fullPath, key, buildId, isFetch } of tqdm(assets)) {
66
72
  const cacheKey = computeCacheKey(key, {
67
- prefix: proxy.env[R2_CACHE_PREFIX_ENV_NAME],
73
+ prefix,
68
74
  buildId,
69
75
  cacheType: isFetch ? "fetch" : "cache",
70
76
  });
@@ -78,11 +84,11 @@ async function populateR2IncrementalCache(options, populateCacheOptions) {
78
84
  async function populateKVIncrementalCache(options, populateCacheOptions) {
79
85
  logger.info("\nPopulating KV incremental cache...");
80
86
  const config = unstable_readConfig({ env: populateCacheOptions.environment });
81
- const proxy = await getPlatformProxy(populateCacheOptions);
82
87
  const binding = config.kv_namespaces.find(({ binding }) => binding === KV_CACHE_BINDING_NAME);
83
88
  if (!binding) {
84
89
  throw new Error(`No KV binding ${JSON.stringify(KV_CACHE_BINDING_NAME)} found!`);
85
90
  }
91
+ const prefix = await getPlatformProxyEnv(populateCacheOptions, KV_CACHE_PREFIX_ENV_NAME);
86
92
  const assets = getCacheAssets(options);
87
93
  const chunkSize = Math.max(1, populateCacheOptions.cacheChunkSize ?? 25);
88
94
  const totalChunks = Math.ceil(assets.length / chunkSize);
@@ -93,7 +99,7 @@ async function populateKVIncrementalCache(options, populateCacheOptions) {
93
99
  .slice(i * chunkSize, (i + 1) * chunkSize)
94
100
  .map(({ fullPath, key, buildId, isFetch }) => ({
95
101
  key: computeCacheKey(key, {
96
- prefix: proxy.env[KV_CACHE_PREFIX_ENV_NAME],
102
+ prefix,
97
103
  buildId,
98
104
  cacheType: isFetch ? "fetch" : "cache",
99
105
  }),
@@ -75,6 +75,10 @@ function initRuntime() {
75
75
  Request: CustomRequest,
76
76
  __BUILD_TIMESTAMP_MS__: __BUILD_TIMESTAMP_MS__,
77
77
  __NEXT_BASE_PATH__: __NEXT_BASE_PATH__,
78
+ // The external middleware will use the convertTo function of the `edge` converter
79
+ // by default it will try to fetch the request, but since we are running everything in the same worker
80
+ // we need to use the request as is.
81
+ __dangerous_ON_edge_converter_returns_request: true,
78
82
  });
79
83
  }
80
84
  /**
@@ -1,5 +1,7 @@
1
1
  //@ts-expect-error: Will be resolved by wrangler build
2
2
  import { runWithCloudflareRequestContext } from "./cloudflare/init.js";
3
+ // @ts-expect-error: Will be resolved by wrangler build
4
+ import { handler as middlewareHandler } from "./middleware/handler.mjs";
3
5
  //@ts-expect-error: Will be resolved by wrangler build
4
6
  export { DOQueueHandler } from "./.build/durable-objects/queue.js";
5
7
  //@ts-expect-error: Will be resolved by wrangler build
@@ -27,9 +29,14 @@ export default {
27
29
  ? env.ASSETS?.fetch(`http://assets.local${imageUrl}`)
28
30
  : fetch(imageUrl, { cf: { cacheEverything: true } });
29
31
  }
32
+ // - `Request`s are handled by the Next server
33
+ const reqOrResp = await middlewareHandler(request, env, ctx);
34
+ if (reqOrResp instanceof Response) {
35
+ return reqOrResp;
36
+ }
30
37
  // @ts-expect-error: resolved by wrangler build
31
38
  const { handler } = await import("./server-functions/default/handler.mjs");
32
- return handler(request, env, ctx);
39
+ return handler(reqOrResp, env, ctx);
33
40
  });
34
41
  },
35
42
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@opennextjs/cloudflare",
3
3
  "description": "Cloudflare builder for next apps",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "opennextjs-cloudflare": "dist/cli/index.js"
@@ -43,7 +43,7 @@
43
43
  "homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
44
44
  "dependencies": {
45
45
  "@dotenvx/dotenvx": "1.31.0",
46
- "@opennextjs/aws": "~3.6.0",
46
+ "@opennextjs/aws": "^3.6.1",
47
47
  "enquirer": "^2.4.1",
48
48
  "glob": "^11.0.0",
49
49
  "ts-tqdm": "^0.8.6"
@@ -13,9 +13,10 @@
13
13
  // See https://opennext.js.org/cloudflare/caching
14
14
  {
15
15
  "binding": "NEXT_INC_CACHE_R2_BUCKET",
16
- // Create a bucket before deploying
16
+ // Create the bucket before deploying
17
+ // You can change the bucket name if you want
17
18
  // See https://developers.cloudflare.com/workers/wrangler/commands/#r2-bucket-create
18
- "bucket_name": "<BUCKET_NAME>"
19
+ "bucket_name": "cache"
19
20
  }
20
21
  ]
21
22
  }