@trieb.work/nextjs-turbo-redis-cache 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/.github/workflows/ci.yml +31 -6
  2. package/.github/workflows/release.yml +7 -3
  3. package/.next/trace +11 -0
  4. package/.vscode/settings.json +10 -0
  5. package/CHANGELOG.md +71 -0
  6. package/README.md +154 -34
  7. package/dist/index.d.mts +96 -20
  8. package/dist/index.d.ts +96 -20
  9. package/dist/index.js +317 -61
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +313 -61
  12. package/dist/index.mjs.map +1 -1
  13. package/package.json +14 -7
  14. package/scripts/vitest-run-staged.cjs +1 -1
  15. package/src/CachedHandler.ts +23 -9
  16. package/src/DeduplicatedRequestHandler.ts +50 -1
  17. package/src/RedisStringsHandler.ts +331 -91
  18. package/src/SyncedMap.ts +74 -4
  19. package/src/ZodHandler.ts +45 -0
  20. package/src/index.ts +4 -2
  21. package/src/utils/debug.ts +30 -0
  22. package/src/utils/json.ts +26 -0
  23. package/test/integration/next-app-15-0-3/README.md +36 -0
  24. package/test/integration/next-app-15-0-3/eslint.config.mjs +16 -0
  25. package/test/integration/next-app-15-0-3/next.config.js +6 -0
  26. package/test/integration/next-app-15-0-3/package-lock.json +5833 -0
  27. package/test/integration/next-app-15-0-3/package.json +29 -0
  28. package/test/integration/next-app-15-0-3/pnpm-lock.yaml +3679 -0
  29. package/test/integration/next-app-15-0-3/postcss.config.mjs +5 -0
  30. package/test/integration/next-app-15-0-3/public/file.svg +1 -0
  31. package/test/integration/next-app-15-0-3/public/globe.svg +1 -0
  32. package/test/integration/next-app-15-0-3/public/next.svg +1 -0
  33. package/test/integration/next-app-15-0-3/public/vercel.svg +1 -0
  34. package/test/integration/next-app-15-0-3/public/window.svg +1 -0
  35. package/test/integration/next-app-15-0-3/src/app/api/cached-static-fetch/route.ts +18 -0
  36. package/test/integration/next-app-15-0-3/src/app/api/nested-fetch-in-api-route/revalidated-fetch/route.ts +27 -0
  37. package/test/integration/next-app-15-0-3/src/app/api/revalidatePath/route.ts +15 -0
  38. package/test/integration/next-app-15-0-3/src/app/api/revalidateTag/route.ts +15 -0
  39. package/test/integration/next-app-15-0-3/src/app/api/revalidated-fetch/route.ts +17 -0
  40. package/test/integration/next-app-15-0-3/src/app/api/uncached-fetch/route.ts +15 -0
  41. package/test/integration/next-app-15-0-3/src/app/globals.css +26 -0
  42. package/test/integration/next-app-15-0-3/src/app/layout.tsx +59 -0
  43. package/test/integration/next-app-15-0-3/src/app/page.tsx +755 -0
  44. package/test/integration/next-app-15-0-3/src/app/pages/cached-static-fetch/default--force-dynamic-page/page.tsx +19 -0
  45. package/test/integration/next-app-15-0-3/src/app/pages/cached-static-fetch/revalidate15--default-page/page.tsx +34 -0
  46. package/test/integration/next-app-15-0-3/src/app/pages/cached-static-fetch/revalidate15--force-dynamic-page/page.tsx +25 -0
  47. package/test/integration/next-app-15-0-3/src/app/pages/no-fetch/default-page/page.tsx +55 -0
  48. package/test/integration/next-app-15-0-3/src/app/pages/revalidated-fetch/default--force-dynamic-page/page.tsx +19 -0
  49. package/test/integration/next-app-15-0-3/src/app/pages/revalidated-fetch/revalidate15--default-page/page.tsx +35 -0
  50. package/test/integration/next-app-15-0-3/src/app/pages/revalidated-fetch/revalidate15--force-dynamic-page/page.tsx +25 -0
  51. package/test/integration/next-app-15-0-3/src/app/pages/uncached-fetch/default--force-dynamic-page/page.tsx +19 -0
  52. package/test/integration/next-app-15-0-3/src/app/pages/uncached-fetch/revalidate15--default-page/page.tsx +32 -0
  53. package/test/integration/next-app-15-0-3/src/app/pages/uncached-fetch/revalidate15--force-dynamic-page/page.tsx +25 -0
  54. package/test/integration/next-app-15-0-3/src/app/revalidation-interface.tsx +267 -0
  55. package/test/integration/next-app-15-0-3/tsconfig.json +27 -0
  56. package/test/integration/next-app-15-3-2/README.md +36 -0
  57. package/test/integration/next-app-15-3-2/eslint.config.mjs +16 -0
  58. package/test/integration/next-app-15-3-2/next.config.js +6 -0
  59. package/test/integration/next-app-15-3-2/package-lock.json +5969 -0
  60. package/test/integration/next-app-15-3-2/package.json +33 -0
  61. package/test/integration/next-app-15-3-2/pnpm-lock.yaml +3688 -0
  62. package/test/integration/next-app-15-3-2/postcss.config.mjs +5 -0
  63. package/test/integration/next-app-15-3-2/public/file.svg +1 -0
  64. package/test/integration/next-app-15-3-2/public/globe.svg +1 -0
  65. package/test/integration/next-app-15-3-2/public/next.svg +1 -0
  66. package/test/integration/next-app-15-3-2/public/vercel.svg +1 -0
  67. package/test/integration/next-app-15-3-2/public/window.svg +1 -0
  68. package/test/integration/next-app-15-3-2/src/app/api/cached-static-fetch/route.ts +18 -0
  69. package/test/integration/next-app-15-3-2/src/app/api/nested-fetch-in-api-route/revalidated-fetch/route.ts +27 -0
  70. package/test/integration/next-app-15-3-2/src/app/api/revalidatePath/route.ts +15 -0
  71. package/test/integration/next-app-15-3-2/src/app/api/revalidateTag/route.ts +15 -0
  72. package/test/integration/next-app-15-3-2/src/app/api/revalidated-fetch/route.ts +17 -0
  73. package/test/integration/next-app-15-3-2/src/app/api/uncached-fetch/route.ts +15 -0
  74. package/test/integration/next-app-15-3-2/src/app/globals.css +26 -0
  75. package/test/integration/next-app-15-3-2/src/app/layout.tsx +59 -0
  76. package/test/integration/next-app-15-3-2/src/app/page.tsx +755 -0
  77. package/test/integration/next-app-15-3-2/src/app/pages/cached-static-fetch/default--force-dynamic-page/page.tsx +19 -0
  78. package/test/integration/next-app-15-3-2/src/app/pages/cached-static-fetch/revalidate15--default-page/page.tsx +34 -0
  79. package/test/integration/next-app-15-3-2/src/app/pages/cached-static-fetch/revalidate15--force-dynamic-page/page.tsx +25 -0
  80. package/test/integration/next-app-15-3-2/src/app/pages/no-fetch/default-page/page.tsx +55 -0
  81. package/test/integration/next-app-15-3-2/src/app/pages/revalidated-fetch/default--force-dynamic-page/page.tsx +19 -0
  82. package/test/integration/next-app-15-3-2/src/app/pages/revalidated-fetch/revalidate15--default-page/page.tsx +35 -0
  83. package/test/integration/next-app-15-3-2/src/app/pages/revalidated-fetch/revalidate15--force-dynamic-page/page.tsx +25 -0
  84. package/test/integration/next-app-15-3-2/src/app/pages/uncached-fetch/default--force-dynamic-page/page.tsx +19 -0
  85. package/test/integration/next-app-15-3-2/src/app/pages/uncached-fetch/revalidate15--default-page/page.tsx +32 -0
  86. package/test/integration/next-app-15-3-2/src/app/pages/uncached-fetch/revalidate15--force-dynamic-page/page.tsx +25 -0
  87. package/test/integration/next-app-15-3-2/src/app/revalidation-interface.tsx +267 -0
  88. package/test/integration/next-app-15-3-2/tsconfig.json +27 -0
  89. package/test/integration/next-app-customized/README.md +36 -0
  90. package/test/integration/next-app-customized/customized-cache-handler.js +34 -0
  91. package/test/integration/next-app-customized/eslint.config.mjs +16 -0
  92. package/test/integration/next-app-customized/next.config.js +6 -0
  93. package/test/integration/nextjs-cache-handler.integration.test.ts +859 -0
  94. package/vite.config.ts +23 -8
