Skip to content

Commit 797b3d4

Browse files
committed
Code refactoring
1 parent 197b1a5 commit 797b3d4

File tree

16 files changed

+463
-483
lines changed

16 files changed

+463
-483
lines changed

.changeset/khaki-spies-notice.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
Code refactoring
6+
7+
BREAKING CHANGES
8+
9+
- `DurableObjectQueueHandler` renamed to `DOQueueHandler`
10+
- `NEXT_CACHE_DO_QUEUE_MAX_NUM_REVALIDATIONS` renamed to `NEXT_CACHE_DO_QUEUE_MAX_RETRIES`
11+
- `D1TagCache` has been removed, use `D1NextModeTagCache` instead.
12+
- The `enableShardReplication` and `shardReplicationOptions` options passed to `ShardedDOTagCache`
13+
have been folded into `shardReplication`. A value for `shardReplication` must be specified to enable
14+
replications. The value must be an object with the number of soft and hard replicas.

examples/bugs/gh-223/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"postcss": "^8.4.49",
3333
"tailwindcss": "^3.4.17",
3434
"typescript": "^5.7.2",
35-
"wrangler": "^3.107.0"
35+
"wrangler": "catalog:"
3636
}
3737
}

examples/e2e/app-router/open-next.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export default defineCloudflareConfig({
88
// With such a configuration, we could have up to 12 * (8 + 2) = 120 Durable Objects instances
99
tagCache: shardedTagCache({
1010
baseShardSize: 12,
11-
enableShardReplication: true,
12-
shardReplicationOptions: {
11+
shardReplication: {
1312
numberOfSoftReplicas: 8,
1413
numberOfHardReplicas: 2,
1514
},

examples/e2e/app-router/wrangler.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"bindings": [
1313
{
1414
"name": "NEXT_CACHE_DO_QUEUE",
15-
"class_name": "DurableObjectQueueHandler"
15+
"class_name": "DOQueueHandler"
1616
},
1717
{
1818
"name": "NEXT_TAG_CACHE_DO_SHARDED",
@@ -23,7 +23,7 @@
2323
"migrations": [
2424
{
2525
"tag": "v1",
26-
"new_sqlite_classes": ["DurableObjectQueueHandler", "DOShardedTagCache"]
26+
"new_sqlite_classes": ["DOQueueHandler", "DOShardedTagCache"]
2727
}
2828
],
2929
"kv_namespaces": [
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
2-
import d1TagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-tag-cache";
2+
import d1NextTagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-next-tag-cache";
33
import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";
44
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
55
import { withRegionalCache } from "@opennextjs/cloudflare/overrides/incremental-cache/regional-cache";
@@ -9,6 +9,6 @@ export default defineCloudflareConfig({
99
mode: "long-lived",
1010
shouldLazilyUpdateOnCacheHit: true,
1111
}),
12-
tagCache: d1TagCache,
12+
tagCache: d1NextTagCache,
1313
queue: memoryQueue,
1414
});

packages/cloudflare/src/api/cloudflare-context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Context, RunningCodeOptions } from "node:vm";
22

33
import type { GetPlatformProxyOptions } from "wrangler";
44

5-
import type { DurableObjectQueueHandler } from "./durable-objects/queue";
5+
import type { DOQueueHandler } from "./durable-objects/queue";
66
import { DOShardedTagCache } from "./durable-objects/sharded-tag-cache";
77

88
declare global {
@@ -35,7 +35,7 @@ declare global {
3535
NEXT_TAG_CACHE_DO_SHARDED_DLQ?: Queue;
3636

3737
// Durable Object namespace to use for the durable object queue
38-
NEXT_CACHE_DO_QUEUE?: DurableObjectNamespace<DurableObjectQueueHandler>;
38+
NEXT_CACHE_DO_QUEUE?: DurableObjectNamespace<DOQueueHandler>;
3939

4040
// Below are the optional environment variables to configure the durable object queue
4141
// The max number of revalidations that can be processed by the durable worker at the same time
@@ -46,7 +46,7 @@ declare global {
4646
// If it fails again it will exponentially back off until it reaches the max retry interval
4747
NEXT_CACHE_DO_QUEUE_RETRY_INTERVAL_MS?: string;
4848
// The maximum number of attempts that can be made to revalidate a path
49-
NEXT_CACHE_DO_QUEUE_MAX_NUM_REVALIDATIONS?: string;
49+
NEXT_CACHE_DO_QUEUE_MAX_RETRIES?: string;
5050
// Disable SQLite for the durable object queue handler
5151
// This can be safely used if you don't use an eventually consistent incremental cache (i.e. R2 without the regional cache for example)
5252
NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE?: string;

packages/cloudflare/src/api/durable-objects/queue.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it, vi } from "vitest";
22

3-
import { DurableObjectQueueHandler } from "./queue";
3+
import { DOQueueHandler } from "./queue";
44

55
vi.mock("cloudflare:workers", () => ({
66
DurableObject: class {
@@ -36,7 +36,7 @@ const createDurableObjectQueue = ({
3636
},
3737
};
3838
// eslint-disable-next-line @typescript-eslint/no-explicit-any
39-
return new DurableObjectQueueHandler(mockState as any, {
39+
return new DOQueueHandler(mockState as any, {
4040
WORKER_SELF_REFERENCE: {
4141
fetch: vi.fn().mockReturnValue(
4242
new Promise<Response>((res) =>
@@ -198,7 +198,7 @@ describe("DurableObjectQueue", () => {
198198
});
199199

200200
describe("addAlarm", () => {
201-
const getStorage = (queue: DurableObjectQueueHandler): DurableObjectStorage => {
201+
const getStorage = (queue: DOQueueHandler): DurableObjectStorage => {
202202
// @ts-expect-error - ctx is a protected field
203203
return queue.ctx.storage;
204204
};

packages/cloudflare/src/api/durable-objects/queue.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import { DurableObject } from "cloudflare:workers";
1111
const DEFAULT_MAX_REVALIDATION = 5;
1212
const DEFAULT_REVALIDATION_TIMEOUT_MS = 10_000;
1313
const DEFAULT_RETRY_INTERVAL_MS = 2_000;
14-
const DEFAULT_MAX_NUM_REVALIDATIONS = 6;
14+
const DEFAULT_MAX_RETRIES = 6;
1515

1616
interface FailedState {
1717
msg: QueueMessage;
1818
retryCount: number;
1919
nextAlarmMs: number;
2020
}
2121

22-
export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
22+
export class DOQueueHandler extends DurableObject<CloudflareEnv> {
2323
// Ongoing revalidations are deduped by the deduplication id
2424
// Since this is running in waitUntil, we expect the durable object state to persist this during the duration of the revalidation
2525
// TODO: handle incremental cache with only eventual consistency (i.e. KV or R2/D1 with the optional cache layer on top)
@@ -35,7 +35,7 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
3535
readonly maxRevalidations: number;
3636
readonly revalidationTimeout: number;
3737
readonly revalidationRetryInterval: number;
38-
readonly maxRevalidationAttempts: number;
38+
readonly maxRetries: number;
3939
readonly disableSQLite: boolean;
4040

4141
constructor(ctx: DurableObjectState, env: CloudflareEnv) {
@@ -57,9 +57,9 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
5757
? parseInt(env.NEXT_CACHE_DO_QUEUE_RETRY_INTERVAL_MS)
5858
: DEFAULT_RETRY_INTERVAL_MS;
5959

60-
this.maxRevalidationAttempts = env.NEXT_CACHE_DO_QUEUE_MAX_NUM_REVALIDATIONS
61-
? parseInt(env.NEXT_CACHE_DO_QUEUE_MAX_NUM_REVALIDATIONS)
62-
: DEFAULT_MAX_NUM_REVALIDATIONS;
60+
this.maxRetries = env.NEXT_CACHE_DO_QUEUE_MAX_RETRIES
61+
? parseInt(env.NEXT_CACHE_DO_QUEUE_MAX_RETRIES)
62+
: DEFAULT_MAX_RETRIES;
6363

6464
this.disableSQLite = env.NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE === "true";
6565

@@ -199,10 +199,9 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
199199
let updatedFailedState: FailedState;
200200

201201
if (existingFailedState) {
202-
if (existingFailedState.retryCount >= this.maxRevalidationAttempts) {
203-
// We give up after 6 retries and log the error
202+
if (existingFailedState.retryCount >= this.maxRetries) {
204203
error(
205-
`The revalidation for ${msg.MessageBody.host}${msg.MessageBody.url} has failed after 6 retries. It will not be tried again, but subsequent ISR requests will retry.`
204+
`The revalidation for ${msg.MessageBody.host}${msg.MessageBody.url} has failed after ${this.maxRetries} retries. It will not be tried again, but subsequent ISR requests will retry.`
206205
);
207206
this.routeInFailedState.delete(msg.MessageDeduplicationId);
208207
return;

packages/cloudflare/src/api/overrides/incremental-cache/kv-incremental-cache.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ export const CACHE_ASSET_DIR = "cdn-cgi/_next_cache";
77

88
export const STATUS_DELETED = 1;
99

10+
export const CACHE_NAME = "cf-kv-incremental-cache";
11+
1012
/**
1113
* Open Next cache based on cloudflare KV and Assets.
1214
*
1315
* Note: The class is instantiated outside of the request context.
1416
* The cloudflare context and process.env are not initialized yet
1517
* when the constructor is called.
1618
*/
17-
class Cache implements IncrementalCache {
18-
readonly name = "cloudflare-kv";
19+
class KVIncrementalCache implements IncrementalCache {
20+
readonly name = CACHE_NAME;
1921

2022
async get<IsFetch extends boolean = false>(
2123
key: string,
@@ -158,4 +160,4 @@ class Cache implements IncrementalCache {
158160
}
159161
}
160162

161-
export default new Cache();
163+
export default new KVIncrementalCache();

packages/cloudflare/src/api/overrides/incremental-cache/r2-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { getCloudflareContext } from "../../cloudflare-context.js";
1212
* environment variable, and defaults to `incremental-cache`.
1313
*/
1414
class R2IncrementalCache implements IncrementalCache {
15-
readonly name = "r2-incremental-cache";
15+
readonly name = "cf-r2-incremental-cache";
1616

1717
async get<IsFetch extends boolean = false>(
1818
key: string,

0 commit comments

Comments
 (0)