Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 9 additions & 12 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,18 @@ export default class Cache {
async getFetchCache(key: string, softTags?: string[], tags?: string[]) {
debug("get fetch cache", { key, softTags, tags });
try {
const { value, lastModified } = await globalThis.incrementalCache.get(
key,
true,
);
const cachedEntry = await globalThis.incrementalCache.get(key, true);

const _lastModified = await globalThis.tagCache.getLastModified(
key,
lastModified,
cachedEntry?.lastModified,
);
if (_lastModified === -1) {
// If some tags are stale we need to force revalidation
return null;
}

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

// For cases where we don't have tags, we need to ensure that the soft tags are not being revalidated
// We only need to check for the path as it should already contain all the tags
Expand All @@ -159,7 +156,7 @@ export default class Cache {
if (path) {
const pathLastModified = await globalThis.tagCache.getLastModified(
path.replace("_N_T_/", ""),
lastModified,
cachedEntry.lastModified,
);
if (pathLastModified === -1) {
// In case the path has been revalidated, we don't want to use the fetch cache
Expand All @@ -170,7 +167,7 @@ export default class Cache {

return {
lastModified: _lastModified,
value: value,
value: cachedEntry.value,
} as CacheHandlerValue;
} catch (e) {
// We can usually ignore errors here as they are usually due to cache not being found
Expand All @@ -181,18 +178,18 @@ export default class Cache {

async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> {
try {
const { value: cacheData, lastModified } =
await globalThis.incrementalCache.get(key, false);
const cachedEntry = await globalThis.incrementalCache.get(key, false);

const meta = cacheData?.meta;
const meta = cachedEntry?.value?.meta;
const _lastModified = await globalThis.tagCache.getLastModified(
key,
lastModified,
cachedEntry?.lastModified,
);
if (_lastModified === -1) {
// If some tags are stale we need to force revalidation
return null;
}
const cacheData = cachedEntry?.value;
const requestId = globalThis.__openNextAls.getStore()?.requestId ?? "";
globalThis.lastModified[requestId] = _lastModified;
if (cacheData?.type === "route") {
Expand Down
4 changes: 2 additions & 2 deletions packages/open-next/src/core/routing/cacheInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export async function cacheInterceptor(
try {
const cachedData = await globalThis.incrementalCache.get(localizedPath);
debug("cached data in interceptor", cachedData);
if (cachedData.value?.type === "app") {
if (cachedData?.value?.type === "app") {
// We need to check the tag cache now
const _lastModified = await globalThis.tagCache.getLastModified(
localizedPath,
Expand All @@ -169,7 +169,7 @@ export async function cacheInterceptor(
}
}
const host = event.headers.host;
switch (cachedData.value?.type) {
switch (cachedData?.value?.type) {
case "app":
case "page":
return generateResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const multiTierCache: IncrementalCache = {
}
}
const result = await S3Cache.get(key, isFetch);
if (result.value) {
if (result?.value) {
localCache.set(key, {
value: result.value,
lastModified: result.lastModified ?? Date.now(),
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/types/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type IncrementalCache = {
get<IsFetch extends boolean = false>(
key: string,
isFetch?: IsFetch,
): Promise<WithLastModified<CacheValue<IsFetch>>>;
): Promise<WithLastModified<CacheValue<IsFetch>> | null>;
set<IsFetch extends boolean = false>(
key: string,
value: CacheValue<IsFetch>,
Expand Down