Skip to content

Commit

Permalink
avoid unnecessary type casting in kvCache
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Feb 7, 2025
1 parent 491dc53 commit fffd2ec
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions packages/cloudflare/src/api/kvCache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
CachedFetchValue,
CacheValue,
IncrementalCache,
WithLastModified,
} from "@opennextjs/aws/types/overrides";
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";
Expand Down Expand Up @@ -70,12 +65,15 @@ class Cache implements IncrementalCache {
// The cache can not be updated when there is no KV
// As we don't want to keep serving stale data for ever,
// we pretend the entry is not in cache
const entryValue = entry?.value as CachedFetchValue;
if (entryValue?.kind === "FETCH") {
const expires = entryValue.data.headers?.expires;
const expiresTime = new Date(expires as string).getTime();
if (
entry?.value &&
"kind" in entry.value &&
entry.value.kind === "FETCH" &&
entry.value.data?.headers?.expires
) {
const expiresTime = new Date(entry.value.data.headers.expires).getTime();
if (!isNaN(expiresTime) && expiresTime <= Date.now()) {
this.debug(`found expired entry (expire time: ${expires})`);
this.debug(`found expired entry (expire time: ${entry.value.data.headers.expires})`);
return null;
}
}
Expand Down

0 comments on commit fffd2ec

Please sign in to comment.