@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.
@@ -965,47 +965,54 @@ async function getImpl(state, cacheKey, softTags) {
965
965
  });
966
966
  return void 0;
967
967
  }
968
- if (softTags.length > 0) {
968
+ const freshnessTags = softTags.length > 0 ? [...softTags, ...envelope.tags] : envelope.tags;
969
+ let tagStale = false;
970
+ if (freshnessTags.length > 0) {
969
971
  let mostRecent = 0;
970
- for (const t of softTags) {
972
+ for (const t of freshnessTags) {
971
973
  const ts = state.localTagTimestamps.get(t);
972
974
  if (ts !== void 0 && ts > mostRecent) mostRecent = ts;
973
975
  }
974
976
  if (mostRecent > envelope.timestamp) {
975
- state.emit({
976
- type: "cache.miss",
977
- meta: { reason: "soft-tag-invalidated" }
978
- });
979
- return void 0;
977
+ if (state.opts.staleWhileRevalidate && partition.freshness !== "expired") {
978
+ tagStale = true;
979
+ } else {
980
+ state.emit({ type: "cache.miss", meta: { reason: "tag-invalidated" } });
981
+ return void 0;
982
+ }
980
983
  }
981
984
  }
982
- if (partition.freshness === "stale") {
985
+ const freshness = tagStale ? "stale" : partition.freshness;
986
+ if (freshness === "stale") {
987
+ const staleMeta = tagStale ? { freshness, reason: "tag-invalidated" } : { freshness };
983
988
  if (state.opts.singleFlight) {
984
989
  const isLeader = await tryAcquireRefreshLock(state, cacheKey);
985
990
  state.emit({
986
991
  type: isLeader ? "cache.stale.refresh.leader" : "cache.stale.refresh.follower",
987
992
  ms: Date.now() - start,
988
- meta: { freshness: partition.freshness }
993
+ meta: staleMeta
989
994
  });
990
995
  } else {
991
996
  state.emit({
992
997
  type: "cache.stale",
993
998
  ms: Date.now() - start,
994
- meta: { freshness: partition.freshness }
999
+ meta: staleMeta
995
1000
  });
996
1001
  }
997
1002
  } else {
998
1003
  state.emit({
999
1004
  type: "cache.hit",
1000
1005
  ms: Date.now() - start,
1001
- meta: { freshness: partition.freshness }
1006
+ meta: { freshness }
1002
1007
  });
1003
1008
  }
1009
+ const revalidateMs = Math.max(0, envelope.revalidate) * 1e3;
1010
+ const outTimestamp = tagStale ? Math.min(envelope.timestamp, Date.now() - revalidateMs - 1) : envelope.timestamp;
1004
1011
  return {
1005
1012
  value: bufferToStream(Buffer.from(envelope.value, "base64")),
1006
1013
  tags: envelope.tags,
1007
1014
  stale: envelope.stale,
1008
- timestamp: envelope.timestamp,
1015
+ timestamp: outTimestamp,
1009
1016
  expire: envelope.expire,
1010
1017
  revalidate: envelope.revalidate
1011
1018
  };