Unverified Commit 690fc36c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update revalidate checks (#19796)

This ensures we show an error for string values for `revalidate` since we currently don't handle these and instead it results in `revalidate: false` being used. This also adds tests for our `revalidate` checks to ensure they are triggered correctly and not triggered for valid values. 

x-ref: https://github.com/vercel/next.js/issues/20310
 
parent 118588b0
...@@ -708,32 +708,46 @@ export async function renderToHTML( ...@@ -708,32 +708,46 @@ export async function renderToHTML(
) )
} }
if ('revalidate' in data && typeof data.revalidate === 'number') { if ('revalidate' in data) {
if (!Number.isInteger(data.revalidate)) { if (typeof data.revalidate === 'number') {
throw new Error( if (!Number.isInteger(data.revalidate)) {
`A page's revalidate option must be seconds expressed as a natural number. Mixed numbers, such as '${data.revalidate}', cannot be used.` + throw new Error(
`\nTry changing the value to '${Math.ceil( `A page's revalidate option must be seconds expressed as a natural number for ${req.url}. Mixed numbers, such as '${data.revalidate}', cannot be used.` +
data.revalidate `\nTry changing the value to '${Math.ceil(
)}' or using \`Math.ceil()\` if you're computing the value.` data.revalidate
) )}' or using \`Math.ceil()\` if you're computing the value.`
} else if (data.revalidate <= 0) { )
} else if (data.revalidate <= 0) {
throw new Error(
`A page's revalidate option can not be less than or equal to zero for ${req.url}. 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) {
// 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 === 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
} else if (
data.revalidate === false ||
typeof data.revalidate === 'undefined'
) {
// By default, we never revalidate.
data.revalidate = false
} else {
throw new Error( 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.` + `A page's revalidate option must be seconds expressed as a natural number. Mixed numbers and strings cannot be used. Received '${JSON.stringify(
`\n\nTo never revalidate, you can set revalidate to \`false\` (only ran once at build-time).` + data.revalidate
`\nTo revalidate as soon as possible, you can set the value to \`1\`.` )}' for ${req.url}`
)
} 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 ('revalidate' in data && 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
} else { } else {
// By default, we never revalidate. // By default, we never revalidate.
;(data as any).revalidate = false ;(data as any).revalidate = false
......
export default function Page() {
return (
<>
<p>a-ok</p>
</>
)
}
export const getStaticProps = () => {
return {
props: {
hello: 'world',
},
revalidate: 1,
}
}
/* eslint-env jest */
import { join } from 'path'
import {
findPort,
killApp,
File,
launchApp,
renderViaHTTP,
check,
waitFor,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 1)
const appDir = join(__dirname, '..')
const pageFile = new File(join(appDir, 'pages/ssg.js'))
let app
let appPort
describe('Invalid revalidate values', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await pageFile.restore()
})
it('should not show error initially', async () => {
const html = await renderViaHTTP(appPort, '/ssg')
expect(html).toContain('a-ok')
})
it('should not show error for false revalidate value', async () => {
pageFile.replace('revalidate: 1', 'revalidate: false')
try {
for (let i = 0; i < 3; i++) {
await waitFor(1000)
const html = await renderViaHTTP(appPort, '/ssg')
expect(html).toContain('a-ok')
}
} finally {
pageFile.restore()
}
})
it('should not show error for true revalidate value', async () => {
pageFile.replace('revalidate: 1', 'revalidate: true')
try {
for (let i = 0; i < 3; i++) {
await waitFor(1000)
const html = await renderViaHTTP(appPort, '/ssg')
expect(html).toContain('a-ok')
}
} finally {
pageFile.restore()
}
})
it('should show error for string revalidate value', async () => {
pageFile.replace('revalidate: 1', 'revalidate: "1"')
try {
await check(
() => renderViaHTTP(appPort, '/ssg'),
/A page's revalidate option must be seconds expressed as a natural number. Mixed numbers and strings cannot be used. Received/
)
} finally {
pageFile.restore()
}
})
it('should show error for null revalidate value', async () => {
pageFile.replace('revalidate: 1', 'revalidate: null')
try {
await check(
() => renderViaHTTP(appPort, '/ssg'),
/A page's revalidate option must be seconds expressed as a natural number. Mixed numbers and strings cannot be used. Received/
)
} finally {
pageFile.restore()
}
})
it('should show error for float revalidate value', async () => {
pageFile.replace('revalidate: 1', 'revalidate: 1.1')
try {
await check(
() => renderViaHTTP(appPort, '/ssg'),
/A page's revalidate option must be seconds expressed as a natural number for \/ssg. Mixed numbers, such as/
)
} finally {
pageFile.restore()
}
})
})
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