@trieb.work/nextjs-turbo-redis-cache 1.10.0-beta.14 → 1.11.0-beta.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 (55) hide show
  1. package/.github/workflows/ci.yml +28 -12
  2. package/CHANGELOG.md +6 -94
  3. package/README.md +94 -0
  4. package/dist/index.d.mts +22 -1
  5. package/dist/index.d.ts +22 -1
  6. package/dist/index.js +333 -15
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +330 -14
  9. package/dist/index.mjs.map +1 -1
  10. package/package.json +3 -2
  11. package/playwright.config.ts +8 -1
  12. package/src/CacheComponentsHandler.ts +471 -0
  13. package/src/RedisStringsHandler.ts +11 -11
  14. package/src/index.test.ts +1 -1
  15. package/src/index.ts +5 -0
  16. package/test/cache-components/cache-components.integration.spec.ts +188 -0
  17. package/test/integration/next-app-15-4-7/next.config.js +3 -0
  18. package/test/integration/next-app-15-4-7/pnpm-lock.yaml +1 -1
  19. package/test/integration/next-app-16-0-3/next.config.ts +3 -0
  20. package/test/integration/next-app-16-1-1-cache-components/README.md +36 -0
  21. package/test/integration/next-app-16-1-1-cache-components/cache-handler.js +3 -0
  22. package/test/integration/next-app-16-1-1-cache-components/eslint.config.mjs +18 -0
  23. package/test/integration/next-app-16-1-1-cache-components/next.config.ts +13 -0
  24. package/test/integration/next-app-16-1-1-cache-components/package.json +28 -0
  25. package/test/integration/next-app-16-1-1-cache-components/pnpm-lock.yaml +4128 -0
  26. package/test/integration/next-app-16-1-1-cache-components/postcss.config.mjs +7 -0
  27. package/test/integration/next-app-16-1-1-cache-components/public/file.svg +1 -0
  28. package/test/integration/next-app-16-1-1-cache-components/public/globe.svg +1 -0
  29. package/test/integration/next-app-16-1-1-cache-components/public/next.svg +1 -0
  30. package/test/integration/next-app-16-1-1-cache-components/public/public/file.svg +1 -0
  31. package/test/integration/next-app-16-1-1-cache-components/public/public/globe.svg +1 -0
  32. package/test/integration/next-app-16-1-1-cache-components/public/public/next.svg +1 -0
  33. package/test/integration/next-app-16-1-1-cache-components/public/public/vercel.svg +1 -0
  34. package/test/integration/next-app-16-1-1-cache-components/public/public/window.svg +1 -0
  35. package/test/integration/next-app-16-1-1-cache-components/public/vercel.svg +1 -0
  36. package/test/integration/next-app-16-1-1-cache-components/public/window.svg +1 -0
  37. package/test/integration/next-app-16-1-1-cache-components/src/app/api/cached-static-fetch/route.ts +19 -0
  38. package/test/integration/next-app-16-1-1-cache-components/src/app/api/cached-with-tag/route.ts +21 -0
  39. package/test/integration/next-app-16-1-1-cache-components/src/app/api/revalidate-tag/route.ts +19 -0
  40. package/test/integration/next-app-16-1-1-cache-components/src/app/api/revalidated-fetch/route.ts +19 -0
  41. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/cachelife-short/page.tsx +110 -0
  42. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/page.tsx +90 -0
  43. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/runtime-data-suspense/page.tsx +127 -0
  44. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/stale-while-revalidate/page.tsx +130 -0
  45. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/tag-invalidation/page.tsx +127 -0
  46. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/use-cache-nondeterministic/page.tsx +110 -0
  47. package/test/integration/next-app-16-1-1-cache-components/src/app/favicon.ico +0 -0
  48. package/test/integration/next-app-16-1-1-cache-components/src/app/globals.css +26 -0
  49. package/test/integration/next-app-16-1-1-cache-components/src/app/layout.tsx +57 -0
  50. package/test/integration/next-app-16-1-1-cache-components/src/app/page.tsx +755 -0
  51. package/test/integration/next-app-16-1-1-cache-components/src/app/revalidation-interface.tsx +267 -0
  52. package/test/integration/next-app-16-1-1-cache-components/src/app/update-tag-test/page.tsx +22 -0
  53. package/test/integration/next-app-16-1-1-cache-components/tsconfig.json +34 -0
  54. package/tests/cache-lab.spec.ts +157 -0
  55. package/vitest.cache-components.config.ts +16 -0
