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