@@ -16,6 +16,7 @@ permissions:
16
16
  statuses: write
17
17
  issues: write
18
18
  actions: write
19
+ discussions: write
19
20
 
20
21
  jobs:
21
22
  build:
@@ -27,28 +28,52 @@ jobs:
27
28
  - name: Checkout code
28
29
  uses: actions/checkout@v3
29
30
 
31
+ - name: Install pnpm
32
+ run: corepack enable
33
+
30
34
  - name: Setup Node.js
31
35
  uses: actions/setup-node@v3
32
36
  with:
33
37
  node-version: '20'
38
+ cache: 'pnpm'
34
39
 
35
40
  - name: Install dependencies
36
- run: npm ci
41
+ run: pnpm install
37
42
 
38
43
  - name: Run lint
39
- run: npm run lint
44
+ run: pnpm lint
45
+
46
+ - name: Build project
47
+ run: pnpm build
48
+
49
+ - name: Start Redis
50
+ uses: supercharge/redis-github-action@1.8.0
51
+ with:
52
+ redis-version: '7'
53
+ redis-port: 6379
54
+
55
+ - name: Install redis-cli
56
+ run: sudo apt-get update && sudo apt-get install -y redis-tools
57
+
58
+ - name: Configure Redis Keyspace Notifications
59
+ run: redis-cli config set notify-keyspace-events Exe
60
+
61
+ - name: Install Integration Test Project
62
+ run: cd test/integration/next-app-15-3-2 && pnpm install
63
+
64
+ - name: Build Integration Test Project
65
+ run: cd test/integration/next-app-15-3-2 && pnpm build
40
66
 
41
67
  - name: Run tests
42
- run: npm run test
68
+ run: pnpm test
69
+ env:
70
+ SKIP_BUILD: true
43
71
 
44
72
  - name: Code Coverage Comments
45
73
  uses: kcjpop/coverage-comments@v2.2
46
74
  with:
47
75
  github-token: ${{ secrets.GITHUB_TOKEN }}
48
76
  coverage-file: './coverage/lcov.info'
