Skip to content

Commit 84142d2

Browse files
committed
updated types
1 parent 8121659 commit 84142d2

File tree

10 files changed

+101
-68
lines changed

10 files changed

+101
-68
lines changed

packages/open-next/src/adapters/cache.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default class Cache {
5757
async getFetchCache(key: string, softTags?: string[], tags?: string[]) {
5858
debug("get fetch cache", { key, softTags, tags });
5959
try {
60-
const cachedEntry = await globalThis.incrementalCache.get(key, true);
60+
const cachedEntry = await globalThis.incrementalCache.get(key, "fetch");
6161

6262
if (cachedEntry?.value === undefined) return null;
6363

@@ -107,7 +107,7 @@ export default class Cache {
107107

108108
async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> {
109109
try {
110-
const cachedEntry = await globalThis.incrementalCache.get(key, false);
110+
const cachedEntry = await globalThis.incrementalCache.get(key, "cache");
111111

112112
if (!cachedEntry?.value) {
113113
return null;
@@ -227,7 +227,7 @@ export default class Cache {
227227
},
228228
revalidate,
229229
},
230-
false,
230+
"cache",
231231
);
232232
break;
233233
}
@@ -248,7 +248,7 @@ export default class Cache {
248248
},
249249
revalidate,
250250
},
251-
false,
251+
"cache",
252252
);
253253
} else {
254254
await globalThis.incrementalCache.set(
@@ -259,7 +259,7 @@ export default class Cache {
259259
json: pageData,
260260
revalidate,
261261
},
262-
false,
262+
"cache",
263263
);
264264
}
265265
break;
@@ -278,12 +278,12 @@ export default class Cache {
278278
},
279279
revalidate,
280280
},
281-
false,
281+
"cache",
282282
);
283283
break;
284284
}
285285
case "FETCH":
286-
await globalThis.incrementalCache.set<true>(key, data, true);
286+
await globalThis.incrementalCache.set(key, data, "fetch");
287287
break;
288288
case "REDIRECT":
289289
await globalThis.incrementalCache.set(
@@ -293,7 +293,7 @@ export default class Cache {
293293
props: data.props,
294294
revalidate,
295295
},
296-
false,
296+
"cache",
297297
);
298298
break;
299299
case "IMAGE":

packages/open-next/src/adapters/composable-cache.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import type {
2-
ComposableCacheEntry,
3-
ComposableCacheHandler,
4-
StoredComposableCacheEntry,
5-
} from "types/cache";
1+
import type { ComposableCacheEntry, ComposableCacheHandler } from "types/cache";
62
import { fromReadableStream, toReadableStream } from "utils/stream";
73
import { debug } from "./logger";
84

