Unverified Commit 786ef25e authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

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.
parent 9288ef49
......@@ -9,6 +9,7 @@ import type {
ExportAppWorker,
ExportPageInput,
} from '../export/types'
import type { Revalidate } from '../server/lib/revalidate'
import '../lib/setup-exception-listeners'
......@@ -169,7 +170,7 @@ interface DataRouteRouteInfo {
export interface SsgRoute
extends ExperimentalBypassForInfo,
DataRouteRouteInfo {
initialRevalidateSeconds: number | false
initialRevalidateSeconds: Revalidate
srcRoute: string | null
initialStatus?: number
initialHeaders?: Record<string, string>
......
......@@ -107,7 +107,7 @@ export async function exportAppPage(
const html = result.toUnchunkedString()
const { metadata } = result
const flightData = metadata.pageData
const revalidate = metadata.revalidate
const revalidate = metadata.revalidate ?? false
if (revalidate === 0) {
if (isDynamicError) {
......
......@@ -214,7 +214,7 @@ export async function exportPages(
return {
ampValidations,
revalidate: metadata.revalidate,
revalidate: metadata.revalidate ?? false,
ssgNotFound,
}
}
......@@ -7,6 +7,7 @@ import type AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import type { FontConfig } from '../server/font-utils'
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import type { Span } from '../trace'
import type { Revalidate } from '../server/lib/revalidate'
export interface AmpValidation {
page: string
......@@ -62,7 +63,7 @@ export type ExportedPageFile = {
export type ExportRouteResult =
| {
ampValidations?: AmpValidation[]
revalidate: number | false
revalidate: Revalidate
metadata?: {
status?: number
headers?: OutgoingHttpHeaders
......@@ -126,7 +127,7 @@ export type ExportAppResult = {
/**
* The revalidation time for the page in seconds.
*/
revalidate?: number | false
revalidate?: Revalidate
/**
* The metadata for the page.
*/
......
/**
* The revalidate option used internally for pages. A value of `false` means
* that the page should not be revalidated. A number means that the page
* should be revalidated after the given number of seconds (this also includes
* `1` which means to revalidate after 1 second). A value of `0` is not a valid
* value for this option.
*/
export type Revalidate = number | false
import type { StaticGenerationStore } from '../client/components/static-generation-async-storage.external'
import type { Revalidate } from './lib/revalidate'
import type { PipeTarget } from './pipe-readable'
import { pipeReadable } from './pipe-readable'
type ContentTypeOption = string | undefined
export type RenderResultMetadata = {
pageData?: any
revalidate?: any
revalidate?: Revalidate
staticBailoutInfo?: any
assetQueryString?: string
isNotFound?: boolean
......
......@@ -37,6 +37,8 @@ import type { NextFontManifest } from '../build/webpack/plugins/next-font-manife
import type { PagesModule } from './future/route-modules/pages/module'
import type { ComponentsEnhancer } from '../shared/lib/utils'
import type { NextParsedUrlQuery } from './request-meta'
import type { Revalidate } from './lib/revalidate'
import React from 'react'
import ReactDOMServer from 'react-dom/server.browser'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
......@@ -820,7 +822,7 @@ export async function renderToHTMLImpl(
}
if (isSSG && !isFallback) {
let data: UnwrapPromise<ReturnType<GetStaticProps>>
let data: Readonly<UnwrapPromise<ReturnType<GetStaticProps>>>
try {
data = await getTracer().trace(
......@@ -931,6 +933,7 @@ export async function renderToHTMLImpl(
)
}
let revalidate: Revalidate
if ('revalidate' in data) {
if (data.revalidate && renderOpts.nextConfigOutput === 'export') {
throw new Error(
......@@ -951,24 +954,28 @@ export async function renderToHTMLImpl(
`\n\nTo never revalidate, you can set revalidate to \`false\` (only ran once at build-time).` +
`\nTo revalidate as soon as possible, you can set the value to \`1\`.`
)
} else if (data.revalidate > 31536000) {
// if it's greater than a year for some reason error
console.warn(
`Warning: A page's revalidate option was set to more than a year for ${req.url}. This may have been done in error.` +
`\nTo only run getStaticProps at build-time and not revalidate at runtime, you can set \`revalidate\` to \`false\`!`
)
} else {
if (data.revalidate > 31536000) {
// if it's greater than a year for some reason error
console.warn(
`Warning: A page's revalidate option was set to more than a year for ${req.url}. This may have been done in error.` +
`\nTo only run getStaticProps at build-time and not revalidate at runtime, you can set \`revalidate\` to \`false\`!`
)
}
revalidate = data.revalidate
}
} else if (data.revalidate === true) {
// When enabled, revalidate after 1 second. This value is optimal for
// the most up-to-date page possible, but without a 1-to-1
// request-refresh ratio.
data.revalidate = 1
revalidate = 1
} else if (
data.revalidate === false ||
typeof data.revalidate === 'undefined'
) {
// By default, we never revalidate.
data.revalidate = false
revalidate = false
} else {
throw new Error(
`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(
}
} else {
// By default, we never revalidate.
;(data as any).revalidate = false
revalidate = false
}
props.pageProps = Object.assign(
......@@ -988,8 +995,7 @@ export async function renderToHTMLImpl(
)
// pass up revalidate and props for export
renderResultMeta.revalidate =
'revalidate' in data ? data.revalidate : undefined
renderResultMeta.revalidate = revalidate
renderResultMeta.pageData = props
// this must come after revalidate is added to renderResultMeta
......
import type { OutgoingHttpHeaders } from 'http'
import type RenderResult from '../render-result'
import type { Revalidate } from '../lib/revalidate'
export interface ResponseCacheBase {
get(
......@@ -93,7 +94,7 @@ export type ResponseCacheValue =
| CachedRouteValue
export type ResponseCacheEntry = {
revalidate?: number | false
revalidate?: Revalidate
value: ResponseCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
......@@ -121,6 +122,6 @@ export interface IncrementalCache {
set: (
key: string,
data: IncrementalCacheValue | null,
ctx: { revalidate: number | false }
ctx: { revalidate: Revalidate }
) => Promise<void>
}
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