package/dist/index.mjs CHANGED
@@ -512,16 +512,15 @@ var RedisStringsHandler = class {
512
512
  error,
513
513
  killContainerOnErrorCount++
514
514
  );
515
- setTimeout(() => {
516
- if (!this.client.isOpen && !this.client.isReady) {
517
- this.client.connect().catch((error2) => {
518
- console.error(
519
- "Failed to reconnect Redis client after connection loss:",
520
- error2
521
- );
522
- });
523
- }
524
- }, 1e3);
515
+ setTimeout(
516
+ () => this.client.connect().catch((error2) => {
517
+ console.error(
518
+ "Failed to reconnect Redis client after connection loss:",
519
+ error2
520
+ );
521
+ }),
522
+ 1e3
523
+ );
525
524
  if (this.killContainerOnErrorThreshold > 0 && killContainerOnErrorCount >= this.killContainerOnErrorThreshold) {
526
525
  console.error(
527
526
  "Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)",
@@ -537,9 +536,12 @@ var RedisStringsHandler = class {
537
536
  });
538
537
  this.client.connect().then(() => {
539
538
  debug("green", "Redis client connected.");
540
- }).catch((error) => {
541
- console.error("Failed to connect Redis client:", error);
542
- throw error;
539
+ }).catch(() => {
540
+ this.client.connect().catch((error) => {
541
+ console.error("Failed to connect Redis client:", error);
542
+ this.client.disconnect();
543
+ throw error;
544
+ });
543
545
  });
544
546
  } catch (error) {
545
547
  console.error("Failed to initialize Redis client");
@@ -948,10 +950,324 @@ var CachedHandler = class {
948
950
  }
949
951
  };
950
952
 
