@opennextjs/cloudflare 1.8.0 → 1.8.1

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.
@@ -9,4 +9,4 @@ export declare const purgeCache: ({ type }: PurgeOptions) => {
9
9
  resolvedRoutes: import("@opennextjs/aws/types/open-next.js").ResolvedRoute[];
10
10
  }[]): Promise<void>;
11
11
  };
12
- export {};
12
+ export default purgeCache;
@@ -25,3 +25,4 @@ export const purgeCache = ({ type = "direct" }) => {
25
25
  },
26
26
  };
27
27
  };
28
+ export default purgeCache;
@@ -20,13 +20,15 @@ type Options = {
20
20
  * Whether the regional cache entry should be updated in the background or not when it experiences
21
21
  * a cache hit.
22
22
  *
23
- * @default `false` for the `short-lived` mode, and `true` for the `long-lived` mode.
23
+ * @default `true` in `long-lived` mode when cache purge is not used, `false` otherwise.
24
24
  */
25
25
  shouldLazilyUpdateOnCacheHit?: boolean;
26
26
  /**
27
27
  * Whether on cache hits the tagCache should be skipped or not. Skipping the tagCache allows requests to be
28
- * handled faster, the downside of this is that you need to make sure that the cache gets correctly purged
29
- * either by enabling the auto cache purging feature or doing that manually.
28
+ * handled faster,
29
+ *
30
+ * Note: When this is enabled, make sure that the cache gets purged
31
+ * either by enabling the auto cache purging feature or manually.
30
32
  *
31
33
  * @default `true` if the auto cache purging is enabled, `false` otherwise.
32
34
  */
@@ -38,7 +40,16 @@ interface PutToCacheInput {
38
40
  entry: IncrementalCacheEntry<CacheEntryType>;
39
41
  }
40
42
  /**
41
- * Wrapper adding a regional cache on an `IncrementalCache` implementation
43
+ * Wrapper adding a regional cache on an `IncrementalCache` implementation.
44
+ *
45
+ * Using a the `RegionalCache` does not directly improves the performance much.
46
+ * However it allows bypassing the tag cache (see `bypassTagCacheOnCacheHit`) on hits.
47
+ * That's where bigger perf gain happens.
48
+ *
49
+ * We recommend using cache purge.
50
+ * When cache purge is not enabled, there is a possibility that the Cache API (local to a Data Center)
51
+ * is out of sync with the cache store (i.e. R2). That's why when cache purge is not enabled the Cache
52
+ * API is refreshed from the cache store on cache hits (for the long-lived mode).
42
53
  */
43
54
  declare class RegionalCache implements IncrementalCache {
44
55
  #private;
@@ -5,7 +5,16 @@ import { NAME as KV_CACHE_NAME } from "./kv-incremental-cache.js";
5
5
  const ONE_MINUTE_IN_SECONDS = 60;
6
6
  const THIRTY_MINUTES_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 30;
7
7
  /**
8
- * Wrapper adding a regional cache on an `IncrementalCache` implementation
8
+ * Wrapper adding a regional cache on an `IncrementalCache` implementation.
9
+ *
10
+ * Using a the `RegionalCache` does not directly improves the performance much.
11
+ * However it allows bypassing the tag cache (see `bypassTagCacheOnCacheHit`) on hits.
12
+ * That's where bigger perf gain happens.
13
+ *
14
+ * We recommend using cache purge.
15
+ * When cache purge is not enabled, there is a possibility that the Cache API (local to a Data Center)
16
+ * is out of sync with the cache store (i.e. R2). That's why when cache purge is not enabled the Cache
17
+ * API is refreshed from the cache store on cache hits (for the long-lived mode).
9
18
  */
10
19
  class RegionalCache {
11
20
  store;
@@ -19,7 +28,8 @@ class RegionalCache {
19
28
  throw new Error("The KV incremental cache does not need a regional cache.");
20
29
  }
21
30
  this.name = this.store.name;
22
- this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived";
31
+ this.opts.shouldLazilyUpdateOnCacheHit ??=
32
+ this.opts.mode === "long-lived" && !this.#hasAutomaticCachePurging;
23
33
  }
24
34
  get #bypassTagCacheOnCacheHit() {
25
35
  if (this.opts.bypassTagCacheOnCacheHit !== undefined) {
@@ -27,8 +37,11 @@ class RegionalCache {
27
37
  return this.opts.bypassTagCacheOnCacheHit;
28
38
  }
29
39
  // Otherwise we default to whether the automatic cache purging is enabled or not
30
- const hasAutomaticCachePurging = !!getCloudflareContext().env.NEXT_CACHE_DO_PURGE;
31
- return hasAutomaticCachePurging;
40
+ return this.#hasAutomaticCachePurging;
41
+ }
42
+ get #hasAutomaticCachePurging() {
43
+ const cdnInvalidation = globalThis.openNextConfig.default?.override?.cdnInvalidation;
44
+ return cdnInvalidation !== undefined && cdnInvalidation !== "dummy";
32
45
  }
33
46
  async get(key, cacheType) {
34
47
  try {
@@ -38,7 +51,8 @@ class RegionalCache {
38
51
  const cachedResponse = await cache.match(urlKey);
39
52
  if (cachedResponse) {
40
53
  debugCache("Get - cached response");
41
- // Re-fetch from the store and update the regional cache in the background
54
+ // Re-fetch from the store and update the regional cache in the background.
55
+ // Note: this is only useful when the Cache API is not purged automatically.
42
56
  if (this.opts.shouldLazilyUpdateOnCacheHit) {
43
57
  getCloudflareContext().ctx.waitUntil(this.store.get(key, cacheType).then(async (rawEntry) => {
44
58
  const { value, lastModified } = rawEntry ?? {};
@@ -12,6 +12,7 @@ export async function compileInit(options, wranglerConfig) {
12
12
  const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
13
13
  const basePath = nextConfig.basePath ?? "";
14
14
  const deploymentId = nextConfig.deploymentId ?? "";
15
+ const trailingSlash = nextConfig.trailingSlash ?? false;
15
16
  await build({
16
17
  entryPoints: [initPath],
17
18
  outdir: path.join(options.outputDir, "cloudflare"),
@@ -25,6 +26,7 @@ export async function compileInit(options, wranglerConfig) {
25
26
  __NEXT_BASE_PATH__: JSON.stringify(basePath),
26
27
  __ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
27
28
  __DEPLOYMENT_ID__: JSON.stringify(deploymentId),
29
+ __TRAILING_SLASH__: JSON.stringify(trailingSlash),
28
30
  },
29
31
  });
30
32
  }
@@ -12,4 +12,5 @@ declare global {
12
12
  var __NEXT_BASE_PATH__: string;
13
13
  var __ASSETS_RUN_WORKER_FIRST__: boolean | string[] | undefined;
14
14
  var __DEPLOYMENT_ID__: string;
15
+ var __TRAILING_SLASH__: boolean;
15
16
  }
@@ -76,6 +76,7 @@ function initRuntime() {
76
76
  __BUILD_TIMESTAMP_MS__,
77
77
  __NEXT_BASE_PATH__,
78
78
  __ASSETS_RUN_WORKER_FIRST__,
79
+ __TRAILING_SLASH__,
79
80
  // The external middleware will use the convertTo function of the `edge` converter
80
81
  // by default it will try to fetch the request, but since we are running everything in the same worker
81
82
  // we need to use the request as is.
@@ -33,7 +33,8 @@ export default {
33
33
  : env.ASSETS?.fetch(new URL(`/${imageUrl}`, url));
34
34
  }
35
35
  // Fallback for the Next default image loader.
36
- if (url.pathname === `${globalThis.__NEXT_BASE_PATH__}/_next/image`) {
36
+ if (url.pathname ===
37
+ `${globalThis.__NEXT_BASE_PATH__}/_next/image${globalThis.__TRAILING_SLASH__ ? "/" : ""}`) {
37
38
  const imageUrl = url.searchParams.get("url") ?? "";
38
39
  return await fetchImage(env.ASSETS, imageUrl, ctx);
39
40
  }
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.8.0",
4
+ "version": "1.8.1",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "opennextjs-cloudflare": "dist/cli/index.js"