Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revalidate Type #56763

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ExportAppWorker,
ExportPageInput,
} from '../export/types'
import type { Revalidate } from '../server/lib/revalidate'

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

Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/export/routes/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export async function exportPages(

return {
ampValidations,
revalidate: metadata.revalidate,
revalidate: metadata.revalidate ?? false,
ssgNotFound,
}
}
5 changes: 3 additions & 2 deletions packages/next/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ export type ExportedPageFile = {
export type ExportRouteResult =
| {
ampValidations?: AmpValidation[]
revalidate: number | false
revalidate: Revalidate
metadata?: {
status?: number
headers?: OutgoingHttpHeaders
Expand Down Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/next/src/server/lib/revalidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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
4 changes: 3 additions & 1 deletion packages/next/src/server/render-result.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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
Expand Down
30 changes: 18 additions & 12 deletions packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -931,6 +933,7 @@ export async function renderToHTMLImpl(
)
}

let revalidate: Revalidate
if ('revalidate' in data) {
if (data.revalidate && renderOpts.nextConfigOutput === 'export') {
throw new Error(
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this changed because TS didn't like it or something?

// 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(
Expand All @@ -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(
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/response-cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { OutgoingHttpHeaders } from 'http'
import type RenderResult from '../render-result'
import type { Revalidate } from '../lib/revalidate'

export interface ResponseCacheBase {
get(
Expand Down Expand Up @@ -93,7 +94,7 @@ export type ResponseCacheValue =
| CachedRouteValue

export type ResponseCacheEntry = {
revalidate?: number | false
revalidate?: Revalidate
value: ResponseCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
Expand Down Expand Up @@ -121,6 +122,6 @@ export interface IncrementalCache {
set: (
key: string,
data: IncrementalCacheValue | null,
ctx: { revalidate: number | false }
ctx: { revalidate: Revalidate }
) => Promise<void>
}
Loading