@leejpsd/nextjs-cache-handler 0.3.1 → 0.3.2

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.cjs CHANGED
@@ -989,47 +989,54 @@ async function getImpl(state, cacheKey, softTags) {
989
989
  });
990
990
  return void 0;
991
991
  }
992
- if (softTags.length > 0) {
992
+ const freshnessTags = softTags.length > 0 ? [...softTags, ...envelope.tags] : envelope.tags;
993
+ let tagStale = false;
994
+ if (freshnessTags.length > 0) {
993
995
  let mostRecent = 0;
994
- for (const t of softTags) {
996
+ for (const t of freshnessTags) {
995
997
  const ts = state.localTagTimestamps.get(t);
996
998
  if (ts !== void 0 && ts > mostRecent) mostRecent = ts;
997
999
  }
998
1000
  if (mostRecent > envelope.timestamp) {
999
- state.emit({
1000
- type: "cache.miss",
1001
- meta: { reason: "soft-tag-invalidated" }
1002
- });
1003
- return void 0;
1001
+ if (state.opts.staleWhileRevalidate && partition.freshness !== "expired") {
1002
+ tagStale = true;
1003
+ } else {
1004
+ state.emit({ type: "cache.miss", meta: { reason: "tag-invalidated" } });
1005
+ return void 0;
1006
+ }
1004
1007
  }
1005
1008
  }
1006
- if (partition.freshness === "stale") {
1009
+ const freshness = tagStale ? "stale" : partition.freshness;
1010
+ if (freshness === "stale") {
1011
+ const staleMeta = tagStale ? { freshness, reason: "tag-invalidated" } : { freshness };
1007
1012
  if (state.opts.singleFlight) {
1008
1013
  const isLeader = await tryAcquireRefreshLock(state, cacheKey);
1009
1014
  state.emit({
1010
1015
  type: isLeader ? "cache.stale.refresh.leader" : "cache.stale.refresh.follower",
1011
1016
  ms: Date.now() - start,
1012
- meta: { freshness: partition.freshness }
1017
+ meta: staleMeta
1013
1018
  });
1014
1019
  } else {
1015
1020
  state.emit({
1016
1021
  type: "cache.stale",
1017
1022
  ms: Date.now() - start,
1018
- meta: { freshness: partition.freshness }
1023
+ meta: staleMeta
1019
1024
  });
1020
1025
  }
1021
1026
  } else {
1022
1027
  state.emit({
1023
1028
  type: "cache.hit",
1024
1029
  ms: Date.now() - start,
1025
- meta: { freshness: partition.freshness }
1030
+ meta: { freshness }
1026
1031
  });
1027
1032
  }
1033
+ const revalidateMs = Math.max(0, envelope.revalidate) * 1e3;
1034
+ const outTimestamp = tagStale ? Math.min(envelope.timestamp, Date.now() - revalidateMs - 1) : envelope.timestamp;
1028
1035
  return {
1029
1036
  value: bufferToStream(Buffer.from(envelope.value, "base64")),
1030
1037
  tags: envelope.tags,
1031
1038
  stale: envelope.stale,
1032
- timestamp: envelope.timestamp,
1039
+ timestamp: outTimestamp,
1033
1040
  expire: envelope.expire,
1034
1041
  revalidate: envelope.revalidate
1035
1042
  };