This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Use getRevalidateTime lib for global revalidate vals (#2490)
- Loading branch information
Showing
4 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { convertPrimitiveToNumber, previewRevalidateTime as fallback } from '.' | ||
|
||
describe('convertPrimitiveToNumber', () => { | ||
it('coerces a string with an integer into number', () => { | ||
const time = convertPrimitiveToNumber('52') | ||
expect(time).toBe(52) | ||
}) | ||
it('returns fallback if string does not include integer', () => { | ||
const time = convertPrimitiveToNumber('imastringwithoutaninteger') | ||
expect(time).toBe(fallback) | ||
}) | ||
it('returns number if type is number', () => { | ||
const time = convertPrimitiveToNumber(23) | ||
expect(time).toBe(23) | ||
}) | ||
it('returns fallback if falsy', () => { | ||
const time = convertPrimitiveToNumber(undefined) | ||
expect(time).toBe(fallback) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Utility function that converts a primitive into a number. If it can't convert | ||
* the argument into a number, it will return a fallback number. | ||
* | ||
* @param primitive - The primitive for conversion | ||
* @param fallback - Fallback return value if the primitive can't be converted into a number | ||
* | ||
* @returns The converted number or the fallback | ||
*/ | ||
|
||
export const convertPrimitiveToNumber = ( | ||
primitive: string | number | boolean, | ||
fallback = 10 | ||
): number => { | ||
if (!primitive) return fallback | ||
|
||
const isNumber = typeof primitive === 'number' | ||
const isStr = typeof primitive === 'string' | ||
const parsesToInteger = isStr ? !!Number.parseInt(primitive, 10) : false | ||
|
||
if (isNumber) { | ||
return primitive | ||
} else if (isStr && parsesToInteger) { | ||
return Number.parseInt(primitive, 10) | ||
} else { | ||
return fallback | ||
} | ||
} | ||
|
||
// The shorter revalidate interval allows authors allows for | ||
// near-real-time ISR for content authors when drafting | ||
// content via Preview Mode | ||
export const previewRevalidateTime = 10 | ||
|
||
/** | ||
* Wrapper function for `convertPrimitiveToNumber` that converts the value of | ||
* `process.env.GLOBAL_REVALIDATE` into a number. This can then be used to set the | ||
* `revalidate` key in `getStaticProps`. Since `process.env` properties are | ||
* typed as strings but `getStaticProps` expects `revalidate`to be a number, we | ||
* need to convert the type into a number. | ||
* | ||
* @returns The converted number or the fallback | ||
*/ | ||
|
||
export const getRevalidateTime = () => { | ||
return convertPrimitiveToNumber( | ||
process.env.GLOBAL_REVALIDATE, | ||
previewRevalidateTime | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ca8da86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
terraform-website – ./
terraform-website-git-master-hashicorp.vercel.app
terraform-website-hashicorp.vercel.app
terraform-website.vercel.app
www.terraform.io
terraform.io