Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
chore: Use getRevalidateTime lib for global revalidate vals (#2490)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnMod committed May 16, 2023
1 parent 0ff7471 commit ca8da86
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
20 changes: 20 additions & 0 deletions lib/get-revalidate-time/index.test.ts
@@ -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)
})
})
50 changes: 50 additions & 0 deletions lib/get-revalidate-time/index.ts
@@ -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
)
}
6 changes: 2 additions & 4 deletions pages/home/index.tsx
Expand Up @@ -3,6 +3,7 @@ import rivetQuery from '@hashicorp/platform-cms'
import homepageQuery from './query.graphql'
import { isInternalLink } from 'lib/utils'
import ReactHead from '@hashicorp/react-head'
import { getRevalidateTime } from 'lib/get-revalidate-time'
import IoHomeHeroAlt from 'components/io-home-hero-alt'
import IoHomeIntro from 'components/io-home-intro'
import IoHomeInPractice from 'components/io-home-in-practice'
Expand Down Expand Up @@ -180,9 +181,6 @@ export async function getStaticProps() {

return {
props: { data: terraformHomepage },
revalidate:
process.env.HASHI_ENV === 'production'
? process.env.GLOBAL_REVALIDATE
: 10,
revalidate: getRevalidateTime(),
}
}
6 changes: 2 additions & 4 deletions pages/use-cases/[slug].tsx
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import rivetQuery from '@hashicorp/platform-cms'
import useCasesQuery from './query.graphql'
import ReactHead from '@hashicorp/react-head'
import { getRevalidateTime } from 'lib/get-revalidate-time'
import IoUsecaseHero from 'components/io-usecase-hero'
import IoUsecaseSection from 'components/io-usecase-section'
import IoUsecaseCustomer from 'components/io-usecase-customer'
Expand Down Expand Up @@ -236,9 +237,6 @@ export async function getStaticProps({ params }) {
props: {
data: page,
},
revalidate:
process.env.HASHI_ENV === 'production'
? process.env.GLOBAL_REVALIDATE
: 10,
revalidate: getRevalidateTime(),
}
}

1 comment on commit ca8da86

@vercel
Copy link

@vercel vercel bot commented on ca8da86 May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.