Skip to content

Commit

Permalink
chore: added revalidate type
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Oct 12, 2023
1 parent 230099b commit d85c7cd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 14 deletions.
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
14 changes: 8 additions & 6 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 | undefined
if ('revalidate' in data) {
if (data.revalidate && renderOpts.nextConfigOutput === 'export') {
throw new Error(
Expand Down Expand Up @@ -962,13 +965,13 @@ export async function renderToHTMLImpl(
// 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 +981,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 +991,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>
}

0 comments on commit d85c7cd

Please sign in to comment.