Skip to content
4 changes: 2 additions & 2 deletions examples/e2e/app-pages-router/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import memoryQueue from "@opennextjs/cloudflare/memory-queue";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";
import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/e2e/app-pages-router/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
10 changes: 5 additions & 5 deletions examples/e2e/app-router/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import shardedTagCache from "@opennextjs/cloudflare/do-sharded-tag-cache";
import doQueue from "@opennextjs/cloudflare/durable-queue";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";
import shardedTagCache from "@opennextjs/cloudflare/overrides/tag-cache/do-sharded-tag-cache";
import doQueue from "@opennextjs/cloudflare/overrides/queue/do-queue";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
// With such a configuration, we could have up to 12 * (8 + 2) = 120 Durable Objects instances
// With such a configuration, we could have up to 2 * 6 * (8 + 2) = 120 Durable Objects instances
tagCache: shardedTagCache({
numberOfShards: 12,
baseShardSize: 6,
enableShardReplication: true,
shardReplicationOptions: {
numberOfSoftReplicas: 8,
Expand Down
4 changes: 2 additions & 2 deletions examples/e2e/app-router/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"durable_objects": {
"bindings": [
{
"name": "NEXT_CACHE_REVALIDATION_DURABLE_OBJECT",
"name": "NEXT_CACHE_DO_REVALIDATION",
"class_name": "DurableObjectQueueHandler"
},
{
Expand All @@ -28,7 +28,7 @@
],
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
4 changes: 2 additions & 2 deletions examples/e2e/pages-router/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import memoryQueue from "@opennextjs/cloudflare/memory-queue";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";
import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/e2e/pages-router/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
4 changes: 2 additions & 2 deletions examples/overrides/d1-tag-next/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import d1NextTagCache from "@opennextjs/cloudflare/d1-next-tag-cache";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";
import d1NextTagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-next-tag-cache";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/overrides/d1-tag-next/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
4 changes: 2 additions & 2 deletions examples/overrides/memory-queue/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import memoryQueue from "@opennextjs/cloudflare/memory-queue";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";
import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/overrides/memory-queue/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
8 changes: 4 additions & 4 deletions examples/overrides/r2-incremental-cache/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import d1TagCache from "@opennextjs/cloudflare/d1-tag-cache";
import memoryQueue from "@opennextjs/cloudflare/memory-queue";
import r2IncrementalCache from "@opennextjs/cloudflare/r2-incremental-cache";
import { withRegionalCache } from "@opennextjs/cloudflare/regional-cache";
import d1TagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-tag-cache";
import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
import { withRegionalCache } from "@opennextjs/cloudflare/overrides/incremental-cache/regional-cache";

export default defineCloudflareConfig({
incrementalCache: withRegionalCache(r2IncrementalCache, {
Expand Down
2 changes: 1 addition & 1 deletion examples/playground14/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/playground14/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
2 changes: 1 addition & 1 deletion examples/playground15/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/playground15/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"kv_namespaces": [
{
"binding": "NEXT_CACHE_WORKERS_KV",
"binding": "NEXT_CACHE_KV",
"id": "<BINDING_ID>"
}
],
Expand Down
2 changes: 1 addition & 1 deletion examples/ssg-app/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion examples/vercel-blog-starter/open-next.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-cache";

export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@

- 87f4fb5: feat: configure kv binding name with env var

The Workers KV binding used in the Next.js cache handler can be given a custom name with the `__OPENNEXT_KV_BINDING_NAME` environment variable at build-time, instead of defaulting to `NEXT_CACHE_WORKERS_KV`.
The Workers KV binding used in the Next.js cache handler can be given a custom name with the `__OPENNEXT_KV_BINDING_NAME` environment variable at build-time, instead of defaulting to `NEXT_CACHE_KV`.

### Patch Changes

Expand Down
25 changes: 15 additions & 10 deletions packages/cloudflare/src/api/cloudflare-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@ declare global {
// Default to "production"
NEXTJS_ENV?: string;

// Service binding for the worker itself to be able to call itself from within the worker
NEXT_CACHE_REVALIDATION_WORKER?: Service;

// KV used for the incremental cache
NEXT_CACHE_WORKERS_KV?: KVNamespace;
NEXT_CACHE_KV?: KVNamespace;

// R2 bucket used for the incremental cache
NEXT_CACHE_R2_BUCKET?: R2Bucket;
// Prefix used for the R2 incremental cache bucket
NEXT_CACHE_R2_PREFIX?: string;

// D1 db used for the tag cache
NEXT_CACHE_D1?: D1Database;
// D1 table to use for the tag cache for the tag/path mapping
// Optional, default to "tags"
NEXT_CACHE_D1_TAGS_TABLE?: string;
// D1 table to use for the tag cache for storing the tag and their associated revalidation times
// Optional, default to "revalidations"
NEXT_CACHE_D1_REVALIDATIONS_TABLE?: string;
// Service binding for the worker itself to be able to call itself from within the worker
NEXT_CACHE_REVALIDATION_WORKER?: Service;
// R2 bucket used for the incremental cache
NEXT_CACHE_R2_BUCKET?: R2Bucket;
// Prefix used for the R2 incremental cache bucket
NEXT_CACHE_R2_PREFIX?: string;

// Durable Object namespace to use for the durable object queue handler
NEXT_CACHE_REVALIDATION_DURABLE_OBJECT?: DurableObjectNamespace<DurableObjectQueueHandler>;
NEXT_CACHE_DO_REVALIDATION?: DurableObjectNamespace<DurableObjectQueueHandler>;
// Durables object namespace to use for the sharded tag cache
NEXT_CACHE_DO_SHARDED?: DurableObjectNamespace<DOShardedTagCache>;
// Queue of failed tag write
// It could be used for monitoring or to reprocess failed writes
// Entirely optional
// Optional, could be used to monitor or reprocess failed writes
NEXT_CACHE_DO_SHARDED_DLQ?: Queue;

// Below are the potential environment variables that can be set by the user to configure the durable object queue handler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides";
import { IgnorableError, RecoverableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context.js";
import { getCloudflareContext } from "../../cloudflare-context.js";

export const CACHE_ASSET_DIR = "cdn-cgi/_next_cache";

Expand All @@ -11,7 +11,7 @@ export const STATUS_DELETED = 1;
* Open Next cache based on cloudflare KV and Assets.
*
* Note: The class is instantiated outside of the request context.
* The cloudflare context and process.env are not initialzed yet
* The cloudflare context and process.env are not initialized yet
* when the constructor is called.
*/
class Cache implements IncrementalCache {
Expand All @@ -22,7 +22,7 @@ class Cache implements IncrementalCache {
isFetch?: IsFetch
): Promise<WithLastModified<CacheValue<IsFetch>> | null> {
const cfEnv = getCloudflareContext().env;
const kv = cfEnv.NEXT_CACHE_WORKERS_KV;
const kv = cfEnv.NEXT_CACHE_KV;
const assets = cfEnv.ASSETS;

if (!(kv || assets)) {
Expand Down Expand Up @@ -92,7 +92,7 @@ class Cache implements IncrementalCache {
value: CacheValue<IsFetch>,
isFetch?: IsFetch
): Promise<void> {
const kv = getCloudflareContext().env.NEXT_CACHE_WORKERS_KV;
const kv = getCloudflareContext().env.NEXT_CACHE_KV;

if (!kv) {
throw new IgnorableError(`No KVNamespace`);
Expand Down Expand Up @@ -120,7 +120,7 @@ class Cache implements IncrementalCache {
}

async delete(key: string): Promise<void> {
const kv = getCloudflareContext().env.NEXT_CACHE_WORKERS_KV;
const kv = getCloudflareContext().env.NEXT_CACHE_KV;

if (!kv) {
throw new IgnorableError(`No KVNamespace`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { debug, error } from "@opennextjs/aws/adapters/logger.js";
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides.js";
import { IgnorableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context.js";
import { getCloudflareContext } from "../../cloudflare-context.js";

/**
* An instance of the Incremental Cache that uses an R2 bucket (`NEXT_CACHE_R2_BUCKET`) as it's
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
import { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides.js";

import { getCloudflareContext } from "./cloudflare-context.js";
import { IncrementalCacheEntry } from "./internal/incremental-cache.js";
import { getCloudflareContext } from "../../cloudflare-context.js";
import { IncrementalCacheEntry } from "./internal.js";

const ONE_MINUTE_IN_SECONDS = 60;
const THIRTY_MINUTES_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 30;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides";
import { IgnorableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context";
import { getCloudflareContext } from "../../cloudflare-context";

export default {
name: "durable-queue",
name: "do-queue",
send: async (msg: QueueMessage) => {
const durableObject = getCloudflareContext().env.NEXT_CACHE_REVALIDATION_DURABLE_OBJECT;
const durableObject = getCloudflareContext().env.NEXT_CACHE_DO_REVALIDATION;
if (!durableObject) throw new IgnorableError("No durable object binding for cache revalidation");

const id = durableObject.idFromName(msg.MessageGroupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cache, { DEFAULT_REVALIDATION_TIMEOUT_MS } from "./memory-queue.js";
vi.mock("./.next/prerender-manifest.json", () => Promise.resolve({ preview: { previewModeId: "id" } }));

const mockServiceWorkerFetch = vi.fn();
vi.mock("./cloudflare-context", () => ({
vi.mock("../../cloudflare-context", () => ({
getCloudflareContext: () => ({
env: { NEXT_CACHE_REVALIDATION_WORKER: { fetch: mockServiceWorkerFetch } },
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { debug, error } from "@opennextjs/aws/adapters/logger.js";
import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides.js";
import { IgnorableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context";
import { getCloudflareContext } from "../../cloudflare-context";

export const DEFAULT_REVALIDATION_TIMEOUT_MS = 10_000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
import { RecoverableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context.js";
import { DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE } from "./constants.js";
import { getCloudflareContext } from "../../cloudflare-context.js";
import { DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE } from "./internal.js";

export class D1NextModeTagCache implements NextModeTagCache {
readonly mode = "nextMode" as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
import type { OriginalTagCache } from "@opennextjs/aws/types/overrides.js";
import { RecoverableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context.js";
import { DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE } from "./constants.js";
import { getCloudflareContext } from "../../cloudflare-context.js";
import { DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE, DEFAULT_NEXT_CACHE_D1_TAGS_TABLE } from "./internal.js";

/**
* An instance of the Tag Cache that uses a D1 binding (`NEXT_CACHE_D1`) as it's underlying data store.
Expand Down Expand Up @@ -154,7 +154,7 @@ class D1TagCache implements OriginalTagCache {
isDisabled: false as const,
db,
tables: {
tags: cfEnv.NEXT_CACHE_D1_TAGS_TABLE ?? "tags",
tags: cfEnv.NEXT_CACHE_D1_TAGS_TABLE ?? DEFAULT_NEXT_CACHE_D1_TAGS_TABLE,
revalidations: cfEnv.NEXT_CACHE_D1_REVALIDATIONS_TABLE ?? DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getMock = vi
.mockReturnValue({ hasBeenRevalidated: hasBeenRevalidatedMock, writeTags: writeTagsMock });
const waitUntilMock = vi.fn().mockImplementation(async (fn) => fn());
const sendDLQMock = vi.fn();
vi.mock("./cloudflare-context", () => ({
vi.mock("../../cloudflare-context", () => ({
getCloudflareContext: () => ({
env: {
NEXT_CACHE_DO_SHARDED: { idFromName: idFromNameMock, get: getMock },
Expand Down Expand Up @@ -83,7 +83,6 @@ describe("DOShardedTagCache", () => {
{ doId: expect.objectContaining({ shardId: "tag-hard;shard-1" }), tags: ["tag1"] },
];
const result = cache.groupTagsByDO({ tags: ["tag1", "_N_T_/tag1"], generateAllReplicas: true });
console.log(result);
expect(result).toEqual(expectedResult);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
import { IgnorableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context";
import { getCloudflareContext } from "../../cloudflare-context";

const SOFT_TAG_PREFIX = "_N_T_/";
export const DEFAULT_SOFT_REPLICAS = 4;
Expand Down Expand Up @@ -89,7 +89,7 @@ export class TagCacheDOId {
}
class ShardedDOTagCache implements NextModeTagCache {
readonly mode = "nextMode" as const;
readonly name = "sharded-do-tag-cache";
readonly name = "do-sharded-tag-cache";
readonly numSoftReplicas: number;
readonly numHardReplicas: number;
readonly maxWriteRetries: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DEFAULT_NEXT_CACHE_D1_REVALIDATIONS_TABLE = "revalidations";
export const DEFAULT_NEXT_CACHE_D1_TAGS_TABLE = "tags";
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "node:path";

import * as buildHelper from "@opennextjs/aws/build/helper.js";

import { CACHE_ASSET_DIR } from "../../../api/kv-cache.js";
import { CACHE_ASSET_DIR } from "../../../api/overrides/incremental-cache/kv-cache.js";

export function copyCacheAssets(options: buildHelper.BuildOptions) {
const { outputDir } = options;
Expand Down
Loading