@leejpsd/nextjs-cache-handler 0.3.1 → 0.3.3

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.
package/dist/index.js CHANGED
@@ -979,47 +979,54 @@ async function getImpl(state, cacheKey, softTags) {
979
979
  });
980
980
  return void 0;
981
981
  }
982
- if (softTags.length > 0) {
982
+ const freshnessTags = softTags.length > 0 ? [...softTags, ...envelope.tags] : envelope.tags;
983
+ let tagStale = false;
984
+ if (freshnessTags.length > 0) {
983
985
  let mostRecent = 0;
984
- for (const t of softTags) {
986
+ for (const t of freshnessTags) {
985
987
  const ts = state.localTagTimestamps.get(t);
986
988
  if (ts !== void 0 && ts > mostRecent) mostRecent = ts;
987
989
  }
988
990
  if (mostRecent > envelope.timestamp) {
989
- state.emit({
990
- type: "cache.miss",
991
- meta: { reason: "soft-tag-invalidated" }
992
- });
993
- return void 0;
991
+ if (state.opts.staleWhileRevalidate && partition.freshness !== "expired") {
992
+ tagStale = true;
993
+ } else {
994
+ state.emit({ type: "cache.miss", meta: { reason: "tag-invalidated" } });
995
+ return void 0;
996
+ }
994
997
  }
995
998
  }
996
- if (partition.freshness === "stale") {
999
+ const freshness = tagStale ? "stale" : partition.freshness;
1000
+ if (freshness === "stale") {
1001
+ const staleMeta = tagStale ? { freshness, reason: "tag-invalidated" } : { freshness };
997
1002
  if (state.opts.singleFlight) {
998
1003
  const isLeader = await tryAcquireRefreshLock(state, cacheKey);
999
1004
  state.emit({
1000
1005
  type: isLeader ? "cache.stale.refresh.leader" : "cache.stale.refresh.follower",
1001
1006
  ms: Date.now() - start,
1002
- meta: { freshness: partition.freshness }
1007
+ meta: staleMeta
1003
1008
  });
1004
1009
  } else {
1005
1010
  state.emit({
1006
1011
  type: "cache.stale",
1007
1012
  ms: Date.now() - start,
1008
- meta: { freshness: partition.freshness }
1013
+ meta: staleMeta
1009
1014
  });
1010
1015
  }
1011
1016
  } else {
1012
1017
  state.emit({
1013
1018
  type: "cache.hit",
1014
1019
  ms: Date.now() - start,
1015
- meta: { freshness: partition.freshness }
1020
+ meta: { freshness }
1016
1021
  });
1017
1022
  }
1023
+ const revalidateMs = Math.max(0, envelope.revalidate) * 1e3;
1024
+ const outTimestamp = tagStale ? Math.min(envelope.timestamp, Date.now() - revalidateMs - 1) : envelope.timestamp;
1018
1025
  return {
1019
1026
  value: bufferToStream(Buffer.from(envelope.value, "base64")),
1020
1027
  tags: envelope.tags,
1021
1028
  stale: envelope.stale,
1022
- timestamp: envelope.timestamp,
1029
+ timestamp: outTimestamp,
1023
1030
  expire: envelope.expire,
1024
1031
  revalidate: envelope.revalidate
1025
1032
  };
@@ -1557,8 +1564,35 @@ var IncrementalRedisCacheHandler = class {
1557
1564
  this.state.emit({ type: "cache.miss", meta: { reason: "tag-state-expired" } });
1558
1565
  return null;
1559
1566
  }
1567
+ let lastModified = parsed.lastModified;
1568
+ if (!isFetch && typeof lastModified === "number") {
1569
+ const softTs = Math.max(
1570
+ 0,
1571
+ ...tagStates.map(
1572
+ (s) => s && typeof s.stale === "number" ? s.stale : 0
1573
+ )
1574
+ );
1575
+ if (softTs >= lastModified) {
1576
+ const rev = parsed.revalidateSec;
1577
+ if (typeof rev !== "number" || rev <= 0 || rev >= ONE_YEAR_SEC2) {
1578
+ this.state.emit({
1579
+ type: "cache.miss",
1580
+ meta: { reason: "tag-invalidated" }
1581
+ });
1582
+ return null;
1583
+ }
1584
+ lastModified = Math.min(
1585
+ lastModified,
1586
+ Date.now() - (rev + 1) * 1e3
1587
+ );
1588
+ this.state.emit({
1589
+ type: "cache.stale",
1590
+ meta: { reason: "tag-invalidated" }
1591
+ });
1592
+ }
1593
+ }
1560
1594
  this.state.emit({ type: "cache.hit" });
1561
- return { ...parsed, tags };
1595
+ return { ...parsed, lastModified, tags };
1562
1596
  } catch (err) {
1563
1597
  this.state.logger.error("get() unexpected error", {
1564
1598
  message: err.message
@@ -1572,7 +1606,11 @@ var IncrementalRedisCacheHandler = class {
1572
1606
  const tags = extractTagsFromValue(data, ctx);
1573
1607
  const useLocal = shouldUseLocalIncrementalStore(tags);
1574
1608
  const ttl = normalizeTtlSeconds(data, ctx);
1575
- const record = { lastModified: Date.now(), value: data };
1609
+ const record = {
1610
+ lastModified: Date.now(),
1611
+ value: data,
1612
+ revalidateSec: ttl
1613
+ };
1576
1614
  const serialized = serializeCacheRecord(record);
1577
1615
  const eKey = entryKey2(this.state, cacheKey);
1578
1616
  this.requestMemo.delete(eKey);