Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
f464e32e
Unverified
Commit
f464e32e
authored
Sep 11, 2026
by
James Argarin
Committed by
GitHub
Sep 11, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(typegen): merge cacheLife types into next/cache instead of shadowing it (#98252)
parent
64596d1f
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
148 additions
and
325 deletions
+148
-325
cache.d.ts
packages/next/cache.d.ts
+5
-1
cache-life-type-utils.test.ts
...src/server/lib/router-utils/cache-life-type-utils.test.ts
+34
-276
cache-life-type-utils.ts
...next/src/server/lib/router-utils/cache-life-type-utils.ts
+15
-48
layout.tsx
test/e2e/cache-life-typecheck/app/layout.tsx
+9
-0
page.tsx
test/e2e/cache-life-typecheck/app/page.tsx
+3
-0
cache-life-typecheck.test.ts
test/e2e/cache-life-typecheck/cache-life-typecheck.test.ts
+35
-0
next.config.js
test/e2e/cache-life-typecheck/next.config.js
+14
-0
tsconfig.json
test/e2e/cache-life-typecheck/tsconfig.json
+20
-0
typecheck-validation.ts
test/e2e/cache-life-typecheck/typecheck-validation.ts
+13
-0
No files found.
packages/next/cache.d.ts
View file @
f464e32e
...
@@ -20,6 +20,8 @@ import { cacheTag } from 'next/dist/server/use-cache/cache-tag'
...
@@ -20,6 +20,8 @@ import { cacheTag } from 'next/dist/server/use-cache/cache-tag'
export
{
cacheTag
}
export
{
cacheTag
}
export
interface
CacheLifeProfiles
{}
/**
/**
* Cache this `"use cache"` for a timespan defined by the `"default"` profile.
* Cache this `"use cache"` for a timespan defined by the `"default"` profile.
* ```
* ```
...
@@ -128,7 +130,9 @@ export function cacheLife(profile: 'max'): void
...
@@ -128,7 +130,9 @@ export function cacheLife(profile: 'max'): void
*
*
* You can define custom profiles in `next.config.ts`.
* You can define custom profiles in `next.config.ts`.
*/
*/
export
function
cacheLife
(
profile
:
string
):
void
export
function
cacheLife
(
profile
:
keyof
CacheLifeProfiles
extends
never
?
string
:
never
):
void
/**
/**
* Cache this `"use cache"` using a custom timespan.
* Cache this `"use cache"` using a custom timespan.
...
...
packages/next/src/server/lib/router-utils/cache-life-type-utils.test.ts
View file @
f464e32e
This diff is collapsed.
Click to expand it.
packages/next/src/server/lib/router-utils/cache-life-type-utils.ts
View file @
f464e32e
...
@@ -71,6 +71,7 @@ function formatTimespanWithSeconds(seconds: undefined | number): string {
...
@@ -71,6 +71,7 @@ function formatTimespanWithSeconds(seconds: undefined | number): string {
export
function
generateCacheLifeTypes
(
cacheLife
:
{
export
function
generateCacheLifeTypes
(
cacheLife
:
{
[
profile
:
string
]:
CacheLife
[
profile
:
string
]:
CacheLife
}):
string
{
}):
string
{
let
profileKeys
=
''
let
overloads
=
''
let
overloads
=
''
const
profileNames
=
Object
.
keys
(
cacheLife
)
const
profileNames
=
Object
.
keys
(
cacheLife
)
...
@@ -81,6 +82,9 @@ export function generateCacheLifeTypes(cacheLife: {
...
@@ -81,6 +82,9 @@ export function generateCacheLifeTypes(cacheLife: {
continue
continue
}
}
profileKeys
+=
`
${
JSON
.
stringify
(
profileName
)}
: true`
let
description
=
''
let
description
=
''
if
(
profile
.
stale
===
undefined
)
{
if
(
profile
.
stale
===
undefined
)
{
...
@@ -136,58 +140,21 @@ export function generateCacheLifeTypes(cacheLife: {
...
@@ -136,58 +140,21 @@ export function generateCacheLifeTypes(cacheLife: {
`
`
}
}
overloads
+=
`
// The top-level `export {}` makes this file a module, which in turn makes
/**
// the block below a module augmentation that merges into the `next/cache`
* Cache this \`"use cache"\` using a custom timespan.
// types. Without it the file is a script, the block is a standalone ambient
* \`\`\`
// module declaration, and it shadows `next/cache` instead of merging: every
* stale: ... // seconds
// other export then has to be redeclared here, and TypeScript offers two
* revalidate: ... // seconds
// identical auto-import completions for each of them, one from this file and
* expire: ... // seconds
// one from the package's own types.
* \`\`\`
*
* This is similar to Cache-Control: max-age=\`stale\`,s-max-age=\`revalidate\`,stale-while-revalidate=\`expire-revalidate\`
*
* If a value is left out, the lowest of other cacheLife() calls or the default, is used instead.
*/
export function cacheLife(profile: {
/**
* This cache may be stale on clients for ... seconds before checking with the server.
*/
stale?: number,
/**
* If the server receives a new request after ... seconds, start revalidating new values in the background.
*/
revalidate?: number,
/**
* If this entry has no traffic for ... seconds it will expire. The next request will recompute it.
*/
expire?: number
}): void
`
// Redefine the cacheLife() accepted arguments.
return
`// Type definitions for Next.js cacheLife configs
return
`// Type definitions for Next.js cacheLife configs
declare module 'next/cache' {
export {}
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export {
updateTag,
revalidateTag,
revalidatePath,
refresh,
} from 'next/dist/server/web/spec-extension/revalidate'
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
export { io } from 'next/dist/server/request/io'
export { unstable_navigation } from 'next/dist/server/request/cache-stages'
export { unstable_prefetch } from 'next/dist/server/request/cache-stages'
declare module 'next/cache' {
export interface CacheLifeProfiles {
${
profileKeys
}
}
${
overloads
}
${
overloads
}
import { cacheTag } from 'next/dist/server/use-cache/cache-tag'
export { cacheTag }
export const unstable_cacheTag: typeof cacheTag
export const unstable_cacheLife: typeof cacheLife
}
}
`
`
}
}
...
...
test/e2e/cache-life-typecheck/app/layout.tsx
0 → 100644
View file @
f464e32e
import
{
ReactNode
}
from
'react'
export
default
function
Root
({
children
}:
{
children
:
ReactNode
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/e2e/cache-life-typecheck/app/page.tsx
0 → 100644
View file @
f464e32e
export
default
function
Page
()
{
return
<
p
>
hello world
</
p
>
}
test/e2e/cache-life-typecheck/cache-life-typecheck.test.ts
0 → 100644
View file @
f464e32e
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
retry
}
from
'next-test-utils'
import
execa
from
'execa'
describe
(
'cache-life-typecheck'
,
()
=>
{
const
{
next
,
skipped
}
=
nextTestSetup
({
files
:
__dirname
,
skipDeployment
:
true
,
})
if
(
skipped
)
{
return
}
it
(
'accepts configured cacheLife profiles and rejects unknown ones'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/'
)
expect
(
$
(
'p'
).
text
()).
toBe
(
'hello world'
)
await
retry
(
async
()
=>
{
await
next
.
readFile
(
`
${
next
.
distDir
}
/types/cache-life.d.ts`
)
},
10
_000
)
await
next
.
stop
()
try
{
const
{
stdout
,
stderr
}
=
await
execa
(
'pnpm'
,
[
'tsc'
,
'--noEmit'
],
{
cwd
:
next
.
testDir
,
reject
:
false
,
})
expect
({
stdout
,
stderr
}).
toEqual
({
stdout
:
''
,
stderr
:
''
})
}
finally
{
await
next
.
start
()
}
})
})
test/e2e/cache-life-typecheck/next.config.js
0 → 100644
View file @
f464e32e
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{
cacheLife
:
{
blog
:
{
stale
:
300
,
revalidate
:
900
,
expire
:
3600
,
},
},
}
module
.
exports
=
nextConfig
test/e2e/cache-life-typecheck/tsconfig.json
0 → 100644
View file @
f464e32e
{
"compilerOptions"
:
{
"target"
:
"ES2017"
,
"lib"
:
[
"dom"
,
"dom.iterable"
,
"esnext"
],
"allowJs"
:
true
,
"skipLibCheck"
:
true
,
"strict"
:
true
,
"noEmit"
:
true
,
"incremental"
:
true
,
"module"
:
"esnext"
,
"esModuleInterop"
:
true
,
"moduleResolution"
:
"bundler"
,
"resolveJsonModule"
:
true
,
"isolatedModules"
:
true
,
"jsx"
:
"react-jsx"
,
"plugins"
:
[{
"name"
:
"next"
}]
},
"include"
:
[
"next-env.d.ts"
,
"**/*.mts"
,
"**/*.ts"
,
"**/*.tsx"
],
"exclude"
:
[
"node_modules"
,
"**/*.test.ts"
,
"**/*.test.tsx"
]
}
test/e2e/cache-life-typecheck/typecheck-validation.ts
0 → 100644
View file @
f464e32e
import
{
cacheLife
,
unstable_cacheLife
}
from
'next/cache'
export
function
validate
()
{
cacheLife
(
'blog'
)
cacheLife
(
'minutes'
)
cacheLife
({
stale
:
30
,
revalidate
:
60
,
expire
:
120
})
unstable_cacheLife
(
'blog'
)
// @ts-expect-error unknown profile
cacheLife
(
'bogus'
)
// @ts-expect-error unknown profile
unstable_cacheLife
(
'bogus'
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment