-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show cache life column in build output
- Loading branch information
1 parent
00aba49
commit b9af2e5
Showing
5 changed files
with
117 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { CacheControl } from '../../server/lib/cache-control' | ||
|
||
const timeUnits = [ | ||
{ label: 'y', seconds: 31536000 }, | ||
{ label: 'w', seconds: 604800 }, | ||
{ label: 'd', seconds: 86400 }, | ||
{ label: 'h', seconds: 3600 }, | ||
{ label: 'min', seconds: 60 }, | ||
{ label: 's', seconds: 1 }, | ||
] | ||
|
||
function humanReadableTimeRounded(seconds: number): string { | ||
// Find the largest fitting unit. | ||
let candidateIndex = timeUnits.length - 1 | ||
for (let i = 0; i < timeUnits.length; i++) { | ||
if (seconds >= timeUnits[i].seconds) { | ||
candidateIndex = i | ||
break | ||
} | ||
} | ||
|
||
const candidate = timeUnits[candidateIndex] | ||
const value = seconds / candidate.seconds | ||
const isExact = Number.isInteger(value) | ||
|
||
// For days and weeks only, check if using the next smaller unit yields an | ||
// exact result. | ||
if (!isExact && (candidate.label === 'd' || candidate.label === 'w')) { | ||
const nextUnit = timeUnits[candidateIndex + 1] | ||
const nextValue = seconds / nextUnit.seconds | ||
|
||
if (Number.isInteger(nextValue)) { | ||
return `${nextValue} ${nextUnit.label}` | ||
} | ||
} | ||
|
||
if (isExact) { | ||
return `${value} ${candidate.label}` | ||
} | ||
|
||
return `~${Math.round(value)} ${candidate.label}` | ||
} | ||
|
||
export function formatCacheControl(cacheControl: CacheControl): string { | ||
const { revalidate, expire } = cacheControl | ||
|
||
if (!revalidate) { | ||
return '' | ||
} | ||
|
||
const readableRevalidate = humanReadableTimeRounded(revalidate) | ||
const readableExpire = expire ? humanReadableTimeRounded(expire) : '∞' | ||
|
||
return `${readableRevalidate} / ${readableExpire}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters