Skip to content

Commit 9393ce2

Browse files
authored
Fixed: Redis keys ttl do not respect revalidate configurations (#33)
* Package bumps * Fixed revalidate (TTL) for full route cache * Bump version
1 parent f79430a commit 9393ce2

File tree

9 files changed

+274
-242
lines changed

9 files changed

+274
-242
lines changed

examples/redis-minimal/package-lock.json

Lines changed: 218 additions & 222 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/redis-minimal/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
"lint": "next lint"
1414
},
1515
"dependencies": {
16-
"@fortedigital/nextjs-cache-handler": "^2.0.0-canary12",
17-
"next": "15.3.4",
18-
"react": "^19.0.0",
19-
"react-dom": "^19.0.0",
20-
"redis": "^5.5.6"
16+
"@fortedigital/nextjs-cache-handler": "^2.0.0",
17+
"next": "15.4.1",
18+
"react": "^19.1.0",
19+
"react-dom": "^19.1.0",
20+
"redis": "^5.6.0"
2121
},
2222
"devDependencies": {
2323
"@eslint/eslintrc": "^3",
2424
"@tailwindcss/postcss": "^4",
25-
"@types/node": "^20",
25+
"@types/node": "^24",
2626
"@types/react": "^19",
2727
"@types/react-dom": "^19",
2828
"eslint": "^9",
29-
"eslint-config-next": "15.3.4",
29+
"eslint-config-next": "15.4.1",
3030
"tailwindcss": "^4",
3131
"typescript": "^5"
3232
}

package-lock.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/nextjs-cache-handler/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/nextjs-cache-handler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"next",
1919
"redis"
2020
],
21-
"version": "2.0.0",
21+
"version": "2.0.1",
2222
"type": "module",
2323
"license": "MIT",
2424
"description": "Next.js cache handlers",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const REVALIDATED_TAGS_KEY = "__revalidated_tags__";
1+
export const REVALIDATED_TAGS_KEY = "__revalidated_tags__";

packages/nextjs-cache-handler/src/handlers/cache-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import {
1616
} from "./cache-handler.types";
1717
import { PrerenderManifest } from "next/dist/build";
1818
import {
19-
type CachedFetchValue,
2019
type IncrementalCachedPageValue,
2120
type GetIncrementalResponseCacheContext,
2221
type GetIncrementalFetchCacheContext,
2322
IncrementalCacheValue,
2423
} from "next/dist/server/response-cache/types";
24+
import { resolveRevalidateValue } from "../helpers/resolveRevalidateValue";
2525

2626
const PRERENDER_MANIFEST_VERSION = 4;
2727

@@ -651,7 +651,7 @@ export class CacheHandler implements NextCacheHandler {
651651

652652
const { tags = [], internal_lastModified } = ctx ?? {};
653653

654-
const revalidate = (incrementalCacheValue as CachedFetchValue)?.revalidate;
654+
const revalidate = resolveRevalidateValue(incrementalCacheValue, ctx);
655655

656656
const lastModified = Math.round(internal_lastModified ?? Date.now());
657657

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
type CachedFetchValue,
3+
IncrementalCachedAppPageValue,
4+
SetIncrementalResponseCacheContext,
5+
} from "next/dist/server/response-cache";
6+
import {
7+
CacheHandlerParametersSet,
8+
Revalidate,
9+
} from "../handlers/cache-handler.types";
10+
11+
/**
12+
* Resolved revalidate value based on type of the cached value.
13+
*
14+
* @param incrementalCacheValue - Todo
15+
* @param ctx - todo
16+
*
17+
* @returns Cache revalidate value.
18+
*/
19+
export function resolveRevalidateValue(
20+
incrementalCacheValue: CacheHandlerParametersSet[1],
21+
ctx: CacheHandlerParametersSet[2] & {
22+
revalidate?: Revalidate;
23+
},
24+
) {
25+
const cachedFetchValue = incrementalCacheValue as CachedFetchValue;
26+
const cachedPageValue =
27+
incrementalCacheValue as IncrementalCachedAppPageValue;
28+
const responseCacheCtx = ctx as SetIncrementalResponseCacheContext;
29+
30+
let revalidate;
31+
32+
if (cachedFetchValue.kind === "FETCH") {
33+
revalidate = cachedFetchValue.revalidate;
34+
} else if (cachedPageValue.kind === "APP_PAGE") {
35+
revalidate = responseCacheCtx.cacheControl?.revalidate;
36+
}
37+
38+
return revalidate ?? ctx.revalidate;
39+
}

packages/nextjs-cache-handler/src/instrumentation/register-initial-cache.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import type { OutgoingHttpHeaders } from "http";
1313
import { getTagsFromHeaders } from "../helpers/getTagsFromHeaders";
1414
import { Revalidate } from "../handlers/cache-handler.types";
15+
import { resolveRevalidateValue } from "../helpers/resolveRevalidateValue";
1516

1617
type CacheHandlerType = typeof import("../handlers/cache-handler").CacheHandler;
1718

@@ -392,9 +393,11 @@ export async function registerInitialCache(
392393
return;
393394
}
394395

396+
const revalidateValue = fetchCache.revalidate;
397+
395398
// HACK: By default, Next.js sets the revalidate option to CACHE_ONE_YEAR if the revalidate option is set
396399
const revalidate =
397-
fetchCache.revalidate === CACHE_ONE_YEAR ? false : fetchCache.revalidate;
400+
revalidateValue === CACHE_ONE_YEAR ? false : revalidateValue;
398401

399402
try {
400403
await cacheHandler.set(fetchCacheKey, fetchCache, {

0 commit comments

Comments
 (0)