Skip to content

refactor(cache): code cleanup #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,13 @@ type IncrementalCacheContext = {
tags?: string[] | undefined;
};

interface CacheHandlerContext {
fs?: never;
dev?: boolean;
flushToDisk?: boolean;
serverDistDir?: string;
maxMemoryCacheSize?: number;
_appDir: boolean;
_requestHeaders: never;
fetchCacheKeyPrefix?: string;
}

interface CacheHandlerValue {
lastModified?: number;
age?: number;
cacheState?: string;
value: IncrementalCacheValue | null;
}

/** Beginning single backslash is intentional, to look for the dot + the extension. Do not escape it again. */
const CACHE_EXTENSION_REGEX = /\.(cache|fetch)$/;

export function hasCacheExtension(key: string) {
return CACHE_EXTENSION_REGEX.test(key);
}

function isFetchCache(
options?:
| boolean
Expand All @@ -120,7 +102,7 @@ function isFetchCache(
return false;
}
// 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
export default class S3Cache {
export default class Cache {
public async get(
key: string,
// fetchCache is for next 13.5 and above, kindHint is for next 14 and above and boolean is for earlier versions
Expand Down Expand Up @@ -152,7 +134,7 @@ export default class S3Cache {
key,
true,
);
// const { Body, LastModified } = await this.getS3Object(key, "fetch");

const _lastModified = await globalThis.tagCache.getLastModified(
key,
lastModified,
Expand Down Expand Up @@ -201,10 +183,7 @@ export default class S3Cache {
try {
const { value: cacheData, lastModified } =
await globalThis.incrementalCache.get(key, false);
// const { Body, LastModified } = await this.getS3Object(key, "cache");
// const cacheData = JSON.parse(
// (await Body?.transformToString()) ?? "{}",
// ) as S3CachedFile;

const meta = cacheData?.meta;
const _lastModified = await globalThis.tagCache.getLastModified(
key,
Expand Down
8 changes: 4 additions & 4 deletions packages/open-next/src/types/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface Queue {

// Incremental cache

export type S3CachedFile =
export type CachedFile =
| {
type: "redirect";
props?: Object;
Expand All @@ -53,16 +53,16 @@ export type S3CachedFile =
meta?: Meta;
};

export type S3FetchCache = Object;
export type FetchCache = Object;

export type WithLastModified<T> = {
lastModified?: number;
value?: T;
};

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

export type IncrementalCache = {
get<IsFetch extends boolean = false>(
Expand Down
29 changes: 6 additions & 23 deletions packages/tests-unit/tests/adapters/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
/* eslint-disable sonarjs/no-duplicate-string */
import S3Cache, { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js";
import Cache from "@opennextjs/aws/adapters/cache.js";
import { vi } from "vitest";

declare global {
var disableIncrementalCache: boolean;
var isNextAfter15: boolean;
}

describe("hasCacheExtension", () => {
it("Should returns true if has an extension and it is a CacheExtension", () => {
expect(hasCacheExtension("hello.cache")).toBeTruthy();
});

it("Should returns false if has an extension and it is not a CacheExtension", () => {
expect(hasCacheExtension("hello.json")).toBeFalsy();
});

it("Should return false if does not have any extension", () => {
expect(hasCacheExtension("hello,json")).toBeFalsy();
});
});

describe("S3Cache", () => {
let cache: S3Cache;
describe("CacheHandler", () => {
let cache: Cache;

vi.useFakeTimers().setSystemTime("2024-01-02T00:00:00Z");
const getFetchCacheSpy = vi.spyOn(S3Cache.prototype, "getFetchCache");
const getIncrementalCache = vi.spyOn(
S3Cache.prototype,
"getIncrementalCache",
);
const getFetchCacheSpy = vi.spyOn(Cache.prototype, "getFetchCache");
const getIncrementalCache = vi.spyOn(Cache.prototype, "getIncrementalCache");

const incrementalCache = {
name: "mock",
Expand Down Expand Up @@ -70,7 +53,7 @@ describe("S3Cache", () => {
beforeEach(() => {
vi.clearAllMocks();

cache = new S3Cache();
cache = new Cache();

globalThis.disableIncrementalCache = false;
globalThis.isNextAfter15 = false;
Expand Down
Loading