Skip to content

Commit 786ef25

Browse files
authored
Revalidate Type (#56763)
This adds a new `Revalidate` type which is used internally by Next.js to associate the user inputted value of `revalidate` from `getStaticProps` or the exported `revalidate` variable in app directory.
1 parent 9288ef4 commit 786ef25

File tree

8 files changed

+39
-20
lines changed

8 files changed

+39
-20
lines changed

packages/next/src/build/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
ExportAppWorker,
1010
ExportPageInput,
1111
} from '../export/types'
12+
import type { Revalidate } from '../server/lib/revalidate'
1213

1314
import '../lib/setup-exception-listeners'
1415

@@ -169,7 +170,7 @@ interface DataRouteRouteInfo {
169170
export interface SsgRoute
170171
extends ExperimentalBypassForInfo,
171172
DataRouteRouteInfo {
172-
initialRevalidateSeconds: number | false
173+
initialRevalidateSeconds: Revalidate
173174
srcRoute: string | null
174175
initialStatus?: number
175176
initialHeaders?: Record<string, string>

packages/next/src/export/routes/app-page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export async function exportAppPage(
107107
const html = result.toUnchunkedString()
108108
const { metadata } = result
109109
const flightData = metadata.pageData
110-
const revalidate = metadata.revalidate
110+
const revalidate = metadata.revalidate ?? false
111111

112112
if (revalidate === 0) {
113113
if (isDynamicError) {

packages/next/src/export/routes/pages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export async function exportPages(
214214

215215
return {
216216
ampValidations,
217-
revalidate: metadata.revalidate,
217+
revalidate: metadata.revalidate ?? false,
218218
ssgNotFound,
219219
}
220220
}

packages/next/src/export/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
77
import type { FontConfig } from '../server/font-utils'
88
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
99
import type { Span } from '../trace'
10+
import type { Revalidate } from '../server/lib/revalidate'
1011

1112
export interface AmpValidation {
1213
page: string
@@ -62,7 +63,7 @@ export type ExportedPageFile = {
6263
export type ExportRouteResult =
6364
| {
6465
ampValidations?: AmpValidation[]
65-
revalidate: number | false
66+
revalidate: Revalidate
6667
metadata?: {
6768
status?: number
6869
headers?: OutgoingHttpHeaders
@@ -126,7 +127,7 @@ export type ExportAppResult = {
126127
/**
127128
* The revalidation time for the page in seconds.
128129
*/
129-
revalidate?: number | false
130+
revalidate?: Revalidate
130131
/**
131132
* The metadata for the page.
132133
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* The revalidate option used internally for pages. A value of `false` means
3+
* that the page should not be revalidated. A number means that the page
4+
* should be revalidated after the given number of seconds (this also includes
5+
* `1` which means to revalidate after 1 second). A value of `0` is not a valid
6+
* value for this option.
7+
*/
8+
export type Revalidate = number | false

packages/next/src/server/render-result.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { StaticGenerationStore } from '../client/components/static-generation-async-storage.external'
2+
import type { Revalidate } from './lib/revalidate'
23
import type { PipeTarget } from './pipe-readable'
4+
35
import { pipeReadable } from './pipe-readable'
46

57
type ContentTypeOption = string | undefined
68

79
export type RenderResultMetadata = {
810
pageData?: any
9-
revalidate?: any
11+
revalidate?: Revalidate
1012
staticBailoutInfo?: any
1113
assetQueryString?: string
1214
isNotFound?: boolean

packages/next/src/server/render.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import type { NextFontManifest } from '../build/webpack/plugins/next-font-manife
3737
import type { PagesModule } from './future/route-modules/pages/module'
3838
import type { ComponentsEnhancer } from '../shared/lib/utils'
3939
import type { NextParsedUrlQuery } from './request-meta'
40+
import type { Revalidate } from './lib/revalidate'
41+
4042
import React from 'react'
4143
import ReactDOMServer from 'react-dom/server.browser'
4244
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
@@ -820,7 +822,7 @@ export async function renderToHTMLImpl(
820822
}
821823

822824
if (isSSG && !isFallback) {
823-
let data: UnwrapPromise<ReturnType<GetStaticProps>>
825+
let data: Readonly<UnwrapPromise<ReturnType<GetStaticProps>>>
824826

825827
try {
826828
data = await getTracer().trace(
@@ -931,6 +933,7 @@ export async function renderToHTMLImpl(
931933
)
932934
}
933935

936+
let revalidate: Revalidate
934937
if ('revalidate' in data) {
935938
if (data.revalidate && renderOpts.nextConfigOutput === 'export') {
936939
throw new Error(
@@ -951,24 +954,28 @@ export async function renderToHTMLImpl(
951954
`\n\nTo never revalidate, you can set revalidate to \`false\` (only ran once at build-time).` +
952955
`\nTo revalidate as soon as possible, you can set the value to \`1\`.`
953956
)
954-
} else if (data.revalidate > 31536000) {
955-
// if it's greater than a year for some reason error
956-
console.warn(
957-
`Warning: A page's revalidate option was set to more than a year for ${req.url}. This may have been done in error.` +
958-
`\nTo only run getStaticProps at build-time and not revalidate at runtime, you can set \`revalidate\` to \`false\`!`
959-
)
957+
} else {
958+
if (data.revalidate > 31536000) {
959+
// if it's greater than a year for some reason error
960+
console.warn(
961+
`Warning: A page's revalidate option was set to more than a year for ${req.url}. This may have been done in error.` +
962+
`\nTo only run getStaticProps at build-time and not revalidate at runtime, you can set \`revalidate\` to \`false\`!`
963+
)
964+
}
965+
966+
revalidate = data.revalidate
960967
}
961968
} else if (data.revalidate === true) {
962969
// When enabled, revalidate after 1 second. This value is optimal for
963970
// the most up-to-date page possible, but without a 1-to-1
964971
// request-refresh ratio.
965-
data.revalidate = 1
972+
revalidate = 1
966973
} else if (
967974
data.revalidate === false ||
968975
typeof data.revalidate === 'undefined'
969976
) {
970977
// By default, we never revalidate.
971-
data.revalidate = false
978+
revalidate = false
972979
} else {
973980
throw new Error(
974981
`A page's revalidate option must be seconds expressed as a natural number. Mixed numbers and strings cannot be used. Received '${JSON.stringify(
@@ -978,7 +985,7 @@ export async function renderToHTMLImpl(
978985
}
979986
} else {
980987
// By default, we never revalidate.
981-
;(data as any).revalidate = false
988+
revalidate = false
982989
}
983990

984991
props.pageProps = Object.assign(
@@ -988,8 +995,7 @@ export async function renderToHTMLImpl(
988995
)
989996

990997
// pass up revalidate and props for export
991-
renderResultMeta.revalidate =
992-
'revalidate' in data ? data.revalidate : undefined
998+
renderResultMeta.revalidate = revalidate
993999
renderResultMeta.pageData = props
9941000

9951001
// this must come after revalidate is added to renderResultMeta

packages/next/src/server/response-cache/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { OutgoingHttpHeaders } from 'http'
22
import type RenderResult from '../render-result'
3+
import type { Revalidate } from '../lib/revalidate'
34

45
export interface ResponseCacheBase {
56
get(
@@ -93,7 +94,7 @@ export type ResponseCacheValue =
9394
| CachedRouteValue
9495

9596
export type ResponseCacheEntry = {
96-
revalidate?: number | false
97+
revalidate?: Revalidate
9798
value: ResponseCacheValue | null
9899
isStale?: boolean | -1
99100
isMiss?: boolean
@@ -121,6 +122,6 @@ export interface IncrementalCache {
121122
set: (
122123
key: string,
123124
data: IncrementalCacheValue | null,
124-
ctx: { revalidate: number | false }
125+
ctx: { revalidate: Revalidate }
125126
) => Promise<void>
126127
}

0 commit comments

Comments
 (0)