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

Enforce natural numbers for SPR #8882

Merged
merged 1 commit into from
Sep 27, 2019
Merged
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
Enforce natural numbers for SPR
Per the HTTP specification, only natural numbers may be used for caching durations. We should enforce this with a really helpful error message.
Timer committed Sep 27, 2019
commit eca23eb4c3dd6de77d97d50f198c47c36f71266a
17 changes: 14 additions & 3 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
@@ -394,14 +394,25 @@ export async function renderToHTML(
}

if (typeof data.revalidate === 'number') {
if (data.revalidate < 0) {
if (!Number.isInteger(data.revalidate)) {
throw new Error(
`A page's revalidate option can not be less than zero. A revalidate option of zero means to revalidate _after_ every request. To never revalidate, you can set revalidate to \`false\` (only ran once at build-time).`
`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.revalidate
)}' or using \`Math.round()\` if you're computing the value.`
)
} else if (data.revalidate < 0) {
throw new Error(
`A page's revalidate option can not be less than zero. A revalidate option of zero means to revalidate _after_ every request.` +
`\nTo never revalidate, you can set revalidate to \`false\` (only ran once at build-time).`
)
} 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\`!`
`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\`!`
)
}
}