Unverified Commit d9f7ccae authored by Josh Story's avatar Josh Story Committed by GitHub

Use perf_hooks for current time in incremental cache (#70925)

stacked on #70924 

Incremental cache tracks time using Date.now() and new Date(). this
change updates these timers to use perf_hooks or performance. These are
not susceptible to global clock changes and will not be considered
dynamic for purposes of
dynamicIO in the future
parent f70006ea
...@@ -199,7 +199,8 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -199,7 +199,8 @@ export class IncrementalCache implements IncrementalCacheType {
): Revalidate { ): Revalidate {
// in development we don't have a prerender-manifest // in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging // and default to always revalidating to allow easier debugging
if (dev) return new Date().getTime() - 1000 if (dev)
return Math.floor(performance.timeOrigin + performance.now() - 1000)
// if an entry isn't present in routes we fallback to a default // if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second unless it's a fallback request. // of revalidating after 1 second unless it's a fallback request.
...@@ -433,7 +434,11 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -433,7 +434,11 @@ export class IncrementalCache implements IncrementalCacheType {
} }
revalidate = revalidate || cacheData.value.revalidate revalidate = revalidate || cacheData.value.revalidate
const age = (Date.now() - (cacheData.lastModified || 0)) / 1000 const age =
(performance.timeOrigin +
performance.now() -
(cacheData.lastModified || 0)) /
1000
const isStale = age > revalidate const isStale = age > revalidate
const data = cacheData.value.data const data = cacheData.value.data
...@@ -445,7 +450,8 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -445,7 +450,8 @@ export class IncrementalCache implements IncrementalCacheType {
data, data,
revalidate: revalidate, revalidate: revalidate,
}, },
revalidateAfter: Date.now() + revalidate * 1000, revalidateAfter:
performance.timeOrigin + performance.now() + revalidate * 1000,
isFallback, isFallback,
} satisfies IncrementalCacheEntry } satisfies IncrementalCacheEntry
} }
...@@ -461,12 +467,13 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -461,12 +467,13 @@ export class IncrementalCache implements IncrementalCacheType {
} else { } else {
revalidateAfter = this.calculateRevalidate( revalidateAfter = this.calculateRevalidate(
cacheKey, cacheKey,
cacheData?.lastModified || Date.now(), cacheData?.lastModified || performance.timeOrigin + performance.now(),
this.dev ? ctx.kind !== IncrementalCacheKind.FETCH : false, this.dev ? ctx.kind !== IncrementalCacheKind.FETCH : false,
ctx.isFallback ctx.isFallback
) )
isStale = isStale =
revalidateAfter !== false && revalidateAfter < Date.now() revalidateAfter !== false &&
revalidateAfter < performance.timeOrigin + performance.now()
? true ? true
: undefined : undefined
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment