Skip to content

Commit

Permalink
Rename unstable GSP field (#11602)
Browse files Browse the repository at this point in the history
* Rename unstable GSP revalidate field

* Update error message

* Tweak error message some more

* Apply suggestions from code review

Co-Authored-By: Joe Haddad <[email protected]>

Co-authored-by: Joe Haddad <[email protected]>
  • Loading branch information
ijjk and Timer authored Apr 2, 2020
1 parent bf96337 commit 97a6b64
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 24 deletions.
6 changes: 6 additions & 0 deletions packages/next/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ export const SERVER_PROPS_SSG_CONFLICT = `You can not use getStaticProps with ge
export const PAGES_404_GET_INITIAL_PROPS_ERROR = `\`pages/404\` can not have getInitialProps/getServerSideProps, https://err.sh/next.js/404-get-initial-props`

export const SERVER_PROPS_EXPORT_ERROR = `pages with \`getServerSideProps\` can not be exported. See more info here: https://err.sh/next.js/gss-export`

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'
27 changes: 16 additions & 11 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
SERVER_PROPS_SSG_CONFLICT,
SSG_GET_INITIAL_PROPS_CONFLICT,
UNSTABLE_REVALIDATE_RENAME_ERROR,
} from '../../lib/constants'
import { isSerializableProps } from '../../lib/is-serializable-props'
import { isInAmpMode } from '../lib/amp'
Expand Down Expand Up @@ -518,9 +519,13 @@ export async function renderToHTML(
}

const invalidKeys = Object.keys(data).filter(
key => key !== 'revalidate' && key !== 'props'
key => key !== 'unstable_revalidate' && key !== 'props'
)

if (invalidKeys.includes('revalidate')) {
throw new Error(UNSTABLE_REVALIDATE_RENAME_ERROR)
}

if (invalidKeys.length) {
throw new Error(invalidKeysMsg('getStaticProps', invalidKeys))
}
Expand All @@ -535,41 +540,41 @@ export async function renderToHTML(
)
}

if (typeof data.revalidate === 'number') {
if (!Number.isInteger(data.revalidate)) {
if (typeof data.unstable_revalidate === 'number') {
if (!Number.isInteger(data.unstable_revalidate)) {
throw new Error(
`A page's revalidate option must be seconds expressed as a natural number. Mixed numbers, such as '${data.revalidate}', cannot be used.` +
`A page's revalidate option must be seconds expressed as a natural number. Mixed numbers, such as '${data.unstable_revalidate}', cannot be used.` +
`\nTry changing the value to '${Math.ceil(
data.revalidate
data.unstable_revalidate
)}' or using \`Math.ceil()\` if you're computing the value.`
)
} else if (data.revalidate <= 0) {
} else if (data.unstable_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.revalidate > 31536000) {
} else if (data.unstable_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.revalidate === true) {
} else if (data.unstable_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
data.unstable_revalidate = 1
} else {
// By default, we never revalidate.
data.revalidate = false
data.unstable_revalidate = false
}

props.pageProps = data.props
// pass up revalidate and props for export
// TODO: change this to a different passing mechanism
;(renderOpts as any).revalidate = data.revalidate
;(renderOpts as any).revalidate = data.unstable_revalidate
;(renderOpts as any).pageData = props
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export type GetStaticProps<
previewData?: any
}) => Promise<{
props: P
revalidate?: number | boolean
unstable_revalidate?: number | boolean
}>

export type GetStaticPaths<
Expand Down
2 changes: 1 addition & 1 deletion test/integration/env-config/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function getStaticProps() {
// Do not pass any sensitive values here as they will
// be made PUBLICLY available in `pageProps`
props: { env: items },
revalidate: 1,
unstable_revalidate: 1,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/env-config/app/pages/some-ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function getStaticProps() {
// Do not pass any sensitive values here as they will
// be made PUBLICLY available in `pageProps`
props: { env: items },
revalidate: 1,
unstable_revalidate: 1,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/another/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getStaticProps() {
world: text,
time: new Date().getTime(),
},
revalidate: true,
unstable_revalidate: true,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/blog/[post]/[comment].js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getStaticProps({ params }) {
comment: params.comment,
time: new Date().getTime(),
},
revalidate: 2,
unstable_revalidate: 2,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/blog/[post]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function getStaticProps({ params }) {
post: params.post,
time: (await import('perf_hooks')).performance.now(),
},
revalidate: 10,
unstable_revalidate: 10,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function getStaticProps() {
slugs: ['post-1', 'post-2'],
time: (await import('perf_hooks')).performance.now(),
},
revalidate: 10,
unstable_revalidate: 10,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function getStaticProps({ params: { slug } }) {
props: {
slug,
},
revalidate: 1,
unstable_revalidate: 1,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/catchall/[...slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function getStaticProps({ params: { slug } }) {
props: {
slug,
},
revalidate: 1,
unstable_revalidate: 1,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function getStaticProps() {
return {
props: { world: 'world', time: new Date().getTime() },
// bad-prop
revalidate: 1,
unstable_revalidate: 1,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/something.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function getStaticProps({ params }) {
time: new Date().getTime(),
random: Math.random(),
},
revalidate: false,
unstable_revalidate: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/user/[user]/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function getStaticProps({ params }) {
user: params.user,
time: (await import('perf_hooks')).performance.now(),
},
revalidate: 10,
unstable_revalidate: 10,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/typescript/pages/ssg/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async ({
}) => {
return {
props: { data: params!.slug },
revalidate: false,
unstable_revalidate: false,
}
}

Expand Down

0 comments on commit 97a6b64

Please sign in to comment.