95
export default {
106
async get(cacheKey: string) {
117
try {
12-
// TODO: update the type of the incremental cache
13-
const result = (await globalThis.incrementalCache.get(
8+
const result = await globalThis.incrementalCache.get(
149
cacheKey,
15-
true,
16-
)) as unknown as StoredComposableCacheEntry;
10+
"composable",
11+
);
12+
if (!result || !result.value?.value) {
13+
return undefined;
14+
}
1715

1816
debug("composable cache result", result);
1917

@@ -30,10 +28,14 @@ export default {
3028
async set(cacheKey: string, pendingEntry: Promise<ComposableCacheEntry>) {
3129
const entry = await pendingEntry;
3230
const valueToStore = await fromReadableStream(entry.value);
33-
await globalThis.incrementalCache.set(cacheKey, {
34-
...entry,
35-
value: valueToStore,
36-
} as any);
31+
await globalThis.incrementalCache.set(
32+
cacheKey,
33+
{
34+
...entry,
35+
value: valueToStore,
36+
},
37+
"composable",
38+
);
3739
},
3840

3941
async refreshTags() {

packages/open-next/src/core/routing/cacheInterceptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async function computeCacheControl(
8989
async function generateResult(
9090
event: InternalEvent,
9191
localizedPath: string,
92-
cachedValue: CacheValue<false>,
92+
cachedValue: CacheValue<"cache">,
9393
lastModified?: number,
9494
): Promise<InternalResult> {
9595
debug("Returning result from experimental cache");

packages/open-next/src/overrides/incrementalCache/multi-tier-ddb-s3.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { CacheValue, IncrementalCache } from "types/overrides";
1+
import type {
2+
CacheEntryType,
3+
CacheValue,
4+
IncrementalCache,
5+
} from "types/overrides";
26
import { customFetchClient } from "utils/fetch";
37
import { LRUCache } from "utils/lru";
48
import { debug } from "../../adapters/logger";
@@ -50,11 +54,14 @@ const buildDynamoKey = (key: string) => {
5054
*/
5155
const multiTierCache: IncrementalCache = {
5256
name: "multi-tier-ddb-s3",
53-
async get<IsFetch extends boolean = false>(key: string, isFetch?: IsFetch) {
57+
async get<CacheType extends CacheEntryType = "cache">(
58+
key: string,
59+
isFetch?: CacheType,
60+
) {
5461
// First we check the local cache
5562
const localCacheEntry = localCache.get(key) as
5663
| {
57-
value: CacheValue<IsFetch>;
64+
value: CacheValue<CacheType>;
5865
lastModified: number;
5966
}
6067
| undefined;

packages/open-next/src/overrides/incrementalCache/s3-lite.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ function buildS3Key(key: string, extension: Extension) {
4444
}
4545

4646
const incrementalCache: IncrementalCache = {
47-
async get(key, isFetch) {
48-
const result = await awsFetch(
49-
buildS3Key(key, isFetch ? "fetch" : "cache"),
50-
{
51-
method: "GET",
52-
},
53-
);
47+
async get(key, cacheType) {
48+
const result = await awsFetch(buildS3Key(key, cacheType ?? "cache"), {
49+
method: "GET",
50+
});
5451

5552
if (result.status === 404) {
5653
throw new IgnorableError("Not found");
@@ -66,14 +63,11 @@ const incrementalCache: IncrementalCache = {
6663
).getTime(),
6764
};
6865
},
69-
async set(key, value, isFetch): Promise<void> {
70-
const response = await awsFetch(
71-
buildS3Key(key, isFetch ? "fetch" : "cache"),
72-
{
73-
method: "PUT",
74-
body: JSON.stringify(value),
75-
},
76-
);
66+
async set(key, value, cacheType): Promise<void> {
67+
const response = await awsFetch(buildS3Key(key, cacheType ?? "cache"), {
68+
method: "PUT",
69+
body: JSON.stringify(value),
70+
});
7771
if (response.status !== 200) {
7872
throw new RecoverableError(`Failed to set cache: ${response.status}`);
7973
}

packages/open-next/src/overrides/incrementalCache/s3.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ function buildS3Key(key: string, extension: Extension) {
4040
}
4141

4242
const incrementalCache: IncrementalCache = {
43-
async get(key, isFetch) {
43+
async get(key, cacheType) {
4444
const result = await s3Client.send(
4545
new GetObjectCommand({
4646
Bucket: CACHE_BUCKET_NAME,
47-
Key: buildS3Key(key, isFetch ? "fetch" : "cache"),
47+
Key: buildS3Key(key, cacheType ?? "cache"),
4848
}),
4949
);
5050

@@ -56,11 +56,11 @@ const incrementalCache: IncrementalCache = {
5656
lastModified: result.LastModified?.getTime(),
5757
};
5858
},
59-
async set(key, value, isFetch): Promise<void> {
59+
async set(key, value, cacheType): Promise<void> {
6060
await s3Client.send(
6161
new PutObjectCommand({
6262
Bucket: CACHE_BUCKET_NAME,
63-
Key: buildS3Key(key, isFetch ? "fetch" : "cache"),
63+
Key: buildS3Key(key, cacheType ?? "cache"),
6464
Body: JSON.stringify(value),
6565
}),
6666
);

packages/open-next/src/types/cache.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface CacheHandlerValue {
8181
value: IncrementalCacheValue | null;
8282
}
8383

84-
export type Extension = "cache" | "fetch";
84+
export type Extension = "cache" | "fetch" | "composable";
8585

8686
type MetaHeaders = {
8787
"x-next-cache-tags"?: string;
@@ -141,3 +141,27 @@ export type IncrementalCacheContext =
141141
| SetIncrementalCacheContext
142142
| SetIncrementalFetchCacheContext
143143
| SetIncrementalResponseCacheContext;
144+
145+
export interface ComposableCacheEntry {
146+
value: ReadableStream<Uint8Array>;
147+
tags: string[];
148+
stale: number;
149+
timestamp: number;
150+
expire: number;
151+
revalidate: number;
152+
}
153+
154+
export type StoredComposableCacheEntry = Omit<ComposableCacheEntry, "value"> & {
155+
value: string;
156+
};
157+
158+
export interface ComposableCacheHandler {
159+
get(cacheKey: string): Promise<ComposableCacheEntry | undefined>;
160+
set(
161+
cacheKey: string,
162+
pendingEntry: Promise<ComposableCacheEntry>,
163+
): Promise<void>;
164+
refreshTags(): Promise<void>;
165+
getExpiration(...tags: string[]): Promise<number>;
166+
expireTags(...tags: string[]): Promise<void>;
167+
}

packages/open-next/src/types/overrides.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Readable } from "node:stream";
22

3-
import type { Meta } from "types/cache";
3+
import type { Extension, Meta, StoredComposableCacheEntry } from "types/cache";
4+
45
import type {
56
BaseEventOrResult,
67
BaseOverride,
@@ -77,24 +78,29 @@ export type WithLastModified<T> = {
7778
value?: T;
7879
};
7980

80-
export type CacheValue<IsFetch extends boolean> = (IsFetch extends true
81-
? CachedFetchValue
82-
: CachedFile) & {
83-
/**
84-
* This is available for page cache entry, but only at runtime.
85-
*/
86-
revalidate?: number | false;
87-
};
81+
export type CacheEntryType = Extension;
82+
83+
export type CacheValue<CacheType extends CacheEntryType> =
84+
(CacheType extends "fetch"
85+
? CachedFetchValue
86+
: CacheType extends "cache"
87+
? CachedFile
88+
: StoredComposableCacheEntry) & {
89+
/**
90+
* This is available for page cache entry, but only at runtime.
91+
*/
92+
revalidate?: number | false;
93+
};
8894

8995
export type IncrementalCache = {
90-
get<IsFetch extends boolean = false>(
96+
get<CacheType extends CacheEntryType = "cache">(
9197
key: string,
92-
isFetch?: IsFetch,
93-
): Promise<WithLastModified<CacheValue<IsFetch>> | null>;
94-
set<IsFetch extends boolean = false>(
98+
cacheType?: CacheType,
99+
): Promise<WithLastModified<CacheValue<CacheType>> | null>;
100+
set<CacheType extends CacheEntryType = "cache">(
95101
key: string,
96-
value: CacheValue<IsFetch>,
97-
isFetch?: IsFetch,
102+
value: CacheValue<CacheType>,
103+
isFetch?: CacheType,
98104
): Promise<void>;
99105
delete(key: string): Promise<void>;
100106
name: string;

packages/open-next/src/utils/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function hasBeenRevalidated(
2828
return _lastModified === -1;
2929
}
3030

31-
export function getTagsFromValue(value?: CacheValue<false>) {
31+
export function getTagsFromValue(value?: CacheValue<"cache">) {
3232
if (!value) {
3333
return [];
3434
}

packages/tests-unit/tests/adapters/cache.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ describe("CacheHandler", () => {
361361
expect(incrementalCache.set).toHaveBeenCalledWith(
362362
"key",
363363
{ type: "route", body: "{}", meta: { status: 200, headers: {} } },
364-
false,
364+
"cache",
365365
);
366366
});
367367

@@ -382,7 +382,7 @@ describe("CacheHandler", () => {
382382
body: Buffer.from("{}").toString("base64"),
383383
meta: { status: 200, headers: { "content-type": "image/png" } },
384384
},
385-
false,
385+
"cache",
386386
);
387387
});
388388

@@ -402,7 +402,7 @@ describe("CacheHandler", () => {
402402
html: "<html></html>",
403403
json: {},
404404
},
405-
false,
405+
"cache",
406406
);
407407
});
408408

@@ -423,7 +423,7 @@ describe("CacheHandler", () => {
423423
rsc: "rsc",
424424
meta: { status: 200, headers: {} },
425425
},
426-
false,
426+
"cache",
427427
);
428428
});
429429

@@ -444,7 +444,7 @@ describe("CacheHandler", () => {
444444
rsc: "rsc",
445445
meta: { status: 200, headers: {} },
446446
},
447-
false,
447+
"cache",
448448
);
449449
});
450450

@@ -474,7 +474,7 @@ describe("CacheHandler", () => {
474474
},
475475
revalidate: 60,
476476
},
477-
true,
477+
"fetch",
478478
);
479479
});
480480

@@ -487,7 +487,7 @@ describe("CacheHandler", () => {
487487
type: "redirect",
488488
props: {},
489489
},
490-
false,
490+
"cache",
491491
);
492492
});
493493

0 commit comments

Comments
 (0)