Skip to content

Commit b5e4a12

Browse files
authored
refactor(cache): code cleanup (#680)
1 parent 7140ca5 commit b5e4a12

File tree

3 files changed

+13
-51
lines changed

3 files changed

+13
-51
lines changed

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,13 @@ type IncrementalCacheContext = {
7373
tags?: string[] | undefined;
7474
};
7575

76-
interface CacheHandlerContext {
77-
fs?: never;
78-
dev?: boolean;
79-
flushToDisk?: boolean;
80-
serverDistDir?: string;
81-
maxMemoryCacheSize?: number;
82-
_appDir: boolean;
83-
_requestHeaders: never;
84-
fetchCacheKeyPrefix?: string;
85-
}
86-
8776
interface CacheHandlerValue {
8877
lastModified?: number;
8978
age?: number;
9079
cacheState?: string;
9180
value: IncrementalCacheValue | null;
9281
}
9382

94-
/** Beginning single backslash is intentional, to look for the dot + the extension. Do not escape it again. */
95-
const CACHE_EXTENSION_REGEX = /\.(cache|fetch)$/;
96-
97-
export function hasCacheExtension(key: string) {
98-
return CACHE_EXTENSION_REGEX.test(key);
99-
}
100-
10183
function isFetchCache(
10284
options?:
10385
| boolean
@@ -120,7 +102,7 @@ function isFetchCache(
120102
return false;
121103
}
122104
// We need to use globalThis client here as this class can be defined at load time in next 12 but client is not available at load time
123-
export default class S3Cache {
105+
export default class Cache {
124106
public async get(
125107
key: string,
126108
// fetchCache is for next 13.5 and above, kindHint is for next 14 and above and boolean is for earlier versions
@@ -152,7 +134,7 @@ export default class S3Cache {
152134
key,
153135
true,
154136
);
155-
// const { Body, LastModified } = await this.getS3Object(key, "fetch");
137+
156138
const _lastModified = await globalThis.tagCache.getLastModified(
157139
key,
158140
lastModified,
@@ -201,10 +183,7 @@ export default class S3Cache {
201183
try {
202184
const { value: cacheData, lastModified } =
203185
await globalThis.incrementalCache.get(key, false);
204-
// const { Body, LastModified } = await this.getS3Object(key, "cache");
205-
// const cacheData = JSON.parse(
206-
// (await Body?.transformToString()) ?? "{}",
207-
// ) as S3CachedFile;
186+
208187
const meta = cacheData?.meta;
209188
const _lastModified = await globalThis.tagCache.getLastModified(
210189
key,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface Queue {
2929

3030
// Incremental cache
3131

32-
export type S3CachedFile =
32+
export type CachedFile =
3333
| {
3434
type: "redirect";
3535
props?: Object;
@@ -53,16 +53,16 @@ export type S3CachedFile =
5353
meta?: Meta;
5454
};
5555

56-
export type S3FetchCache = Object;
56+
export type FetchCache = Object;
5757

5858
export type WithLastModified<T> = {
5959
lastModified?: number;
6060
value?: T;
6161
};
6262

6363
export type CacheValue<IsFetch extends boolean> = (IsFetch extends true
64-
? S3FetchCache
65-
: S3CachedFile) & { revalidate?: number | false };
64+
? FetchCache
65+
: CachedFile) & { revalidate?: number | false };
6666

6767
export type IncrementalCache = {
6868
get<IsFetch extends boolean = false>(

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
2-
import S3Cache, { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js";
2+
import Cache from "@opennextjs/aws/adapters/cache.js";
33
import { vi } from "vitest";
44

55
declare global {
66
var disableIncrementalCache: boolean;
77
var isNextAfter15: boolean;
88
}
99

10-
describe("hasCacheExtension", () => {
11-
it("Should returns true if has an extension and it is a CacheExtension", () => {
12-
expect(hasCacheExtension("hello.cache")).toBeTruthy();
13-
});
14-
15-
it("Should returns false if has an extension and it is not a CacheExtension", () => {
16-
expect(hasCacheExtension("hello.json")).toBeFalsy();
17-
});
18-
19-
it("Should return false if does not have any extension", () => {
20-
expect(hasCacheExtension("hello,json")).toBeFalsy();
21-
});
22-
});
23-
24-
describe("S3Cache", () => {
25-
let cache: S3Cache;
10+
describe("CacheHandler", () => {
11+
let cache: Cache;
2612

2713
vi.useFakeTimers().setSystemTime("2024-01-02T00:00:00Z");
28-
const getFetchCacheSpy = vi.spyOn(S3Cache.prototype, "getFetchCache");
29-
const getIncrementalCache = vi.spyOn(
30-
S3Cache.prototype,
31-
"getIncrementalCache",
32-
);
14+
const getFetchCacheSpy = vi.spyOn(Cache.prototype, "getFetchCache");
15+
const getIncrementalCache = vi.spyOn(Cache.prototype, "getIncrementalCache");
3316

3417
const incrementalCache = {
3518
name: "mock",
@@ -70,7 +53,7 @@ describe("S3Cache", () => {
7053
beforeEach(() => {
7154
vi.clearAllMocks();
7255

73-
cache = new S3Cache();
56+
cache = new Cache();
7457

7558
globalThis.disableIncrementalCache = false;
7659
globalThis.isNextAfter15 = false;

0 commit comments

Comments
 (0)