未验证 提交 c40f407d 编写于 作者: T Tim Neutkens 提交者: GitHub

Stabilize revalidate (#15338)

上级 a45def87
......@@ -40,10 +40,8 @@ export const GSSP_NO_RETURNED_VALUE =
'Your `getServerSideProps` function did not return an object. Did you forget to add a `return`?'
export const UNSTABLE_REVALIDATE_RENAME_ERROR =
'The `revalidate` property is not yet available for general use.\n' +
'To try the experimental implementation, please use `unstable_revalidate` instead.\n' +
"We're excited for you to try this feature—please share all feedback on the RFC:\n" +
'https://nextjs.link/issg'
'The `unstable_revalidate` property is available for general use.\n' +
'Please use `revalidate` instead.'
export const GSSP_COMPONENT_MEMBER_ERROR = `can not be attached to a page's component and must be exported from the page. See more info here: https://err.sh/next.js/gssp-component-member`
......
......@@ -525,10 +525,10 @@ export async function renderToHTML(
}
const invalidKeys = Object.keys(data).filter(
(key) => key !== 'unstable_revalidate' && key !== 'props'
(key) => key !== 'revalidate' && key !== 'props'
)
if (invalidKeys.includes('revalidate')) {
if (invalidKeys.includes('unstable_revalidate')) {
throw new Error(UNSTABLE_REVALIDATE_RENAME_ERROR)
}
......@@ -546,41 +546,41 @@ export async function renderToHTML(
)
}
if (typeof data.unstable_revalidate === 'number') {
if (!Number.isInteger(data.unstable_revalidate)) {
if (typeof data.revalidate === 'number') {
if (!Number.isInteger(data.revalidate)) {
throw new Error(
`A page's revalidate option must be seconds expressed as a natural number. Mixed numbers, such as '${data.unstable_revalidate}', cannot be used.` +
`A page's revalidate option must be seconds expressed as a natural number. Mixed numbers, such as '${data.revalidate}', cannot be used.` +
`\nTry changing the value to '${Math.ceil(
data.unstable_revalidate
data.revalidate
)}' or using \`Math.ceil()\` if you're computing the value.`
)
} else if (data.unstable_revalidate <= 0) {
} else if (data.revalidate <= 0) {
throw new Error(
`A page's revalidate option can not be less than or equal to zero. A revalidate option of zero means to revalidate after _every_ request, and implies stale data cannot be tolerated.` +
`\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.unstable_revalidate > 31536000) {
} 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. 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.unstable_revalidate === true) {
} 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.unstable_revalidate = 1
data.revalidate = 1
} else {
// By default, we never revalidate.
data.unstable_revalidate = false
data.revalidate = false
}
props.pageProps = Object.assign({}, props.pageProps, data.props)
// pass up revalidate and props for export
// TODO: change this to a different passing mechanism
;(renderOpts as any).revalidate = data.unstable_revalidate
;(renderOpts as any).revalidate = data.revalidate
;(renderOpts as any).pageData = props
}
......
......@@ -80,7 +80,7 @@ export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
export type GetStaticPropsResult<P> = {
props: P
unstable_revalidate?: number | boolean
revalidate?: number | boolean
}
export type GetStaticProps<
......
......@@ -34,7 +34,7 @@ export async function getStaticProps() {
// Do not pass any sensitive values here as they will
// be made PUBLICLY available in `pageProps`
props: { env: items },
unstable_revalidate: 1,
revalidate: 1,
}
}
......
......@@ -34,7 +34,7 @@ export async function getStaticProps() {
// Do not pass any sensitive values here as they will
// be made PUBLICLY available in `pageProps`
props: { env: items },
unstable_revalidate: 1,
revalidate: 1,
}
}
......
......@@ -18,7 +18,7 @@ export async function getStaticProps() {
world: text,
time: new Date().getTime(),
},
unstable_revalidate: true,
revalidate: true,
}
}
......
......@@ -18,7 +18,7 @@ export async function getStaticProps({ params }) {
comment: params.comment,
time: new Date().getTime(),
},
unstable_revalidate: 2,
revalidate: 2,
}
}
......
......@@ -42,7 +42,7 @@ export async function getStaticProps({ params }) {
post: params.post,
time: (await import('perf_hooks')).performance.now(),
},
unstable_revalidate: 10,
revalidate: 10,
}
}
......
......@@ -7,7 +7,7 @@ export async function getStaticProps() {
slugs: ['post-1', 'post-2'],
time: (await import('perf_hooks')).performance.now(),
},
unstable_revalidate: 10,
revalidate: 10,
}
}
......
......@@ -9,7 +9,7 @@ export async function getStaticProps({ params: { slug } }) {
props: {
slug,
},
unstable_revalidate: 1,
revalidate: 1,
}
}
......
......@@ -9,7 +9,7 @@ export async function getStaticProps({ params: { slug } }) {
props: {
slug,
},
unstable_revalidate: 1,
revalidate: 1,
}
}
......
......@@ -20,7 +20,7 @@ export async function getStaticProps({ params }) {
random: Math.random(),
time: (await import('perf_hooks')).performance.now(),
},
unstable_revalidate: 1,
revalidate: 1,
}
}
......
......@@ -5,7 +5,7 @@ export async function getStaticProps() {
return {
props: { world: 'world', time: new Date().getTime() },
// bad-prop
unstable_revalidate: 1,
revalidate: 1,
}
}
......
......@@ -10,7 +10,7 @@ export async function getStaticProps({ params }) {
time: new Date().getTime(),
random: Math.random(),
},
unstable_revalidate: false,
revalidate: false,
}
}
......
......@@ -11,7 +11,7 @@ export async function getStaticProps({ params }) {
user: params.user,
time: (await import('perf_hooks')).performance.now(),
},
unstable_revalidate: 10,
revalidate: 10,
}
}
......
......@@ -20,7 +20,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async ({
}) => {
return {
props: { data: params!.slug },
unstable_revalidate: false,
revalidate: false,
}
}
......
......@@ -3,7 +3,7 @@ import { InferGetStaticPropsType } from 'next'
export const getStaticProps = async () => {
return {
props: { data: ['hello', 'world'] },
unstable_revalidate: false,
revalidate: false,
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册