-
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 revalidate/expire columns in build output (#76343)
For pages router ISR pages, we are showing the revalidate times in the build output tree view, e.g.: ``` Route (pages) Size First Load JS ┌ ○ /404 190 B 92.6 kB └ ● /my-isr-page (ISR: 300 Seconds) 291 B 92.7 kB ``` For app router pages, this info is currently missing. With this PR, we're not only adding the revalidate times for app router routes, but also showing the expire times as well. Those may be configured in the Next.js config [`expireTime`](https://nextjs.org/docs/app/api-reference/config/next-config-js/expireTime), or via [cache profiles](https://nextjs.org/docs/app/api-reference/functions/cacheLife) in `"use cache"` functions. Both values are moved into separate `Revalidate` and `Expire` columns and are shown in a human-readable format. **After:** <img width="708" alt="two columns" src="https://github.com/user-attachments/assets/3d6dff7b-7df1-43b2-ae33-802227a3cf88" /> **Before:** <img width="788" alt="before" src="https://github.com/user-attachments/assets/19d5bc2c-c39f-4d44-97e0-ddcb72de3d82" /> closes NAR-96
- Loading branch information
1 parent
3ae9d38
commit d43fc5f
Showing
6 changed files
with
154 additions
and
60 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,54 @@ | ||
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: 'm', 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 formatRevalidate(cacheControl: CacheControl): string { | ||
const { revalidate } = cacheControl | ||
|
||
return revalidate ? humanReadableTimeRounded(revalidate) : '' | ||
} | ||
|
||
export function formatExpire(cacheControl: CacheControl): string { | ||
const { expire } = cacheControl | ||
|
||
return expire ? humanReadableTimeRounded(expire) : '' | ||
} |
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
9 changes: 9 additions & 0 deletions
9
test/production/app-dir/build-output-tree-view/fixtures/mixed/app/cache-life-custom/page.tsx
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,9 @@ | ||
'use cache' | ||
|
||
import { unstable_cacheLife } from 'next/cache' | ||
|
||
export default async function Page() { | ||
unstable_cacheLife({ revalidate: 412, expire: 8940 }) | ||
|
||
return <p>hello world</p> | ||
} |
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