@trieb.work/nextjs-turbo-redis-cache 1.17.0 → 1.18.1

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 (300) hide show
  1. package/README.md +87 -24
  2. package/dist/index.d.mts +40 -1
  3. package/dist/index.d.ts +40 -1
  4. package/dist/index.js +291 -116
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +288 -115
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +9 -6
  9. package/.cursor/skills/testing/SKILL.md +0 -121
  10. package/.github/workflows/ci.yml +0 -309
  11. package/.github/workflows/pages.yml +0 -38
  12. package/.github/workflows/release.yml +0 -207
  13. package/.prettierrc.json +0 -6
  14. package/.vscode/settings.json +0 -10
  15. package/ARCHITECTURE.md +0 -431
  16. package/CHANGELOG.md +0 -411
  17. package/commitlint.config.cjs +0 -4
  18. package/docker/redis.conf +0 -1
  19. package/docs/index.html +0 -140
  20. package/docs/robots.txt +0 -4
  21. package/docs/sitemap.xml +0 -8
  22. package/docs/styles.css +0 -141
  23. package/eslint.config.mjs +0 -26
  24. package/hooks/commit-msg +0 -10
  25. package/hooks/pre-commit +0 -11
  26. package/playwright.config.ts +0 -18
  27. package/release.config.cjs +0 -31
  28. package/scripts/prepare.sh +0 -7
  29. package/scripts/setup-git.sh +0 -8
  30. package/scripts/setup-repo-name.sh +0 -17
  31. package/scripts/vitest-run-staged.cjs +0 -44
  32. package/src/CacheComponentsHandler.ts +0 -567
  33. package/src/CachedHandler.ts +0 -53
  34. package/src/DeduplicatedRequestHandler.ts +0 -110
  35. package/src/RedisStringsHandler.ts +0 -937
  36. package/src/SyncedMap.ts +0 -482
  37. package/src/ZodHandler.ts +0 -45
  38. package/src/index.ts +0 -16
  39. package/src/serializer.ts +0 -59
  40. package/src/utils/debug.ts +0 -30
  41. package/src/utils/json.ts +0 -49
  42. package/src/utils/prefix.ts +0 -44
  43. package/test/README.md +0 -194
  44. package/test/nextjs-test-projects/next-app-15-4-11/README.md +0 -36
  45. package/test/nextjs-test-projects/next-app-15-4-11/eslint.config.mjs +0 -16
  46. package/test/nextjs-test-projects/next-app-15-4-11/next.config.js +0 -9
  47. package/test/nextjs-test-projects/next-app-15-4-11/package-lock.json +0 -5969
  48. package/test/nextjs-test-projects/next-app-15-4-11/package.json +0 -29
  49. package/test/nextjs-test-projects/next-app-15-4-11/pnpm-lock.yaml +0 -3726
  50. package/test/nextjs-test-projects/next-app-15-4-11/postcss.config.mjs +0 -5
  51. package/test/nextjs-test-projects/next-app-15-4-11/public/file.svg +0 -1
  52. package/test/nextjs-test-projects/next-app-15-4-11/public/globe.svg +0 -1
  53. package/test/nextjs-test-projects/next-app-15-4-11/public/next.svg +0 -1
  54. package/test/nextjs-test-projects/next-app-15-4-11/public/vercel.svg +0 -1
  55. package/test/nextjs-test-projects/next-app-15-4-11/public/window.svg +0 -1
  56. package/test/nextjs-test-projects/next-app-15-4-11/src/app/api/cached-static-fetch/route.ts +0 -18
  57. package/test/nextjs-test-projects/next-app-15-4-11/src/app/api/nested-fetch-in-api-route/revalidated-fetch/route.ts +0 -27
  58. package/test/nextjs-test-projects/next-app-15-4-11/src/app/api/revalidatePath/route.ts +0 -15
  59. package/test/nextjs-test-projects/next-app-15-4-11/src/app/api/revalidateTag/route.ts +0 -15
  60. package/test/nextjs-test-projects/next-app-15-4-11/src/app/api/revalidated-fetch/route.ts +0 -17
  61. package/test/nextjs-test-projects/next-app-15-4-11/src/app/api/uncached-fetch/route.ts +0 -15
  62. package/test/nextjs-test-projects/next-app-15-4-11/src/app/globals.css +0 -26
  63. package/test/nextjs-test-projects/next-app-15-4-11/src/app/layout.tsx +0 -59
  64. package/test/nextjs-test-projects/next-app-15-4-11/src/app/page.tsx +0 -755
  65. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/cached-static-fetch/default--force-dynamic-page/page.tsx +0 -19
  66. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/cached-static-fetch/revalidate15--default-page/page.tsx +0 -34
  67. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/cached-static-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  68. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/no-fetch/default-page/page.tsx +0 -55
  69. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/revalidated-fetch/default--force-dynamic-page/page.tsx +0 -19
  70. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/revalidated-fetch/revalidate15--default-page/page.tsx +0 -35
  71. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/revalidated-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  72. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/uncached-fetch/default--force-dynamic-page/page.tsx +0 -19
  73. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/uncached-fetch/revalidate15--default-page/page.tsx +0 -32
  74. package/test/nextjs-test-projects/next-app-15-4-11/src/app/pages/uncached-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  75. package/test/nextjs-test-projects/next-app-15-4-11/src/app/revalidation-interface.tsx +0 -267
  76. package/test/nextjs-test-projects/next-app-15-4-11/tsconfig.json +0 -27
  77. package/test/nextjs-test-projects/next-app-16-0-11/README.md +0 -36
  78. package/test/nextjs-test-projects/next-app-16-0-11/eslint.config.mjs +0 -18
  79. package/test/nextjs-test-projects/next-app-16-0-11/next.config.ts +0 -10
  80. package/test/nextjs-test-projects/next-app-16-0-11/package.json +0 -28
  81. package/test/nextjs-test-projects/next-app-16-0-11/pnpm-lock.yaml +0 -4163
  82. package/test/nextjs-test-projects/next-app-16-0-11/postcss.config.mjs +0 -7
  83. package/test/nextjs-test-projects/next-app-16-0-11/src/app/api/cached-static-fetch/route.ts +0 -18
  84. package/test/nextjs-test-projects/next-app-16-0-11/src/app/api/nested-fetch-in-api-route/revalidated-fetch/route.ts +0 -27
  85. package/test/nextjs-test-projects/next-app-16-0-11/src/app/api/revalidatePath/route.ts +0 -15
  86. package/test/nextjs-test-projects/next-app-16-0-11/src/app/api/revalidateTag/route.ts +0 -20
  87. package/test/nextjs-test-projects/next-app-16-0-11/src/app/api/revalidated-fetch/route.ts +0 -17
  88. package/test/nextjs-test-projects/next-app-16-0-11/src/app/api/uncached-fetch/route.ts +0 -15
  89. package/test/nextjs-test-projects/next-app-16-0-11/src/app/favicon.ico +0 -0
  90. package/test/nextjs-test-projects/next-app-16-0-11/src/app/globals.css +0 -26
  91. package/test/nextjs-test-projects/next-app-16-0-11/src/app/layout.tsx +0 -59
  92. package/test/nextjs-test-projects/next-app-16-0-11/src/app/page.tsx +0 -755
  93. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/cached-static-fetch/default--force-dynamic-page/page.tsx +0 -19
  94. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/cached-static-fetch/revalidate15--default-page/page.tsx +0 -34
  95. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/cached-static-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  96. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/no-fetch/default-page/page.tsx +0 -55
  97. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/revalidated-fetch/default--force-dynamic-page/page.tsx +0 -19
  98. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/revalidated-fetch/revalidate15--default-page/page.tsx +0 -35
  99. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/revalidated-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  100. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/uncached-fetch/default--force-dynamic-page/page.tsx +0 -19
  101. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/uncached-fetch/revalidate15--default-page/page.tsx +0 -32
  102. package/test/nextjs-test-projects/next-app-16-0-11/src/app/pages/uncached-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  103. package/test/nextjs-test-projects/next-app-16-0-11/src/app/revalidation-interface.tsx +0 -267
  104. package/test/nextjs-test-projects/next-app-16-0-11/src/app/update-tag-test/page.tsx +0 -25
  105. package/test/nextjs-test-projects/next-app-16-0-11/tsconfig.json +0 -34
  106. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/README.md +0 -36
  107. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/cache-handler.js +0 -3
  108. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/eslint.config.mjs +0 -18
  109. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/next.config.ts +0 -13
  110. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/package.json +0 -28
  111. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/pnpm-lock.yaml +0 -4206
  112. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/postcss.config.mjs +0 -7
  113. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/public/file.svg +0 -1
  114. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/public/globe.svg +0 -1
  115. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/public/next.svg +0 -1
  116. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/public/vercel.svg +0 -1
  117. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/public/window.svg +0 -1
  118. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/cached-static-fetch/route.ts +0 -19
  119. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/cached-with-cachelife/route.ts +0 -24
  120. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/cached-with-tag/route.ts +0 -21
  121. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/revalidate-tag/route.ts +0 -19
  122. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/revalidated-fetch/route.ts +0 -19
  123. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/cachelife-short/page.tsx +0 -110
  124. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/page.tsx +0 -90
  125. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/runtime-data-suspense/page.tsx +0 -127
  126. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/stale-while-revalidate/page.tsx +0 -130
  127. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/tag-invalidation/page.tsx +0 -127
  128. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/use-cache-nondeterministic/page.tsx +0 -110
  129. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/favicon.ico +0 -0
  130. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/globals.css +0 -26
  131. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/layout.tsx +0 -57
  132. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/page.tsx +0 -755
  133. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/revalidation-interface.tsx +0 -267
  134. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/update-tag-test/page.tsx +0 -22
  135. package/test/nextjs-test-projects/next-app-16-0-11-cache-components/tsconfig.json +0 -34
  136. package/test/nextjs-test-projects/next-app-16-2-6/README.md +0 -36
  137. package/test/nextjs-test-projects/next-app-16-2-6/eslint.config.mjs +0 -18
  138. package/test/nextjs-test-projects/next-app-16-2-6/next.config.ts +0 -7
  139. package/test/nextjs-test-projects/next-app-16-2-6/package.json +0 -28
  140. package/test/nextjs-test-projects/next-app-16-2-6/pnpm-lock.yaml +0 -4165
  141. package/test/nextjs-test-projects/next-app-16-2-6/postcss.config.mjs +0 -7
  142. package/test/nextjs-test-projects/next-app-16-2-6/src/app/api/cached-static-fetch/route.ts +0 -18
  143. package/test/nextjs-test-projects/next-app-16-2-6/src/app/api/nested-fetch-in-api-route/revalidated-fetch/route.ts +0 -27
  144. package/test/nextjs-test-projects/next-app-16-2-6/src/app/api/revalidatePath/route.ts +0 -15
  145. package/test/nextjs-test-projects/next-app-16-2-6/src/app/api/revalidateTag/route.ts +0 -20
  146. package/test/nextjs-test-projects/next-app-16-2-6/src/app/api/revalidated-fetch/route.ts +0 -17
  147. package/test/nextjs-test-projects/next-app-16-2-6/src/app/api/uncached-fetch/route.ts +0 -15
  148. package/test/nextjs-test-projects/next-app-16-2-6/src/app/favicon.ico +0 -0
  149. package/test/nextjs-test-projects/next-app-16-2-6/src/app/globals.css +0 -26
  150. package/test/nextjs-test-projects/next-app-16-2-6/src/app/layout.tsx +0 -59
  151. package/test/nextjs-test-projects/next-app-16-2-6/src/app/page.tsx +0 -755
  152. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/cached-static-fetch/default--force-dynamic-page/page.tsx +0 -19
  153. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/cached-static-fetch/revalidate15--default-page/page.tsx +0 -34
  154. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/cached-static-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  155. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/no-fetch/default-page/page.tsx +0 -55
  156. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/revalidated-fetch/default--force-dynamic-page/page.tsx +0 -19
  157. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/revalidated-fetch/revalidate15--default-page/page.tsx +0 -35
  158. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/revalidated-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  159. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/uncached-fetch/default--force-dynamic-page/page.tsx +0 -19
  160. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/uncached-fetch/revalidate15--default-page/page.tsx +0 -32
  161. package/test/nextjs-test-projects/next-app-16-2-6/src/app/pages/uncached-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  162. package/test/nextjs-test-projects/next-app-16-2-6/src/app/revalidation-interface.tsx +0 -267
  163. package/test/nextjs-test-projects/next-app-16-2-6/src/app/update-tag-test/page.tsx +0 -25
  164. package/test/nextjs-test-projects/next-app-16-2-6/tsconfig.json +0 -34
  165. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/README.md +0 -36
  166. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/cache-handler.js +0 -3
  167. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/eslint.config.mjs +0 -18
  168. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/next.config.ts +0 -13
  169. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/package.json +0 -28
  170. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/pnpm-lock.yaml +0 -4165
  171. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/postcss.config.mjs +0 -7
  172. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/public/file.svg +0 -1
  173. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/public/globe.svg +0 -1
  174. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/public/next.svg +0 -1
  175. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/public/vercel.svg +0 -1
  176. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/public/window.svg +0 -1
  177. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/cached-static-fetch/route.ts +0 -19
  178. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/cached-with-cachelife/route.ts +0 -24
  179. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/cached-with-tag/route.ts +0 -21
  180. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/revalidate-tag/route.ts +0 -19
  181. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/revalidated-fetch/route.ts +0 -19
  182. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/cachelife-short/page.tsx +0 -110
  183. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/page.tsx +0 -90
  184. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/runtime-data-suspense/page.tsx +0 -127
  185. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/stale-while-revalidate/page.tsx +0 -130
  186. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/tag-invalidation/page.tsx +0 -127
  187. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/use-cache-nondeterministic/page.tsx +0 -110
  188. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/favicon.ico +0 -0
  189. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/globals.css +0 -26
  190. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/layout.tsx +0 -57
  191. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/page.tsx +0 -755
  192. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/revalidation-interface.tsx +0 -267
  193. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/update-tag-test/page.tsx +0 -22
  194. package/test/nextjs-test-projects/next-app-16-2-6-cache-components/tsconfig.json +0 -34
  195. package/test/nextjs-test-projects/next-app-16-3-0/README.md +0 -36
  196. package/test/nextjs-test-projects/next-app-16-3-0/eslint.config.mjs +0 -18
  197. package/test/nextjs-test-projects/next-app-16-3-0/next.config.ts +0 -7
  198. package/test/nextjs-test-projects/next-app-16-3-0/package.json +0 -28
  199. package/test/nextjs-test-projects/next-app-16-3-0/pnpm-lock.yaml +0 -4284
  200. package/test/nextjs-test-projects/next-app-16-3-0/postcss.config.mjs +0 -7
  201. package/test/nextjs-test-projects/next-app-16-3-0/src/app/api/cached-static-fetch/route.ts +0 -18
  202. package/test/nextjs-test-projects/next-app-16-3-0/src/app/api/nested-fetch-in-api-route/revalidated-fetch/route.ts +0 -27
  203. package/test/nextjs-test-projects/next-app-16-3-0/src/app/api/revalidatePath/route.ts +0 -15
  204. package/test/nextjs-test-projects/next-app-16-3-0/src/app/api/revalidateTag/route.ts +0 -20
  205. package/test/nextjs-test-projects/next-app-16-3-0/src/app/api/revalidated-fetch/route.ts +0 -17
  206. package/test/nextjs-test-projects/next-app-16-3-0/src/app/api/uncached-fetch/route.ts +0 -15
  207. package/test/nextjs-test-projects/next-app-16-3-0/src/app/favicon.ico +0 -0
  208. package/test/nextjs-test-projects/next-app-16-3-0/src/app/globals.css +0 -26
  209. package/test/nextjs-test-projects/next-app-16-3-0/src/app/layout.tsx +0 -59
  210. package/test/nextjs-test-projects/next-app-16-3-0/src/app/page.tsx +0 -755
  211. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/cached-static-fetch/default--force-dynamic-page/page.tsx +0 -19
  212. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/cached-static-fetch/revalidate15--default-page/page.tsx +0 -34
  213. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/cached-static-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  214. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/no-fetch/default-page/page.tsx +0 -55
  215. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/revalidated-fetch/default--force-dynamic-page/page.tsx +0 -19
  216. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/revalidated-fetch/revalidate15--default-page/page.tsx +0 -35
  217. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/revalidated-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  218. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/uncached-fetch/default--force-dynamic-page/page.tsx +0 -19
  219. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/uncached-fetch/revalidate15--default-page/page.tsx +0 -32
  220. package/test/nextjs-test-projects/next-app-16-3-0/src/app/pages/uncached-fetch/revalidate15--force-dynamic-page/page.tsx +0 -25
  221. package/test/nextjs-test-projects/next-app-16-3-0/src/app/revalidation-interface.tsx +0 -267
  222. package/test/nextjs-test-projects/next-app-16-3-0/src/app/update-tag-test/page.tsx +0 -25
  223. package/test/nextjs-test-projects/next-app-16-3-0/tsconfig.json +0 -34
  224. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/README.md +0 -36
  225. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/cache-handler.js +0 -3
  226. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/eslint.config.mjs +0 -18
  227. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/next.config.ts +0 -13
  228. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/package.json +0 -28
  229. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/pnpm-lock.yaml +0 -4284
  230. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/postcss.config.mjs +0 -7
  231. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/public/file.svg +0 -1
  232. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/public/globe.svg +0 -1
  233. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/public/next.svg +0 -1
  234. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/public/vercel.svg +0 -1
  235. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/public/window.svg +0 -1
  236. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/cached-static-fetch/route.ts +0 -19
  237. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/cached-with-cachelife/route.ts +0 -24
  238. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/cached-with-tag/route.ts +0 -21
  239. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/revalidate-tag/route.ts +0 -19
  240. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/revalidated-fetch/route.ts +0 -19
  241. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/cachelife-short/page.tsx +0 -110
  242. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/page.tsx +0 -90
  243. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/runtime-data-suspense/page.tsx +0 -127
  244. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/stale-while-revalidate/page.tsx +0 -130
  245. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/tag-invalidation/page.tsx +0 -127
  246. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/use-cache-nondeterministic/page.tsx +0 -110
  247. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/favicon.ico +0 -0
  248. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/globals.css +0 -26
  249. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/layout.tsx +0 -57
  250. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/page.tsx +0 -755
  251. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/revalidation-interface.tsx +0 -267
  252. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/update-tag-test/page.tsx +0 -22
  253. package/test/nextjs-test-projects/next-app-16-3-0-cache-components/tsconfig.json +0 -34
  254. package/test/nextjs-test-projects/next-app-customized/README.md +0 -36
  255. package/test/nextjs-test-projects/next-app-customized/customized-cache-handler.js +0 -34
  256. package/test/nextjs-test-projects/next-app-customized/eslint.config.mjs +0 -16
  257. package/test/nextjs-test-projects/next-app-customized/next.config.js +0 -6
  258. package/test/nextjs-test-projects/next-pages-16-2-6/README.md +0 -16
  259. package/test/nextjs-test-projects/next-pages-16-2-6/eslint.config.mjs +0 -18
  260. package/test/nextjs-test-projects/next-pages-16-2-6/next.config.ts +0 -7
  261. package/test/nextjs-test-projects/next-pages-16-2-6/package.json +0 -26
  262. package/test/nextjs-test-projects/next-pages-16-2-6/pnpm-lock.yaml +0 -3896
  263. package/test/nextjs-test-projects/next-pages-16-2-6/src/pages/api/revalidate.ts +0 -24
  264. package/test/nextjs-test-projects/next-pages-16-2-6/src/pages/index.tsx +0 -11
  265. package/test/nextjs-test-projects/next-pages-16-2-6/src/pages/isr/[slug].tsx +0 -49
  266. package/test/nextjs-test-projects/next-pages-16-2-6/src/pages/static-forever.tsx +0 -20
  267. package/test/nextjs-test-projects/next-pages-16-2-6/tsconfig.json +0 -34
  268. package/test/nextjs-test-projects/next-pages-16-3-0/README.md +0 -16
  269. package/test/nextjs-test-projects/next-pages-16-3-0/eslint.config.mjs +0 -18
  270. package/test/nextjs-test-projects/next-pages-16-3-0/next.config.ts +0 -7
  271. package/test/nextjs-test-projects/next-pages-16-3-0/package.json +0 -26
  272. package/test/nextjs-test-projects/next-pages-16-3-0/pnpm-lock.yaml +0 -3939
  273. package/test/nextjs-test-projects/next-pages-16-3-0/src/pages/api/revalidate.ts +0 -24
  274. package/test/nextjs-test-projects/next-pages-16-3-0/src/pages/index.tsx +0 -11
  275. package/test/nextjs-test-projects/next-pages-16-3-0/src/pages/isr/[slug].tsx +0 -49
  276. package/test/nextjs-test-projects/next-pages-16-3-0/src/pages/static-forever.tsx +0 -20
  277. package/test/nextjs-test-projects/next-pages-16-3-0/tsconfig.json +0 -34
  278. package/test/playwright/cache-lab.spec.ts +0 -157
  279. package/test/playwright/update-tag.spec.ts +0 -33
  280. package/test/vitest/integration/build-id-prefix.integration.test.ts +0 -102
  281. package/test/vitest/integration/cache-components/cache-components.integration.test.ts +0 -231
  282. package/test/vitest/integration/cache-components/redis-kill-reconnect.test.ts +0 -65
  283. package/test/vitest/integration/cache-components/redis-quit-vs-disconnect.test.ts +0 -59
  284. package/test/vitest/integration/cache-components/redis-subscriber-outage.test.ts +0 -100
  285. package/test/vitest/integration/cache-components/scripts/redis-kill-reconnect.ts +0 -243
  286. package/test/vitest/integration/cache-components/scripts/redis-quit-vs-disconnect.ts +0 -113
  287. package/test/vitest/integration/cache-components/scripts/redis-subscriber-outage.ts +0 -379
  288. package/test/vitest/integration/cache-components/scripts/redis-test-helpers.ts +0 -155
  289. package/test/vitest/integration/nextjs-cache-handler.integration.test.ts +0 -945
  290. package/test/vitest/integration/pages-router.integration.test.ts +0 -420
  291. package/test/vitest/unit/SyncedMap-reconnect-repro.test.ts +0 -154
  292. package/test/vitest/unit/index.test.ts +0 -173
  293. package/test/vitest/unit/pages-router-kinds.test.ts +0 -292
  294. package/test/vitest/unit/reconnect-socket-already-opened.test.ts +0 -117
  295. package/test/vitest/unit/serializer.test.ts +0 -182
  296. package/test/vitest/unit/utils/prefix.test.ts +0 -115
  297. package/tsconfig.json +0 -18
  298. package/tsup.config.ts +0 -14
  299. package/vite.config.ts +0 -34
  300. package/vitest.cache-components.config.ts +0 -16
