@leejpsd/nextjs-cache-handler 0.3.0 → 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/CHANGELOG.md +14 -0
- package/dist/cache-components/index.cjs +19 -12
- package/dist/cache-components/index.cjs.map +1 -1
- package/dist/cache-components/index.js +19 -12
- package/dist/cache-components/index.js.map +1 -1
- package/dist/index.cjs +19 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +19 -12
- package/dist/index.js.map +1 -1
- package/package.json +8 -3
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
|
-
|
|
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
|
|
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.
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
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
|
-
|
|
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:
|
|
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:
|
|
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
|
|
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:
|
|
1029
|
+
timestamp: outTimestamp,
|
|
1023
1030
|
expire: envelope.expire,
|
|
1024
1031
|
revalidate: envelope.revalidate
|
|
1025
1032
|
};
|