49
-
50
- - name: Build project
51
- run: npm run build
52
77
 
53
78
  - name: Dry run the release
54
79
  env:
@@ -14,6 +14,7 @@ permissions:
14
14
  statuses: write
15
15
  issues: write
16
16
  actions: write
17
+ discussions: write
17
18
 
18
19
  jobs:
19
20
  release:
@@ -23,17 +24,20 @@ jobs:
23
24
  - name: Checkout code
24
25
  uses: actions/checkout@v3
25
26
 
27
+ - name: Install pnpm
28
+ run: corepack enable
29
+
26
30
  - name: Setup Node.js
27
31
  uses: actions/setup-node@v3
28
32
  with:
29
33
  node-version: '20'
30
- cache: 'npm'
34
+ cache: 'pnpm'
31
35
 
32
36
  - name: Install dependencies
33
- run: npm ci
37
+ run: pnpm install
34
38
 
35
39
  - name: Build project
36
- run: npm run build
40
+ run: pnpm run build
37
41
 
38
42
  - name: Run Semantic Release
39
43
  env:
package/.next/trace ADDED
@@ -0,0 +1,11 @@
1
+ [{"name":"generate-buildid","duration":140,"timestamp":4515754197453,"id":4,"parentId":1,"tags":{},"startTime":1746464013531,"traceId":"c44ff29d5c06adcb"},{"name":"load-custom-routes","duration":167,"timestamp":4515754197651,"id":5,"parentId":1,"tags":{},"startTime":1746464013531,"traceId":"c44ff29d5c06adcb"},{"name":"next-build","duration":581845,"timestamp":4515753617731,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464012951,"traceId":"c44ff29d5c06adcb"}]
2
+ [{"name":"generate-buildid","duration":217,"timestamp":4515766041037,"id":4,"parentId":1,"tags":{},"startTime":1746464025375,"traceId":"6cf74253dfcd5659"},{"name":"load-custom-routes","duration":199,"timestamp":4515766041361,"id":5,"parentId":1,"tags":{},"startTime":1746464025375,"traceId":"6cf74253dfcd5659"},{"name":"next-build","duration":97220,"timestamp":4515765945991,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464025280,"traceId":"6cf74253dfcd5659"}]
3
+ [{"name":"generate-buildid","duration":131,"timestamp":4515813285753,"id":4,"parentId":1,"tags":{},"startTime":1746464072620,"traceId":"fcfa690a3abfa3ea"},{"name":"load-custom-routes","duration":186,"timestamp":4515813285997,"id":5,"parentId":1,"tags":{},"startTime":1746464072620,"traceId":"fcfa690a3abfa3ea"},{"name":"next-build","duration":60933,"timestamp":4515813226764,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464072561,"traceId":"fcfa690a3abfa3ea"}]
4
+ [{"name":"generate-buildid","duration":120,"timestamp":4515831765983,"id":4,"parentId":1,"tags":{},"startTime":1746464091100,"traceId":"41bfa0fcaa95d6de"},{"name":"load-custom-routes","duration":162,"timestamp":4515831766158,"id":5,"parentId":1,"tags":{},"startTime":1746464091100,"traceId":"41bfa0fcaa95d6de"},{"name":"next-build","duration":64360,"timestamp":4515831703128,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464091037,"traceId":"41bfa0fcaa95d6de"}]
5
+ [{"name":"generate-buildid","duration":138,"timestamp":4515903825041,"id":4,"parentId":1,"tags":{},"startTime":1746464163159,"traceId":"b39fefc0a6cc67ce"},{"name":"load-custom-routes","duration":168,"timestamp":4515903825245,"id":5,"parentId":1,"tags":{},"startTime":1746464163160,"traceId":"b39fefc0a6cc67ce"},{"name":"next-build","duration":62582,"timestamp":4515903763978,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464163098,"traceId":"b39fefc0a6cc67ce"}]
6
+ [{"name":"generate-buildid","duration":131,"timestamp":4515927634537,"id":4,"parentId":1,"tags":{},"startTime":1746464186969,"traceId":"a7e49e03eb857d5c"},{"name":"load-custom-routes","duration":363,"timestamp":4515927634726,"id":5,"parentId":1,"tags":{},"startTime":1746464186969,"traceId":"a7e49e03eb857d5c"},{"name":"next-build","duration":68467,"timestamp":4515927568131,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464186903,"traceId":"a7e49e03eb857d5c"}]
7
+ [{"name":"generate-buildid","duration":109,"timestamp":4516102680149,"id":4,"parentId":1,"tags":{},"startTime":1746464362001,"traceId":"ad1b689177cf0789"},{"name":"load-custom-routes","duration":149,"timestamp":4516102680307,"id":5,"parentId":1,"tags":{},"startTime":1746464362001,"traceId":"ad1b689177cf0789"},{"name":"next-build","duration":56682,"timestamp":4516102625265,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464361946,"traceId":"ad1b689177cf0789"}]
8
+ [{"name":"generate-buildid","duration":108,"timestamp":4516106451659,"id":4,"parentId":1,"tags":{},"startTime":1746464365773,"traceId":"92f2d146f1781280"},{"name":"load-custom-routes","duration":154,"timestamp":4516106451818,"id":5,"parentId":1,"tags":{},"startTime":1746464365773,"traceId":"92f2d146f1781280"},{"name":"next-build","duration":54163,"timestamp":4516106398853,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464365720,"traceId":"92f2d146f1781280"}]
9
+ [{"name":"generate-buildid","duration":196,"timestamp":4516139403659,"id":4,"parentId":1,"tags":{},"startTime":1746464398725,"traceId":"a7a541a7bd9a53a0"},{"name":"load-custom-routes","duration":232,"timestamp":4516139403975,"id":5,"parentId":1,"tags":{},"startTime":1746464398725,"traceId":"a7a541a7bd9a53a0"},{"name":"next-build","duration":58401,"timestamp":4516139349232,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464398670,"traceId":"a7a541a7bd9a53a0"}]
10
+ [{"name":"generate-buildid","duration":103,"timestamp":4516187933744,"id":4,"parentId":1,"tags":{},"startTime":1746464447255,"traceId":"067afd570d8c005a"},{"name":"load-custom-routes","duration":177,"timestamp":4516187933896,"id":5,"parentId":1,"tags":{},"startTime":1746464447255,"traceId":"067afd570d8c005a"},{"name":"next-build","duration":56944,"timestamp":4516187878433,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464447199,"traceId":"067afd570d8c005a"}]
11
+ [{"name":"generate-buildid","duration":114,"timestamp":4516223927765,"id":4,"parentId":1,"tags":{},"startTime":1746464483249,"traceId":"8b53fbabb9360dbf"},{"name":"load-custom-routes","duration":166,"timestamp":4516223927936,"id":5,"parentId":1,"tags":{},"startTime":1746464483249,"traceId":"8b53fbabb9360dbf"},{"name":"next-build","duration":56140,"timestamp":4516223873187,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.0.3"},"startTime":1746464483194,"traceId":"8b53fbabb9360dbf"}]
@@ -0,0 +1,10 @@
1
+ {
2
+ "cSpell.words": [
3
+ "EJSON",
4
+ "Keyevent",
5
+ "keyspace",
6
+ "postboned",
7
+ "REDISHOST",
8
+ "REDISPORT"
9
+ ]
10
+ }
package/CHANGELOG.md CHANGED
@@ -1,3 +1,74 @@
1
+ # [1.4.0](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.3.0...v1.4.0) (2025-05-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add tests ([225fc49](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/225fc49eff26d631c4b8d50a15ef2c864213f36b))
7
+ * comment ([321ddec](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/321ddec4ee9b6a6e2cf1f78062d05d2cc7b45e4c))
8
+ * remove retry ([ca4ff6c](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/ca4ff6c4072e675da42dbcfe36d3ff422dc54f12))
9
+ * remove retry ([3a1cf7d](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/3a1cf7dc5fd315ff3c84460052af20b8e40014ec))
10
+ * remove retry ([207413b](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/207413b81cb51143e04dff9a2310a185d6bba568))
11
+ * tests and ci ([b0841ad](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/b0841ad9bc2c4b3ab9edde7047259cd45fbd5f02))
12
+
13
+
14
+ ### Features
15
+
16
+ * add support for nextjs 15.3.2 ([fcd8bb5](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/fcd8bb5469bbefcd88397d3ef86ce3eb0fee7c02))
17
+
18
+ # [1.3.0](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.2.1...v1.3.0) (2025-05-08)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * encoding of data in cache entry ([9f0e747](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/9f0e747b224294905427ba3060185f68fee16f0f))
24
+ * escape characte ([dbd9141](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/dbd91413b777b2a4554597f3ca2b7f1de1163a80))
25
+ * readme ([31dcaa4](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/31dcaa40ac19ea42a7f4d6ca33e4642704ad1f6b))
26
+ * readme ([dfb69c8](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/dfb69c861ddaa4a94411d419854e1c2e1e99152e))
27
+ * test ([13b2cb8](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/13b2cb8020ef7457473b208675bf3fce4161f492))
28
+ * tests ([02357e1](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/02357e1a7a29d997c95dce42399fef26530467d4))
29
+ * tests ([a5587dc](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/a5587dc3701a92069da945f548991aeb0d999285))
30
+ * tests + re-add old implementation ([cb6d36d](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/cb6d36d46008d422b4396fd580d35fa97848edb7))
31
+ * tests improvement ([73a3594](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/73a35946cfcd5e4811c2c09201bb02c9cdea5298))
32
+ * tests, make them independent from each other ([35885e3](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/35885e38a58ac0aa976c86dd964549e2d3426a79))
33
+
34
+
35
+ ### Features
36
+
37
+ * add files ([d8a2474](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/d8a24747a6a2a12e2709d017dd36c7b80b2ad49f))
38
+ * add more comments ([fb2f105](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/fb2f10588566fad042d2e8da4999cf7bf591555d))
39
+ * add new invalidation logic for fetch requests + new tests ([9d0d1d2](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/9d0d1d2eafb785dbe91b172358a19494c623cc68))
40
+ * add new tests ([836b882](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/836b88249365ea8745ca839a7dc3a3a4a77732e6))
41
+ * add new tests ([d0e6833](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/d0e68335ce13827f74ed1be5b115f7351beebd47))
42
+ * adding a test page ([d4c8a8c](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/d4c8a8caaba7f4e5003606860babd9e50c6ed99a))
43
+ * adding comfortable cache testing tool on homepage ([340d5cb](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/340d5cbc929ea8dcdbef2a949e1656d014d1f3bb))
44
+ * adding nextjs unit tests ([368ec75](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/368ec755a8532a52526e3364cc02f64b7d4245fa))
45
+ * changed ([8501a4e](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/8501a4ee963975f32661789e31281785cf01519a))
46
+ * changed hook ([92f9d3c](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/92f9d3c4b8d9afd6bbbb9cc3a69138823f4d4f87))
47
+ * changed hook ([6b32f0a](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/6b32f0abcda935046e61242ba633728bcf23a460))
48
+ * extend and fix caching + chore: update readme ([7ef38a8](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/7ef38a8ed2a20957cc78ffa213be3cb334736b19))
49
+ * extend tests ([db9bb85](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/db9bb85e398a7d6e1ba522b990de7e5e241b54b6))
50
+ * improve flaky ms sync delay ([6655884](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/665588474a09f1eb4f4683d2e107ec8b5b36b39a))
51
+ * improve readme ([926493a](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/926493a198d15e32e5e4b5c619375df85c646f1c))
52
+ * improve tests ([3fcfadf](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/3fcfadf49389fe8a7218417fb79ec74445e76cb7))
53
+ * remove unnecessary revalidatedTagsMap ([7b5c313](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/7b5c313e91b5157f113085be75dceba8013c0e81))
54
+ * test is running ([6091471](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/6091471a12f954e3da4bd94db0929b0bd2cfd701))
55
+ * tests ([bd6904e](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/bd6904e94a43cfac183a5ec86cbbea0f4e40a816))
56
+ * tests 2 ([974c952](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/974c952587a9d330934f49b26db84c037b3695f5))
57
+ * update CI with pnpm ([c1b0b33](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/c1b0b33417b416bd7755b6b2f43d0b9bea5c7690))
58
+ * update CI with pnpm ([d850377](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/d85037706876dcb036bb6f952a48937c3e96cba3))
59
+ * update CI with pnpm 10 ([c559936](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/c5599361f15c4c82fa999e16a48fcf78e53355a6))
60
+ * update CI with pnpm 11 ([f83708f](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/f83708f76c3924b8b80dd612965dc49a95d7e18c))
61
+ * update CI with pnpm 2 ([235f0fc](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/235f0fc8cb35dbe532091d8f545791b8dd05b6be))
62
+ * update CI with pnpm 2 ([cb9624e](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/cb9624e900f555bc07c734ff152c4f4a93000e54))
63
+ * update CI with pnpm 3 ([72c25ce](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/72c25cefa2aea6767d5a71b5470955a18f9036b9))
64
+ * update CI with pnpm 4 ([fff355a](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/fff355a8e047d766121d255881d891fa7c5a754e))
65
+ * update CI with pnpm 4 ([facd58b](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/facd58bdefd530d70fdbb2f7d48c9962ca2195e5))
66
+ * update CI with pnpm 5 ([03d9d90](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/03d9d906e852927496a58504dd9c7448e31878a1))
67
+ * update CI with pnpm 6 ([b5e7066](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/b5e7066ec840edfa7d6df6c6ab978c61b356c8b5))
68
+ * update CI with pnpm 8 ([20c3a5b](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/20c3a5be9d465537240cce6ac5346554667cda68))
69
+ * update CI with pnpm 9 ([5160fd3](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/5160fd3e1a010a5b52b99d0e2e0de3b9035d1e88))
70
+ * update docs and tests ([7a46c21](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/7a46c21346d878df88283071551f06a34d71eb9a))
71
+
1
72
  ## [1.2.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.2.0...v1.2.1) (2025-03-28)
2
73
 
3
74
 
package/README.md CHANGED
@@ -1,50 +1,130 @@
1
1
  # nextjs-turbo-redis-cache
2
2
 
3
- The ultimate Redis caching solution for Next.js. Built for production-ready, large-scale projects, it delivers unparalleled performance and efficiency with features tailored for high-traffic applications.
3
+ [![npm version](https://img.shields.io/npm/v/@trieb.work/nextjs-turbo-redis-cache.svg)](https://www.npmjs.com/package/@trieb.work/nextjs-turbo-redis-cache)
4
+
5
+ The ultimate Redis caching solution for Next.js. Built for production-ready, large-scale projects, it delivers unparalleled performance and efficiency with features tailored for high-traffic applications. This package has been created after extensibly testing the @neshca package and finding several major issues with it.
4
6
 
5
7
  Key Features:
6
8
 
7
- - _Turbocharged Storage_: Avoids base64 encoding to save ~33% of storage space compared to traditional implementations.
8
9
  - _Batch Tag Invalidation_: Groups and optimizes delete operations for minimal Redis stress.
9
10
  - _Request Deduplication_: Prevents redundant Redis get calls, ensuring faster response times.
10
- - _In-Memory Caching_: Includes local caching for Redis get operations to reduce latency further.
11
+ - _In-Memory Caching_: Includes local caching for Redis get operations to reduce latency further. Don't request redis for the same key multiple times.
11
12
  - _Efficient Tag Management_: in-memory tags map for lightning-fast revalidate operations with minimal Redis overhead.
12
13
  - _Intelligent Key-Space Notifications_: Automatic update of in-memory tags map for expired or evicted keys.
13
14
 
14
- ## Describe Options
15
+ ## Compatibility
16
+
17
+ This package is compatible with Next.js 15.0.3 and above while using App Router. It is not compatible with Next.js 14.x. or 15-canary or if you are using Pages Router.
18
+ Redis need to have Redis Version 2.8.0 or higher and have to be configured with `notify-keyspace-events` to be able to use the key-space notifications feature.
19
+
20
+ Tested versions are:
21
+
22
+ - 15.0.3
23
+ - 15.2.4
24
+ - 15.3.2
25
+ Currently PPR, 'use cache', cacheLife and cacheTag are not tested. Use these operations with caution and your own risk.
15
26
 
16
- TODO
27
+ ## Available Options (needs Option B of getting started)
28
+
29
+ | Option | Description | Default Value |
30
+ | ---------------------- | ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | --- | ----------------- |
31
+ | database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` |
32
+ | keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL | | 'UNDEFINED*URL*'` |
33
+ | sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` |
34
+ | timeoutMs | Timeout in milliseconds for Redis operations | `5000` |
35
+ | revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` |
36
+ | avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) |
37
+ | redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` |
38
+ | inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` |
39
+ | defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) |
40
+ | estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`<br>Other: `staleAge * 1.2` |
17
41
 
18
42
  ## Getting started
19
43
 
44
+ ### Enable redis key-space notifications for Expire and Evict events
45
+
46
+ ```bash
47
+ redis-cli -h localhost config set notify-keyspace-events Exe
48
+ ```
49
+
50
+ ### Install package
51
+
20
52
  ```bash
21
53
  pnpm install @trieb.work/nextjs-turbo-redis-cache
22
54
  ```
23
55
 
56
+ ### Setup environment variables in your project/deployment
57
+
58
+ REDISHOST and REDISPORT environment variables are required.
59
+ VERCEL_URL, VERCEL_ENV are optional. VERCEL_URL is used to create a key prefix for the redis keys. VERCEL_ENV is used to determine the database to use. Only VERCEL_ENV=production will show up in DB 0 (redis default db). All other values of VERCEL_ENV will use DB 1, use `redis-cli -n 1` to connect to different DB 1. This is another protection feature to avoid that different environments (e.g. staging and production) will overwrite each other.
60
+ Furthermore there exists the DEBUG_CACHE_HANDLER environment variable to enable debug logging of the caching handler once it is set to true.
61
+
62
+ ### Option A: minimum implementation with default options
63
+
64
+ extend `next.config.js` with:
65
+
66
+ ```
67
+ const nextConfig = {
68
+ ...
69
+ cacheHandler: require.resolve("@trieb.work/nextjs-turbo-redis-cache")
70
+ ...
71
+ }
72
+ ```
73
+
74
+ ### Option B: create a wrapper file to change options
75
+
76
+ create new file `customized-cache-handler.js` in your project root and add the following code:
77
+
78
+ ```
79
+ const { RedisStringsHandler } = require('@trieb.work/nextjs-turbo-redis-cache');
80
+
81
+ let cachedHandler;
82
+
83
+ module.exports = class CustomizedCacheHandler {
84
+ constructor() {
85
+ if (!cachedHandler) {
86
+ cachedHandler = new RedisStringsHandler({
87
+ database: 0,
88
+ keyPrefix: 'test',
89
+ timeoutMs: 2_000,
90
+ revalidateTagQuerySize: 500,
91
+ sharedTagsKey: '__sharedTags__',
92
+ avgResyncIntervalMs: 10_000 * 60,
93
+ redisGetDeduplication: false,
94
+ inMemoryCachingTime: 0,
95
+ defaultStaleAge: 1209600,
96
+ estimateExpireAge: (staleAge) => staleAge * 2,
97
+ });
98
+ }
99
+ }
100
+ get(...args) {
101
+ return cachedHandler.get(...args);
102
+ }
103
+ set(...args) {
104
+ return cachedHandler.set(...args);
105
+ }
106
+ revalidateTag(...args) {
107
+ return cachedHandler.revalidateTag(...args);
108
+ }
109
+ resetRequestCache(...args) {
110
+ return cachedHandler.resetRequestCache(...args);
111
+ }
112
+ }
113
+ ```
114
+
24
115
  extend `next.config.js` with:
116
+
25
117
  ```
26
118
  const nextConfig = {
27
- cacheHandler:
28
- process.env.NODE_ENV === "production" || process.env.DEV_REDIS_CACHE
29
- ? new CachedHandler({
30
- // Default Options:
31
- // maxMemoryCacheSize, // deprecated
32
- // database = process.env.VERCEL_ENV === 'production' ? 0 : 1,
33
- // keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',
34
- // sharedTagsKey = '__sharedTags__',
35
- // timeoutMs = 5000,
36
- // revalidateTagQuerySize = 250,
37
- // avgResyncIntervalMs = 60 * 60 * 1000,
38
- // redisGetDeduplication = true,
39
- // inMemoryCachingTime = 10_000,
40
- // defaultStaleAge = 60 * 60 * 24 * 14,
41
- // estimateExpireAge = (staleAge) =>
42
- // process.env.VERCEL_ENV === 'preview' ? staleAge * 1.2 : staleAge * 2,
43
- })
44
- : undefined,
119
+ ...
120
+ cacheHandler: require.resolve("./customized-cache-handler")
121
+ ...
122
+ }
45
123
  ```
46
124
 
47
- ## Consistency
125
+ A working example of above can be found in the `test/integration/next-app-customized` folder.
126
+
127
+ ## Consistency of Redis and this caching implementation
48
128
 
49
129
  To understand consistency levels of this caching implementation we first have to understand the consistency of redis itself:
50
130
  Redis executes commands in a single-threaded manner. This ensures that all operations are processed sequentially, so clients always see a consistent view of the data.
@@ -72,23 +152,63 @@ If using local in-memory caching (Enabled by RedisStringsHandler option inMemory
72
152
 
73
153
  Since all caching calls in one api/page/server action request is always served by the same instance this problem will not occur inside a single request but rather in a combination of multiple parallel requests. The probability that this will occur for a single user during a request sequence is very low, since typically a single user will not make the follow up request during this small time window of typically 50ms. To further mitigate the Problem and increase performance (increase local in-memory cache hit ratio) make sure that your load balancer will always serve one user to the same instance (sticky sessions).
74
154
 
155
+ By accepting and tolerating this eventual consistency, the performance of the caching handler is significantly increased.
156
+
75
157
  ## Development
76
158
 
77
- 5. Run `npm install` to install the dependencies
78
- 6. Run `npm run build` to build the project
79
- 7. Run `npm run dev` to develop the project
80
- 8. Run `npm run test` to test the project
81
- 9. Checkout into a new branch (main is protected)
82
- 10. Change code and commit it using conventional commit. Staged code will get checked
83
- 11. Push and create a PR (against main or beta) to run CI
84
- 12. Merge to main or beta to create a release or pre-release
159
+ 1. Run `pnpm install` to install the dependencies
160
+ 1. Run `pnpm build` to build the project
161
+ 1. Run `pnpm lint` to lint the project
162
+ 1. Run `pnpm format` to format the project
163
+ 1. Run `pnpm run-dev-server` to test and develop the caching handler using the nextjs integration test project
164
+ 1. If you make changes to the cache handler, you need to stop `pnpm run-dev-server` and run it again.
165
+
166
+ ## Testing
167
+
168
+ To run all tests you can use the following command:
169
+
170
+ ```bash
171
+ pnpm test
172
+ ```
173
+
174
+ ### Unit tests
175
+
176
+ To run unit tests you can use the following command:
177
+
178
+ ```bash
179
+ pnpm test:unit
180
+ ```
181
+
182
+ ### Integration tests
183
+
184
+ To run integration tests you can use the following command:
185
+
186
+ ```bash
187
+ pnpm test:integration
188
+ ```
189
+
190
+ The integration tests will start a Next.js server and test the caching handler. You can modify testing behavior by setting the following environment variables:
191
+
192
+ - SKIP_BUILD: If set to true, the integration tests will not build the Next.js app. Therefore the nextjs app needs to be built before running the tests. Or you execute the test once without skip build and the re-execute `pnpm test:integration` with skip build set to true.
193
+ - SKIP_OPTIONAL_LONG_RUNNER_TESTS: If set to true, the integration tests will not run the optional long runner tests.
194
+ - DEBUG_INTEGRATION: If set to true, the integration tests will print debug information of the test itself to the console.
195
+
196
+ Integration tests may have dependencies between test cases, so individual test failures should be evaluated in the context of the full test suite rather than in isolation.
197
+
198
+ ## Some words on nextjs caching internals
199
+
200
+ Nextjs will use different caching objects for different pages and api routes. Currently supported are kind: APP_ROUTE and APP_PAGE.
201
+
202
+ app/<segment>/route.ts files will request using the APP_ROUTE kind.
203
+ app/<segment>/page.tsx files will request using the APP_PAGE kind.
204
+ /favicon.ico file will request using the APP_ROUTE kind.
205
+
206
+ Fetch requests (inside app route or page) will request using the FETCH kind.
85
207
 
86
208
  ## License
87
209
 
88
210
  This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
89
211
 
90
-
91
212
  ## Sponsor
92
213
 
93
214
  This project is created and maintained by the Next.js & Payload CMS agency [trieb.work](https://trieb.work)
94
-
package/dist/index.d.mts CHANGED
@@ -1,21 +1,51 @@
1
- import { CacheHandler, IncrementalCache, CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
2
-
3
- type GetParams = Parameters<IncrementalCache['get']>;
4
- type SetParams = Parameters<IncrementalCache['set']>;
5
- type RevalidateParams = Parameters<IncrementalCache['revalidateTag']>;
1
+ type CacheEntry = {
2
+ value: unknown;
3
+ lastModified: number;
4
+ tags: string[];
5
+ };
6
6
  type CreateRedisStringsHandlerOptions = {
7
+ /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise
8
+ * @default process.env.VERCEL_ENV === 'production' ? 0 : 1
9
+ */
7
10
  database?: number;
11
+ /** Prefix added to all Redis keys
12
+ * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'
13
+ */
8
14
  keyPrefix?: string;
15
+ /** Timeout in milliseconds for Redis operations
16
+ * @default 5000
17
+ */
9
18
  timeoutMs?: number;
19
+ /** Number of entries to query in one batch during full sync of shared tags hash map
20
+ * @default 250
21
+ */
10
22
  revalidateTagQuerySize?: number;
23
+ /** Key used to store shared tags hash map in Redis
24
+ * @default '__sharedTags__'
25
+ */
11
26
  sharedTagsKey?: string;
27
+ /** Average interval in milliseconds between tag map full re-syncs
28
+ * @default 3600000 (1 hour)
29
+ */
12
30
  avgResyncIntervalMs?: number;
31
+ /** Enable deduplication of Redis get requests via internal in-memory cache
32
+ * @default true
33
+ */
13
34
  redisGetDeduplication?: boolean;
35
+ /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely
36
+ * @default 10000
37
+ */
14
38
  inMemoryCachingTime?: number;
39
+ /** Default stale age in seconds for cached items
40
+ * @default 1209600 (14 days)
41
+ */
15
42
  defaultStaleAge?: number;
43
+ /** Function to calculate expire age (redis TTL value) from stale age
44
+ * @default Production: staleAge * 2, Other: staleAge * 1.2
45
+ */
16
46
  estimateExpireAge?: (staleAge: number) => number;
17
47
  };
18
- declare class RedisStringsHandler implements CacheHandler {
48
+ declare class RedisStringsHandler {
19
49
  private client;
20
50
  private sharedTagsMap;
21
51
  private revalidatedTagsMap;
@@ -30,23 +60,69 @@ declare class RedisStringsHandler implements CacheHandler {
30
60
  private defaultStaleAge;
31
61
  private estimateExpireAge;
32
62
  constructor({ database, keyPrefix, sharedTagsKey, timeoutMs, revalidateTagQuerySize, avgResyncIntervalMs, redisGetDeduplication, inMemoryCachingTime, defaultStaleAge, estimateExpireAge, }: CreateRedisStringsHandlerOptions);
33
- resetRequestCache(...args: never[]): void;
63
+ resetRequestCache(): void;
34
64
  private assertClientIsReady;
35
- get(key: GetParams[0], ctx: GetParams[1]): Promise<(CacheHandlerValue & {
36
- lastModified: number;
37
- }) | null>;
38
- set(key: SetParams[0], data: SetParams[1] & {
39
- lastModified: number;
40
- }, ctx: SetParams[2]): Promise<void>;
41
- revalidateTag(tagOrTags: RevalidateParams[0]): Promise<void>;
65
+ get(key: string, ctx: {
66
+ kind: 'APP_ROUTE' | 'APP_PAGE';
67
+ isRoutePPREnabled: boolean;
68
+ isFallback: boolean;
69
+ } | {
70
+ kind: 'FETCH';
71
+ revalidate: number;
72
+ fetchUrl: string;
73
+ fetchIdx: number;
74
+ tags: string[];
75
+ softTags: string[];
76
+ isFallback: boolean;
77
+ }): Promise<CacheEntry | null>;
78
+ set(key: string, data: {
79
+ kind: 'APP_PAGE';
80
+ status: number;
81
+ headers: {
82
+ 'x-nextjs-stale-time': string;
83
+ 'x-next-cache-tags': string;
84
+ };
85
+ html: string;
86
+ rscData: Buffer;
87
+ segmentData: unknown;
88
+ postboned: unknown;
89
+ } | {
90
+ kind: 'APP_ROUTE';
91
+ status: number;
92
+ headers: {
93
+ 'cache-control'?: string;
94
+ 'x-nextjs-stale-time': string;
95
+ 'x-next-cache-tags': string;
96
+ };
97
+ body: Buffer;
98
+ } | {
99
+ kind: 'FETCH';
100
+ data: {
101
+ headers: Record<string, string>;
102
+ body: string;
103
+ status: number;
104
+ url: string;
105
+ };
106
+ revalidate: number | false;
107
+ }, ctx: {
108
+ isRoutePPREnabled: boolean;
109
+ isFallback: boolean;
110
+ tags?: string[];
111
+ revalidate?: number | false;
112
+ cacheControl?: {
113
+ revalidate: 5;
114
+ expire: undefined;
115
+ };
116
+ }): Promise<void>;
117
+ revalidateTag(tagOrTags: string | string[], ...rest: any[]): Promise<void>;
42
118
  }
43
119
 
44
- declare class CachedHandler implements CacheHandler {
120
+ declare class CachedHandler {
45
121
  constructor(options: CreateRedisStringsHandlerOptions);
46
- get(...args: Parameters<RedisStringsHandler["get"]>): ReturnType<RedisStringsHandler["get"]>;
47
- set(...args: Parameters<RedisStringsHandler["set"]>): ReturnType<RedisStringsHandler["set"]>;
48
- revalidateTag(...args: Parameters<RedisStringsHandler["revalidateTag"]>): ReturnType<RedisStringsHandler["revalidateTag"]>;
49
- resetRequestCache(...args: Parameters<RedisStringsHandler["resetRequestCache"]>): ReturnType<RedisStringsHandler["resetRequestCache"]>;
122
+ get(...args: Parameters<RedisStringsHandler['get']>): ReturnType<RedisStringsHandler['get']>;
123
+ set(...args: Parameters<RedisStringsHandler['set']>): ReturnType<RedisStringsHandler['set']>;
124
+ revalidateTag(...args: Parameters<RedisStringsHandler['revalidateTag']>): ReturnType<RedisStringsHandler['revalidateTag']>;
125
+ resetRequestCache(...args: Parameters<RedisStringsHandler['resetRequestCache']>): ReturnType<RedisStringsHandler['resetRequestCache']>;
50
126
  }
51
127
 
52
- export { CachedHandler as default };
128
+ export { RedisStringsHandler, CachedHandler as default };