@@ -1,937 +0,0 @@
1
- import { commandOptions, createClient, RedisClientOptions } from 'redis';
2
- import { SyncedMap } from './SyncedMap';
3
- import { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';
4
- import { debug } from './utils/debug';
5
- import { CacheValueSerializer, jsonCacheValueSerializer } from './serializer';
6
-
7
- export type CommandOptions = ReturnType<typeof commandOptions>;
8
- export type Client = ReturnType<typeof createClient>;
9
-
10
- export type CacheEntry = {
11
- value: unknown;
12
- lastModified: number;
13
- tags: string[];
14
- };
15
-
16
- /** Discriminated union of the `ctx` argument Next.js passes to
17
- * {@link RedisStringsHandler.get}. The Pages Router (`PAGES`) has no PPR
18
- * concept, so `isRoutePPREnabled` is optional for it.
19
- *
20
- * Note: `REDIRECT` is intentionally absent. Next.js always calls `get()` with
21
- * `kind: 'PAGES'` for Pages Router routes, even when the stored value has
22
- * `kind: 'REDIRECT'`. The handler returns whatever value is cached and Next.js
23
- * interprets it accordingly. */
24
- export type GetContext =
25
- | {
26
- kind: 'APP_ROUTE' | 'APP_PAGE';
27
- isRoutePPREnabled: boolean;
28
- isFallback: boolean;
29
- }
30
- | {
31
- kind: 'PAGES';
32
- isRoutePPREnabled?: boolean;
33
- isFallback: boolean;
34
- }
35
- | {
36
- kind: 'FETCH';
37
- revalidate: number;
38
- fetchUrl: string;
39
- fetchIdx: number;
40
- tags: string[];
41
- softTags: string[];
42
- isFallback: boolean;
43
- };
44
-
45
- /** Discriminated union of cache payloads accepted by
46
- * {@link RedisStringsHandler.set}. `null` is a valid payload: the Pages Router
47
- * stores `notFound: true` results as a cache entry with a null value. */
48
- export type SetCacheValue =
49
- | {
50
- kind: 'APP_PAGE';
51
- status?: number;
52
- headers: {
53
- 'x-nextjs-stale-time': string; // timestamp in ms
54
- 'x-next-cache-tags': string; // comma separated paths (tags)
55
- };
56
- html: string;
57
- rscData: Buffer;
58
- segmentData: unknown;
59
- postboned: unknown;
60
- }
61
- | {
62
- kind: 'APP_ROUTE';
63
- status: number;
64
- headers: {
65
- 'cache-control'?: string;
66
- 'x-nextjs-stale-time': string; // timestamp in ms
67
- 'x-next-cache-tags': string; // comma separated paths (tags)
68
- };
69
- body: Buffer;
70
- }
71
- | {
72
- kind: 'PAGES';
73
- html: string;
74
- pageData: Record<string, unknown>;
75
- headers?: Record<string, number | string | string[] | undefined>;
76
- status?: number;
77
- }
78
- | {
79
- kind: 'REDIRECT';
80
- props: Record<string, unknown>;
81
- }
82
- | {
83
- kind: 'FETCH';
84
- data: {
85
- headers: Record<string, string>;
86
- body: string; // base64 encoded
87
- status: number;
88
- url: string;
89
- };
90
- revalidate: number | false;
91
- }
92
- | null;
93
-
94
- export function redisErrorHandler<T extends Promise<unknown>>(
95
- debugInfo: string,
96
- redisCommandResult: T,
97
- ): T {
98
- const beforeTimestamp = performance.now();
99
- return redisCommandResult.catch((error) => {
100
- console.error(
101
- 'Redis command error',
102
- (performance.now() - beforeTimestamp).toFixed(2),
103
- 'ms',
104
- debugInfo,
105
- error,
106
- );
107
- throw error;
108
- }) as T;
109
- }
110
-
111
- function isAbortError(error: unknown): boolean {
112
- if (!error || typeof error !== 'object') return false;
113
- const err = error as { name?: unknown; code?: unknown };
114
- return err.name === 'AbortError' || err.code === 'ABORT_ERR';
115
- }
116
-
117
- if (process.env.DEBUG_CACHE_HANDLER) {
118
- // This is a test to check if the event loop is lagging. If it lags, increase CPU of container
119
- setInterval(() => {
120
- const start = performance.now();
121
- setImmediate(() => {
122
- const duration = performance.now() - start;
123
- if (duration > 100) {
124
- debug(
125
- 'yellow',
126
- `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms. If your container is hosted in a cloud provider with container suspension this is normal. If not you should increase the CPU of your container.`,
127
- );
128
- }
129
- });
130
- }, 500);
131
- }
132
-
133
- export type CreateRedisStringsHandlerOptions = {
134
- /** Redis redisUrl to use.
135
- * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
136
- ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
137
- : 'redis://localhost:6379'
138
- */
139
- redisUrl?: string;
140
- /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise
141
- * @default process.env.VERCEL_ENV === 'production' ? 0 : 1
142
- */
143
- database?: number;
144
- /** Prefix added to all Redis keys
145
- * @default process.env.KEY_PREFIX || process.env.VERCEL_URL || 'UNDEFINED_URL_'
146
- */
147
- keyPrefix?: string;
148
- /** Timeout in milliseconds for time critical Redis operations (during cache get, which blocks site rendering).
149
- * If redis get is not fulfilled within this time, the cache handler will return null so site rendering will
150
- * not be blocked further and site can fallback to re-render/re-fetch the content.
151
- * @default 500
152
- */
153
- getTimeoutMs?: number;
154
- /** Number of entries to query in one batch during full sync of shared tags hash map
155
- * @default 250
156
- */
157
- revalidateTagQuerySize?: number;
158
- /** Key used to store shared tags hash map in Redis
159
- * @default '__sharedTags__'
160
- */
161
- sharedTagsKey?: string;
162
- /** Average interval in milliseconds between tag map full re-syncs
163
- * @default 3600000 (1 hour)
164
- */
165
- avgResyncIntervalMs?: number;
166
- /** Enable deduplication of Redis get requests via internal in-memory cache
167
- * @default true
168
- */
169
- redisGetDeduplication?: boolean;
170
- /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely
171
- * @default 10000
172
- */
173
- inMemoryCachingTime?: number;
174
- /** Default stale age in seconds for cached items
175
- * @default 1209600 (14 days)
176
- */
177
- defaultStaleAge?: number;
178
- /** Function to calculate expire age (redis TTL value) from stale age
179
- * @default Production: staleAge * 2, Other: staleAge * 1.2
180
- */
181
- estimateExpireAge?: (staleAge: number) => number;
182
- /** Kill container on Redis client error if error threshold is reached
183
- * @default 0 (0 means no error threshold)
184
- */
185
- killContainerOnErrorThreshold?: number;
186
- /** Additional Redis client socket options
187
- * @example { tls: true, rejectUnauthorized: false }
188
- */
189
- socketOptions?: RedisClientOptions['socket'];
190
- /** Additional Redis client options to be passed directly to createClient
191
- * @example { username: 'user', password: 'pass' }
192
- */
193
- clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;
194
- /** Pluggable wire-format codec for Redis string values. Lets you plug in
195
- * compression (gzip/brotli), encryption, or any other custom encoding without
196
- * forking this package or losing the existing dedup / batch / keyspace features.
197
- *
198
- * Both `serialize` and `deserialize` may return a `Promise`, enabling
199
- * non-blocking async codecs (e.g. `zlib.brotliCompress`) that don't block the
200
- * Node.js event loop. Synchronous implementations continue to work unchanged.
201
- *
202
- * Only the main cache-entry storage path is routed through the serializer.
203
- * The shared-tags map and the revalidated-tags map are not. The in-memory
204
- * deduplication cache stores the wire-format string verbatim - its contents
205
- * change with the serializer, but the cache itself is not re-encoded.
206
- *
207
- * Operational note: changing the serializer (or any of its parameters such as
208
- * a compression level or encryption key) makes existing Redis keys unreadable.
209
- * Either flush the affected keys or bump `keyPrefix` before deploying.
210
- *
211
- * @default jsonCacheValueSerializer (JSON.stringify with bufferAndMapReplacer
212
- * so native Buffer and Map values inside a CacheEntry round-trip transparently)
213
- */
214
- valueSerializer?: CacheValueSerializer;
215
- };
216
-
217
- // Identifier prefix used by Next.js to mark automatically generated cache tags
218
- // These tags are created internally by Next.js for route-based invalidation
219
- const NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';
220
-
221
- // Redis key used to store a map of tags and their last revalidation timestamps
222
- // This helps track when specific tags were last invalidated
223
- const REVALIDATED_TAGS_KEY = '__revalidated_tags__';
224
-
225
- // Cache kinds this handler is designed and tested for. Kept as the single
226
- // source of truth for both payload validation and the warning message, so
227
- // adding a kind is a one-line change here instead of edits scattered across
228
- // the type union, the guard chain, and the log string.
229
- const SUPPORTED_GET_KINDS = [
230
- 'APP_ROUTE',
231
- 'APP_PAGE',
232
- 'PAGES',
233
- 'FETCH',
234
- ] as const;
235
- const SUPPORTED_SET_KINDS = [
236
- 'APP_ROUTE',
237
- 'APP_PAGE',
238
- 'PAGES',
239
- 'REDIRECT',
240
- 'FETCH',
241
- ] as const;
242
-
243
- let killContainerOnErrorCount: number = 0;
244
- export default class RedisStringsHandler {
245
- private client: Client;
246
- private sharedTagsMap: SyncedMap<string[]>;
247
- private revalidatedTagsMap: SyncedMap<number>;
248
- private inMemoryDeduplicationCache: SyncedMap<
249
- Promise<ReturnType<Client['get']>>
250
- >;
251
- private getTimeoutMs: number;
252
- private redisGet: Client['get'];
253
- private redisDeduplicationHandler: DeduplicatedRequestHandler<
254
- Client['get'],
255
- string | Buffer | null
256
- >;
257
- private deduplicatedRedisGet: (key: string) => Client['get'];
258
- private keyPrefix: string;
259
- private redisGetDeduplication: boolean;
260
- private inMemoryCachingTime: number;
261
- private defaultStaleAge: number;
262
- private estimateExpireAge: (staleAge: number) => number;
263
- private killContainerOnErrorThreshold: number;
264
- private valueSerializer: CacheValueSerializer;
265
-
266
- constructor({
267
- redisUrl = process.env.REDIS_URL
268
- ? process.env.REDIS_URL
269
- : process.env.REDISHOST
270
- ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
271
- : 'redis://localhost:6379',
272
- database = process.env.VERCEL_ENV === 'production' ? 0 : 1,
273
- keyPrefix = process.env.KEY_PREFIX ||
274
- process.env.VERCEL_URL ||
275
- 'UNDEFINED_URL_',
276
- sharedTagsKey = '__sharedTags__',
277
- getTimeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS
278
- ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 500)
279
- : 500,
280
- revalidateTagQuerySize = 250,
281
- avgResyncIntervalMs = 60 * 60 * 1_000,
282
- redisGetDeduplication = true,
283
- inMemoryCachingTime = 10_000,
284
- defaultStaleAge = 60 * 60 * 24 * 14,
285
- estimateExpireAge = (staleAge) =>
286
- process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,
287
- killContainerOnErrorThreshold = process.env
288
- .KILL_CONTAINER_ON_ERROR_THRESHOLD
289
- ? (Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0)
290
- : 0,
291
- socketOptions,
292
- clientOptions,
293
- valueSerializer = jsonCacheValueSerializer,
294
- }: CreateRedisStringsHandlerOptions) {
295
- try {
296
- this.keyPrefix = keyPrefix;
297
- this.redisGetDeduplication = redisGetDeduplication;
298
- this.inMemoryCachingTime = inMemoryCachingTime;
299
- this.defaultStaleAge = defaultStaleAge;
300
- this.estimateExpireAge = estimateExpireAge;
301
- this.killContainerOnErrorThreshold = killContainerOnErrorThreshold;
302
- this.getTimeoutMs = getTimeoutMs;
303
- this.valueSerializer = valueSerializer;
304
-
305
- try {
306
- // Create Redis client with properly typed configuration
307
- this.client = createClient({
308
- url: redisUrl,
309
- pingInterval: 10_000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long.
310
- ...(database !== 0 ? { database } : {}),
311
- ...(socketOptions ? { socket: { ...socketOptions } } : {}),
312
- ...(clientOptions || {}),
313
- });
314
-
315
- this.client.on('error', (error) => {
316
- console.error(
317
- 'Redis client error',
318
- error,
319
- killContainerOnErrorCount++,
320
- );
321
- setTimeout(() => {
322
- // Avoid overlapping connect() calls which can lead to
323
- // "Socket already opened" errors when a reconnect is already
324
- // in progress or the socket is still open.
325
- if (!this.client.isOpen && !this.client.isReady) {
326
- this.client.connect().catch((error) => {
327
- console.error(
328
- 'Failed to reconnect Redis client after connection loss:',
329
- error,
330
- );
331
- });
332
- }
333
- }, 1000);
334
- if (
335
- this.killContainerOnErrorThreshold > 0 &&
336
- killContainerOnErrorCount >= this.killContainerOnErrorThreshold
337
- ) {
338
- console.error(
339
- 'Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',
340
- error,
341
- killContainerOnErrorCount++,
342
- );
343
- this.client.disconnect();
344
- this.client.quit();
345
- setTimeout(() => {
346
- process.exit(1);
347
- }, 500);
348
- }
349
- });
350
-
351
- this.client
352
- .connect()
353
- .then(() => {
354
- debug('green', 'Redis client connected.');
355
- })
356
- .catch((error) => {
357
- console.error('Failed to connect Redis client:', error);
358
- throw error;
359
- });
360
- } catch (error: unknown) {
361
- console.error('Failed to initialize Redis client');
362
- throw error;
363
- }
364
-
365
- const filterKeys = (key: string): boolean =>
366
- key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;
367
-
368
- this.sharedTagsMap = new SyncedMap<string[]>({
369
- client: this.client,
370
- keyPrefix,
371
- redisKey: sharedTagsKey,
372
- database,
373
- querySize: revalidateTagQuerySize,
374
- filterKeys,
375
- resyncIntervalMs:
376
- avgResyncIntervalMs -
377
- avgResyncIntervalMs / 10 +
378
- Math.random() * (avgResyncIntervalMs / 10),
379
- });
380
-
381
- this.revalidatedTagsMap = new SyncedMap<number>({
382
- client: this.client,
383
- keyPrefix,
384
- redisKey: REVALIDATED_TAGS_KEY,
385
- database,
386
- querySize: revalidateTagQuerySize,
387
- filterKeys,
388
- resyncIntervalMs:
389
- avgResyncIntervalMs +
390
- avgResyncIntervalMs / 10 +
391
- Math.random() * (avgResyncIntervalMs / 10),
392
- });
393
-
394
- this.inMemoryDeduplicationCache = new SyncedMap({
395
- client: this.client,
396
- keyPrefix,
397
- redisKey: 'inMemoryDeduplicationCache',
398
- database,
399
- querySize: revalidateTagQuerySize,
400
- filterKeys,
401
- customizedSync: {
402
- withoutRedisHashmap: true,
403
- withoutSetSync: true,
404
- },
405
- });
406
-
407
- const redisGet: Client['get'] = this.client.get.bind(this.client);
408
- this.redisDeduplicationHandler = new DeduplicatedRequestHandler(
409
- redisGet,
410
- inMemoryCachingTime,
411
- this.inMemoryDeduplicationCache,
412
- );
413
- this.redisGet = redisGet;
414
- this.deduplicatedRedisGet =
415
- this.redisDeduplicationHandler.deduplicatedFunction;
416
- } catch (error) {
417
- console.error(
418
- 'RedisStringsHandler constructor error',
419
- error,
420
- killContainerOnErrorCount++,
421
- );
422
- if (
423
- killContainerOnErrorThreshold > 0 &&
424
- killContainerOnErrorCount >= killContainerOnErrorThreshold
425
- ) {
426
- console.error(
427
- 'RedisStringsHandler constructor error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',
428
- error,
429
- killContainerOnErrorCount++,
430
- );
431
- process.exit(1);
432
- }
433
- throw error;
434
- }
435
- }
436
-
437
- resetRequestCache(): void {}
438
-
439
- private clientReadyCalls = 0;
440
-
441
- private async assertClientIsReady(): Promise<void> {
442
- if (this.clientReadyCalls > 10) {
443
- throw new Error(
444
- 'assertClientIsReady called more than 10 times without being ready.',
445
- );
446
- }
447
- let readyTimeout: ReturnType<typeof setTimeout> | undefined;
448
- try {
449
- await Promise.race([
450
- Promise.all([
451
- this.sharedTagsMap.waitUntilReady(),
452
- this.revalidatedTagsMap.waitUntilReady(),
453
- ]),
454
- new Promise((_, reject) => {
455
- readyTimeout = setTimeout(() => {
456
- reject(
457
- new Error(
458
- 'assertClientIsReady: Timeout waiting for Redis maps to be ready',
459
- ),
460
- );
461
- }, 30_000);
462
- }),
463
- ]);
464
- } finally {
465
- // Always clear the timeout: once the race is won (the common case after
466
- // the initial sync), the timer would otherwise stay pending for the full
467
- // 30s. Timers capture the ambient async context (AsyncLocalStorage),
468
- // so in a Next.js server every leaked timer pins the request's store —
469
- // and with it the whole response object graph — for at least 30s on
470
- // every single cache operation.
471
- clearTimeout(readyTimeout);
472
- }
473
- this.clientReadyCalls = 0;
474
- if (!this.client.isReady) {
475
- throw new Error(
476
- 'assertClientIsReady: Redis client is not ready yet or connection is lost.',
477
- );
478
- }
479
- }
480
-
481
- public async get(key: string, ctx: GetContext): Promise<CacheEntry | null> {
482
- try {
483
- if (!(SUPPORTED_GET_KINDS as readonly string[]).includes(ctx.kind)) {
484
- console.warn(
485
- 'RedisStringsHandler.get() called with',
486
- key,
487
- ctx,
488
- `this cache handler is only designed and tested for kinds ${SUPPORTED_GET_KINDS.join(', ')} and not for kind`,
489
- (ctx as { kind: string })?.kind,
490
- );
491
- }
492
-
493
- debug('green', 'RedisStringsHandler.get() called with', key, ctx);
494
- await this.assertClientIsReady();
495
-
496
- const clientGet = this.redisGetDeduplication
497
- ? this.deduplicatedRedisGet(key)
498
- : this.redisGet;
499
-
500
- const redisGetOperation = clientGet(
501
- commandOptions({ signal: AbortSignal.timeout(this.getTimeoutMs) }),
502
- this.keyPrefix + key,
503
- ).catch((error) => {
504
- if (isAbortError(error)) {
505
- return null;
506
- }
507
- throw error;
508
- });
509
- const serializedCacheEntry = await redisErrorHandler(
510
- 'RedisStringsHandler.get(), operation: get' +
511
- (this.redisGetDeduplication ? 'deduplicated' : '') +
512
- ' ' +
513
- this.getTimeoutMs +
514
- 'ms ' +
515
- this.keyPrefix +
516
- ' ' +
517
- key,
518
- redisGetOperation,
519
- );
520
-
521
- debug(
522
- 'green',
523
- 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',
524
- serializedCacheEntry?.substring(0, 200),
525
- );
526
-
527
- if (!serializedCacheEntry) {
528
- return null;
529
- }
530
-
531
- let cacheEntry: CacheEntry | null;
532
- try {
533
- cacheEntry =
534
- await this.valueSerializer.deserialize(serializedCacheEntry);
535
- } catch (err) {
536
- // A decode failure (e.g. legacy/corrupted entry after swapping codecs,
537
- // bumping a compression level or rotating an encryption key) is treated
538
- // as a cache miss rather than a hard error - this matches the
539
- // null-from-deserialize semantics in the CacheValueSerializer contract
540
- // and prevents a single unreadable entry from incrementing
541
- // killContainerOnErrorCount on every read.
542
- console.warn(
543
- 'RedisStringsHandler.get() valueSerializer.deserialize failed, treating as cache miss',
544
- this.keyPrefix + key,
545
- err,
546
- );
547
- return null;
548
- }
549
-
550
- debug(
551
- 'green',
552
- 'RedisStringsHandler.get() finished with result (cacheEntry)',
553
- JSON.stringify(cacheEntry).substring(0, 200),
554
- );
555
-
556
- if (!cacheEntry) {
557
- return null;
558
- }
559
-
560
- if (!cacheEntry?.tags) {
561
- console.warn(
562
- 'RedisStringsHandler.get() called with',
563
- key,
564
- ctx,
565
- 'cacheEntry is mall formed (missing tags)',
566
- );
567
- }
568
- // value === null is a legitimate entry: the Pages Router stores
569
- // `notFound: true` results as a cache entry with a null value.
570
- // Only an absent value indicates a malformed entry.
571
- if (cacheEntry?.value === undefined) {
572
- console.warn(
573
- 'RedisStringsHandler.get() called with',
574
- key,
575
- ctx,
576
- 'cacheEntry is mall formed (missing value)',
577
- );
578
- }
579
- if (!cacheEntry?.lastModified) {
580
- console.warn(
581
- 'RedisStringsHandler.get() called with',
582
- key,
583
- ctx,
584
- 'cacheEntry is mall formed (missing lastModified)',
585
- );
586
- }
587
-
588
- if (ctx.kind === 'FETCH') {
589
- const combinedTags = new Set([
590
- ...(ctx?.softTags || []),
591
- ...(ctx?.tags || []),
592
- ]);
593
-
594
- if (combinedTags.size === 0) {
595
- return cacheEntry;
596
- }
597
-
598
- // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information
599
- //
600
- // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)
601
- // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's
602
- // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.
603
- for (const tag of combinedTags) {
604
- // Get the last revalidation time for this tag from our revalidatedTagsMap
605
- const revalidationTime = this.revalidatedTagsMap.get(tag);
606
-
607
- // If we have a revalidation time for this tag and it's more recent than when
608
- // this cache entry was last modified, the entry is stale
609
- if (revalidationTime && revalidationTime > cacheEntry.lastModified) {
610
- const redisKey = this.keyPrefix + key;
611
-
612
- // We don't await this cleanup since it can happen asynchronously in the background.
613
- // The cache entry is already considered invalid at this point.
614
- this.client
615
- .unlink(redisKey)
616
- .catch((err) => {
617
- // Log error but don't retry deletion since the cache entry will likely be
618
- // updated immediately after via set(). A retry could dangerously execute
619
- // after the new value is set.
620
- console.error(
621
- 'Error occurred while unlinking stale data. Error was:',
622
- err,
623
- );
624
- })
625
- .finally(async () => {
626
- // Clean up our tag tracking maps after the Redis key is removed
627
- await this.sharedTagsMap.delete(key);
628
- await this.revalidatedTagsMap.delete(tag);
629
- });
630
-
631
- debug(
632
- 'green',
633
- 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and "null" will be returned.',
634
- tag,
635
- redisKey,
636
- revalidationTime,
637
- cacheEntry,
638
- );
639
-
640
- // Return null to indicate no valid cache entry was found
641
- return null;
642
- }
643
- }
644
- }
645
-
646
- return cacheEntry;
647
- } catch (error) {
648
- // This catch block is necessary to handle any errors that may occur during:
649
- // 1. Redis operations (get, unlink)
650
- // 2. Tag validation and cleanup
651
- // (Deserialization errors are handled locally above and treated as cache misses,
652
- // so they do not reach this branch and do not count toward the kill threshold.)
653
- // If any error occurs, we return null to indicate no valid cache entry was found,
654
- // allowing the application to regenerate the content rather than crash
655
- console.error(
656
- 'RedisStringsHandler.get() Error occurred while getting cache entry. Returning null so site can continue to serve content while cache is disabled. The original error was:',
657
- error,
658
- killContainerOnErrorCount++,
659
- );
660
-
661
- if (
662
- this.killContainerOnErrorThreshold > 0 &&
663
- killContainerOnErrorCount >= this.killContainerOnErrorThreshold
664
- ) {
665
- console.error(
666
- 'RedisStringsHandler get() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',
667
- error,
668
- killContainerOnErrorCount,
669
- );
670
- this.client.disconnect();
671
- this.client.quit();
672
- setTimeout(() => {
673
- process.exit(1);
674
- }, 500);
675
- }
676
- return null;
677
- }
678
- }
679
- public async set(
680
- key: string,
681
- data: SetCacheValue,
682
- ctx: {
683
- isRoutePPREnabled: boolean;
684
- isFallback: boolean;
685
- tags?: string[];
686
- // Different versions of Next.js use different arguments for the same functionality
687
- revalidate?: number | false; // Version 15.0.3
688
- cacheControl?: { revalidate: number | false; expire: number | undefined }; // Version 15.0.3+
689
- },
690
- ) {
691
- try {
692
- if (
693
- data !== null &&
694
- !(SUPPORTED_SET_KINDS as readonly string[]).includes(data.kind)
695
- ) {
696
- console.warn(
697
- 'RedisStringsHandler.set() called with',
698
- key,
699
- ctx,
700
- data,
701
- `this cache handler is only designed and tested for kinds ${SUPPORTED_SET_KINDS.join(', ')} and not for kind`,
702
- (data as { kind: string })?.kind,
703
- );
704
- }
705
-
706
- await this.assertClientIsReady();
707
-
708
- if (data?.kind === 'APP_PAGE' || data?.kind === 'APP_ROUTE') {
709
- const tags = data.headers['x-next-cache-tags']?.split(',');
710
- ctx.tags = [...(ctx.tags || []), ...(tags || [])];
711
- }
712
-
713
- // Pages Router entries (PAGES, REDIRECT and null/notFound results) do not
714
- // carry an x-next-cache-tags header. Attach the implicit path tag
715
- // (_N_T_/<path>) so that revalidatePath()/revalidateTag('_N_T_/<path>')
716
- // invalidates them the same way as App Router entries.
717
- if (data === null || data.kind === 'PAGES' || data.kind === 'REDIRECT') {
718
- const implicitTag = NEXT_CACHE_IMPLICIT_TAG_ID + key;
719
- if (!ctx.tags?.includes(implicitTag)) {
720
- ctx.tags = [...(ctx.tags || []), implicitTag];
721
- }
722
- }
723
-
724
- // Constructing and serializing the value for storing it in redis
725
- const cacheEntry: CacheEntry = {
726
- lastModified: Date.now(),
727
- tags: ctx?.tags || [],
728
- value: data,
729
- };
730
- const serializedCacheEntry =
731
- await this.valueSerializer.serialize(cacheEntry);
732
-
733
- // pre seed data into deduplicated get client. This will reduce redis load by not requesting
734
- // the same value from redis which was just set.
735
- if (this.redisGetDeduplication) {
736
- this.redisDeduplicationHandler.seedRequestReturn(
737
- key,
738
- serializedCacheEntry,
739
- );
740
- }
741
-
742
- // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and "use cache" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife
743
- // Constructing the expire time for the cache entry
744
- const revalidate =
745
- // For fetch requests in newest versions, the revalidate context property is never used, and instead the revalidate property of the passed-in data is used
746
- (data?.kind === 'FETCH' && data.revalidate) ||
747
- ctx.revalidate ||
748
- ctx.cacheControl?.revalidate ||
749
- // Legacy fallback: older Next.js versions attached `revalidate` directly
750
- // to the data payload. FETCH is already handled above, so this only
751
- // matters for those older, non-discriminated shapes.
752
- (data as { revalidate?: number | false } | null)?.revalidate;
753
- const expireAt =
754
- revalidate && Number.isSafeInteger(revalidate) && revalidate > 0
755
- ? this.estimateExpireAge(revalidate)
756
- : this.estimateExpireAge(this.defaultStaleAge);
757
-
758
- // Setting the cache entry in redis
759
- const setOperation: Promise<string | null> = redisErrorHandler(
760
- 'RedisStringsHandler.set(), operation: set ' +
761
- this.keyPrefix +
762
- ' ' +
763
- key,
764
- this.client.set(this.keyPrefix + key, serializedCacheEntry, {
765
- EX: expireAt,
766
- }),
767
- );
768
-
769
- debug(
770
- 'blue',
771
- 'RedisStringsHandler.set() will set the following serializedCacheEntry',
772
- this.keyPrefix,
773
- key,
774
- data,
775
- ctx,
776
- serializedCacheEntry?.substring(0, 200),
777
- expireAt,
778
- );
779
-
780
- // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)
781
- let setTagsOperation: Promise<void> | undefined;
782
- if (ctx.tags && ctx.tags.length > 0) {
783
- const currentTags = this.sharedTagsMap.get(key);
784
- const currentIsSameAsNew =
785
- currentTags?.length === ctx.tags.length &&
786
- currentTags.every((v) => ctx.tags!.includes(v)) &&
787
- ctx.tags.every((v) => currentTags.includes(v));
788
-
789
- if (!currentIsSameAsNew) {
790
- setTagsOperation = this.sharedTagsMap.set(
791
- key,
792
- structuredClone(ctx.tags) as string[],
793
- );
794
- }
795
- }
796
-
797
- debug(
798
- 'blue',
799
- 'RedisStringsHandler.set() will set the following sharedTagsMap',
800
- key,
801
- ctx.tags as string[],
802
- );
803
-
804
- await Promise.all([setOperation, setTagsOperation]);
805
- } catch (error) {
806
- console.error(
807
- 'RedisStringsHandler.set() Error occurred while setting cache entry. The original error was:',
808
- error,
809
- killContainerOnErrorCount++,
810
- );
811
- if (
812
- this.killContainerOnErrorThreshold > 0 &&
813
- killContainerOnErrorCount >= this.killContainerOnErrorThreshold
814
- ) {
815
- console.error(
816
- 'RedisStringsHandler set() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',
817
- error,
818
- killContainerOnErrorCount,
819
- );
820
- this.client.disconnect();
821
- this.client.quit();
822
- setTimeout(() => {
823
- process.exit(1);
824
- }, 500);
825
- }
826
- throw error;
827
- }
828
- }
829
-
830
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
831
- public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {
832
- try {
833
- debug(
834
- 'red',
835
- 'RedisStringsHandler.revalidateTag() called with',
836
- tagOrTags,
837
- rest,
838
- );
839
- const tags = new Set([tagOrTags || []].flat());
840
- await this.assertClientIsReady();
841
-
842
- // find all keys that are related to this tag
843
- const keysToDelete: Set<string> = new Set();
844
-
845
- for (const tag of tags) {
846
- // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)
847
- //
848
- // Invalidation logic for fetch requests that are related to a invalidated page.
849
- // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well
850
- // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request
851
- // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside
852
- // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)
853
- // and delete the cache entry of the fetch request on the next request to the get function
854
- if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {
855
- const now = Date.now();
856
- debug(
857
- 'red',
858
- 'RedisStringsHandler.revalidateTag() set revalidation time for tag',
859
- tag,
860
- 'to',
861
- now,
862
- );
863
- await this.revalidatedTagsMap.set(tag, now);
864
- }
865
- }
866
-
867
- // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags
868
- for (const [key, sharedTags] of this.sharedTagsMap.entries()) {
869
- if (sharedTags.some((tag) => tags.has(tag))) {
870
- keysToDelete.add(key);
871
- }
872
- }
873
-
874
- debug(
875
- 'red',
876
- 'RedisStringsHandler.revalidateTag() found',
877
- keysToDelete,
878
- 'keys to delete',
879
- );
880
-
881
- // exit early if no keys are related to this tag
882
- if (keysToDelete.size === 0) {
883
- return;
884
- }
885
-
886
- // prepare deletion of all keys in redis that are related to this tag
887
- const redisKeys = Array.from(keysToDelete);
888
- const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);
889
- const deleteKeysOperation = redisErrorHandler(
890
- 'RedisStringsHandler.revalidateTag(), operation: unlink ' +
891
- this.keyPrefix +
892
- ' ' +
893
- fullRedisKeys,
894
- this.client.unlink(fullRedisKeys),
895
- );
896
-
897
- // also delete entries from in-memory deduplication cache if they get revalidated
898
- if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {
899
- for (const key of keysToDelete) {
900
- this.inMemoryDeduplicationCache.delete(key);
901
- }
902
- }
903
-
904
- // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely
905
- const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);
906
-
907
- // execute keys and tag maps deletion
908
- await Promise.all([deleteKeysOperation, deleteTagsOperation]);
909
- debug(
910
- 'red',
911
- 'RedisStringsHandler.revalidateTag() finished delete operations',
912
- );
913
- } catch (error) {
914
- console.error(
915
- 'RedisStringsHandler.revalidateTag() Error occurred while revalidating tags. The original error was:',
916
- error,
917
- killContainerOnErrorCount++,
918
- );
919
- if (
920
- this.killContainerOnErrorThreshold > 0 &&
921
- killContainerOnErrorCount >= this.killContainerOnErrorThreshold
922
- ) {
923
- console.error(
924
- 'RedisStringsHandler revalidateTag() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',
925
- error,
926
- killContainerOnErrorCount,
927
- );
928
- this.client.disconnect();
929
- this.client.quit();
930
- setTimeout(() => {
931
- process.exit(1);
932
- }, 500);
933
- }
934
- throw error;
935
- }
936
- }
937
- }