953
+ // src/CacheComponentsHandler.ts
954
+ import { commandOptions as commandOptions2, createClient as createClient2 } from "redis";
955
+ var REVALIDATED_TAGS_KEY2 = "__cacheComponents_revalidated_tags__";
956
+ var SHARED_TAGS_KEY = "__cacheComponents_sharedTags__";
957
+ var killContainerOnErrorCount2 = 0;
958
+ async function streamToBuffer(stream) {
959
+ const reader = stream.getReader();
960
+ const chunks = [];
961
+ while (true) {
962
+ const { value, done } = await reader.read();
963
+ if (done) break;
964
+ if (value) {
965
+ chunks.push(value);
966
+ }
967
+ }
968
+ if (chunks.length === 1) {
969
+ return chunks[0];
970
+ }
971
+ const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
972
+ const result = new Uint8Array(totalLength);
973
+ let offset = 0;
974
+ for (const chunk of chunks) {
975
+ result.set(chunk, offset);
976
+ offset += chunk.byteLength;
977
+ }
978
+ return result;
979
+ }
980
+ function bufferToReadableStream(buffer) {
981
+ return new ReadableStream({
982
+ start(controller) {
983
+ controller.enqueue(buffer);
984
+ controller.close();
985
+ }
986
+ });
987
+ }
988
+ var RedisCacheComponentsHandler = class {
989
+ constructor({
990
+ redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : "redis://localhost:6379",
991
+ database = process.env.VERCEL_ENV === "production" ? 0 : 1,
992
+ keyPrefix = process.env.VERCEL_URL || "UNDEFINED_URL_",
993
+ getTimeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS ? Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 500 : 500,
994
+ revalidateTagQuerySize = 250,
995
+ avgResyncIntervalMs = 60 * 60 * 1e3,
996
+ socketOptions,
997
+ clientOptions,
998
+ killContainerOnErrorThreshold = process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD ? Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0 : 0
999
+ }) {
1000
+ try {
1001
+ this.keyPrefix = keyPrefix;
1002
+ this.getTimeoutMs = getTimeoutMs;
1003
+ this.client = createClient2({
1004
+ url: redisUrl,
1005
+ pingInterval: 1e4,
1006
+ ...database !== 0 ? { database } : {},
1007
+ ...socketOptions ? { socket: { ...socketOptions } } : {},
1008
+ ...clientOptions || {}
1009
+ });
1010
+ this.client.on("error", (error) => {
1011
+ console.error(
1012
+ "RedisCacheComponentsHandler client error",
1013
+ error,
1014
+ killContainerOnErrorCount2++
1015
+ );
1016
+ setTimeout(
1017
+ () => this.client.connect().catch((err) => {
1018
+ console.error(
1019
+ "Failed to reconnect RedisCacheComponentsHandler client after connection loss:",
1020
+ err
1021
+ );
1022
+ }),
1023
+ 1e3
1024
+ );
1025
+ if (killContainerOnErrorThreshold > 0 && killContainerOnErrorCount2 >= killContainerOnErrorThreshold) {
1026
+ console.error(
1027
+ "RedisCacheComponentsHandler client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)",
1028
+ error,
1029
+ killContainerOnErrorCount2++
1030
+ );
1031
+ this.client.disconnect();
1032
+ this.client.quit();
1033
+ setTimeout(() => {
1034
+ process.exit(1);
1035
+ }, 500);
1036
+ }
1037
+ });
1038
+ this.client.connect().then(() => {
1039
+ debug("green", "RedisCacheComponentsHandler client connected.");
1040
+ }).catch(() => {
1041
+ this.client.connect().catch((error) => {
1042
+ console.error(
1043
+ "Failed to connect RedisCacheComponentsHandler client:",
1044
+ error
1045
+ );
1046
+ this.client.disconnect();
1047
+ throw error;
1048
+ });
1049
+ });
1050
+ const filterKeys = (key) => key !== REVALIDATED_TAGS_KEY2 && key !== SHARED_TAGS_KEY;
1051
+ this.revalidatedTagsMap = new SyncedMap({
1052
+ client: this.client,
1053
+ keyPrefix,
1054
+ redisKey: REVALIDATED_TAGS_KEY2,
1055
+ database,
1056
+ querySize: revalidateTagQuerySize,
1057
+ filterKeys,
1058
+ resyncIntervalMs: avgResyncIntervalMs + avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
1059
+ });
1060
+ this.sharedTagsMap = new SyncedMap({
1061
+ client: this.client,
1062
+ keyPrefix,
1063
+ redisKey: SHARED_TAGS_KEY,
1064
+ database,
1065
+ querySize: revalidateTagQuerySize,
1066
+ filterKeys,
1067
+ resyncIntervalMs: avgResyncIntervalMs - avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
1068
+ });
1069
+ } catch (error) {
1070
+ console.error("RedisCacheComponentsHandler constructor error", error);
1071
+ throw error;
1072
+ }
1073
+ }
1074
+ async assertClientIsReady() {
1075
+ if (!this.client.isReady && !this.client.isOpen) {
1076
+ await this.client.connect().catch((error) => {
1077
+ console.error(
1078
+ "RedisCacheComponentsHandler assertClientIsReady reconnect error:",
1079
+ error
1080
+ );
1081
+ throw error;
1082
+ });
1083
+ }
1084
+ await Promise.all([
1085
+ this.revalidatedTagsMap.waitUntilReady(),
1086
+ this.sharedTagsMap.waitUntilReady()
1087
+ ]);
1088
+ }
1089
+ async computeMaxRevalidation(tags) {
1090
+ let max = 0;
1091
+ for (const tag of tags) {
1092
+ const ts = this.revalidatedTagsMap.get(tag);
1093
+ if (ts && ts > max) {
1094
+ max = ts;
1095
+ }
1096
+ }
1097
+ return max;
1098
+ }
1099
+ async get(cacheKey, softTags) {
1100
+ const redisKey = `${this.keyPrefix}${cacheKey}`;
1101
+ try {
1102
+ await this.assertClientIsReady();
1103
+ const serialized = await redisErrorHandler(
1104
+ "RedisCacheComponentsHandler.get(), operation: get " + this.getTimeoutMs + "ms " + redisKey,
1105
+ this.client.get(
1106
+ commandOptions2({ signal: AbortSignal.timeout(this.getTimeoutMs) }),
1107
+ redisKey
1108
+ )
1109
+ );
1110
+ if (!serialized) {
1111
+ return void 0;
1112
+ }
1113
+ const stored = JSON.parse(serialized);
1114
+ const now = Date.now();
1115
+ const expiryTime = stored.timestamp + stored.expire * 1e3;
1116
+ if (Number.isFinite(stored.expire) && stored.expire > 0 && now > expiryTime) {
1117
+ await this.client.unlink(redisKey).catch(() => {
1118
+ });
1119
+ await this.sharedTagsMap.delete(cacheKey).catch(() => {
1120
+ });
1121
+ return void 0;
1122
+ }
1123
+ const maxRevalidation = await this.computeMaxRevalidation([
1124
+ ...stored.tags || [],
1125
+ ...softTags || []
1126
+ ]);
1127
+ if (maxRevalidation > 0 && maxRevalidation > stored.timestamp) {
1128
+ await this.client.unlink(redisKey).catch(() => {
1129
+ });
1130
+ await this.sharedTagsMap.delete(cacheKey).catch(() => {
1131
+ });
1132
+ return void 0;
1133
+ }
1134
+ const valueBuffer = typeof stored.value === "string" ? new Uint8Array(Buffer.from(stored.value, "base64")) : stored.value;
1135
+ return {
1136
+ ...stored,
1137
+ value: bufferToReadableStream(valueBuffer)
1138
+ };
1139
+ } catch (error) {
1140
+ console.error(
1141
+ "RedisCacheComponentsHandler.get() Error occurred while getting cache entry. Returning undefined so site can continue to serve content while cache is disabled. The original error was:",
1142
+ error,
1143
+ killContainerOnErrorCount2++
1144
+ );
1145
+ return void 0;
1146
+ }
1147
+ }
1148
+ async set(cacheKey, pendingEntry) {
1149
+ try {
1150
+ await this.assertClientIsReady();
1151
+ const entry = await pendingEntry;
1152
+ const [storeStream] = entry.value.tee();
1153
+ const buffer = await streamToBuffer(storeStream);
1154
+ const stored = {
1155
+ value: Buffer.from(buffer).toString("base64"),
1156
+ tags: entry.tags || [],
1157
+ stale: entry.stale,
1158
+ timestamp: entry.timestamp,
1159
+ expire: entry.expire,
1160
+ revalidate: entry.revalidate
1161
+ };
1162
+ let serialized;
1163
+ try {
1164
+ const cleanStored = {
1165
+ value: stored.value,
1166
+ tags: Array.isArray(stored.tags) ? [...stored.tags] : [],
1167
+ stale: Number(stored.stale),
1168
+ timestamp: Number(stored.timestamp),
1169
+ expire: Number(stored.expire),
1170
+ revalidate: Number(stored.revalidate)
1171
+ };
1172
+ serialized = JSON.stringify(cleanStored);
1173
+ } catch (jsonError) {
1174
+ console.error("JSON.stringify error:", jsonError);
1175
+ console.error("Stored object:", stored);
1176
+ throw jsonError;
1177
+ }
1178
+ const ttlSeconds = Number.isFinite(stored.expire) && stored.expire > 0 ? Math.floor(stored.expire) : void 0;
1179
+ const redisKey = `${this.keyPrefix}${cacheKey}`;
1180
+ const setOperation = redisErrorHandler(
1181
+ "RedisCacheComponentsHandler.set(), operation: set " + redisKey,
1182
+ this.client.set(redisKey, serialized, {
1183
+ ...ttlSeconds ? { EX: ttlSeconds } : {}
1184
+ })
1185
+ );
1186
+ let tagsOperation;
1187
+ const tags = stored.tags || [];
1188
+ if (tags.length > 0) {
1189
+ const currentTags = this.sharedTagsMap.get(cacheKey);
1190
+ const currentIsSameAsNew = currentTags?.length === tags.length && currentTags.every((v) => tags.includes(v)) && tags.every((v) => currentTags.includes(v));
1191
+ if (!currentIsSameAsNew) {
1192
+ tagsOperation = this.sharedTagsMap.set(cacheKey, [...tags]);
1193
+ }
1194
+ }
1195
+ await Promise.all([setOperation, tagsOperation]);
1196
+ } catch (error) {
1197
+ console.error(
1198
+ "RedisCacheComponentsHandler.set() Error occurred while setting cache entry. The original error was:",
1199
+ error,
1200
+ killContainerOnErrorCount2++
1201
+ );
1202
+ throw error;
1203
+ }
1204
+ }
1205
+ async refreshTags() {
1206
+ await this.assertClientIsReady();
1207
+ }
1208
+ async getExpiration(tags) {
1209
+ try {
1210
+ await this.assertClientIsReady();
1211
+ return this.computeMaxRevalidation(tags || []);
1212
+ } catch (error) {
1213
+ console.error(
1214
+ "RedisCacheComponentsHandler.getExpiration() Error occurred while getting expiration for tags. The original error was:",
1215
+ error
1216
+ );
1217
+ return 0;
1218
+ }
1219
+ }
1220
+ async updateTags(tags, _durations) {
1221
+ try {
1222
+ void _durations;
1223
+ await this.assertClientIsReady();
1224
+ const now = Date.now();
1225
+ const tagsSet = new Set(tags || []);
1226
+ for (const tag of tagsSet) {
1227
+ await this.revalidatedTagsMap.set(tag, now);
1228
+ }
1229
+ const keysToDelete = /* @__PURE__ */ new Set();
1230
+ for (const [key, storedTags] of this.sharedTagsMap.entries()) {
1231
+ if (storedTags.some((tag) => tagsSet.has(tag))) {
1232
+ keysToDelete.add(key);
1233
+ }
1234
+ }
1235
+ if (keysToDelete.size === 0) {
1236
+ return;
1237
+ }
1238
+ const cacheKeys = Array.from(keysToDelete);
1239
+ const fullRedisKeys = cacheKeys.map((key) => `${this.keyPrefix}${key}`);
1240
+ await redisErrorHandler(
1241
+ "RedisCacheComponentsHandler.updateTags(), operation: unlink",
1242
+ this.client.unlink(fullRedisKeys)
1243
+ );
1244
+ const deleteTagsOperation = this.sharedTagsMap.delete(cacheKeys);
1245
+ await deleteTagsOperation;
1246
+ } catch (error) {
1247
+ console.error(
1248
+ "RedisCacheComponentsHandler.updateTags() Error occurred while updating tags. The original error was:",
1249
+ error,
1250
+ killContainerOnErrorCount2++
1251
+ );
1252
+ throw error;
1253
+ }
1254
+ }
1255
+ };
1256
+ var singletonHandler;
1257
+ function getRedisCacheComponentsHandler(options = {}) {
1258
+ if (!singletonHandler) {
1259
+ singletonHandler = new RedisCacheComponentsHandler(options);
1260
+ }
1261
+ return singletonHandler;
1262
+ }
1263
+ var redisCacheHandler = getRedisCacheComponentsHandler();
1264
+
951
1265
  // src/index.ts
952
1266
  var index_default = CachedHandler;
953
1267
  export {
954
1268
  RedisStringsHandler,
955
- index_default as default
1269
+ index_default as default,
1270
+ getRedisCacheComponentsHandler,
1271
+ redisCacheHandler
956
1272
  };
957
1273
  //# sourceMappingURL=index.mjs.map