Skip to content
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
41 changes: 27 additions & 14 deletions src/components/Contributors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@ type AllContributorsRc = {
contributors: (Contributor & { contributions?: string[] })[]
}

// Read `.all-contributorsrc` (bot-maintained) once at module load.
const raw = readFileSync(join(process.cwd(), ".all-contributorsrc"), "utf-8")
const { contributors: rawContributors } = JSON.parse(raw) as AllContributorsRc

// Trim to the fields the card actually renders and shuffle once per SSG worker.
const shuffledContributors: Contributor[] = shuffle(
rawContributors.map(({ login, name, avatar_url, profile }) => ({
login,
name,
avatar_url,
profile,
}))
)
let shuffledContributors: Contributor[] | undefined

// Read `.all-contributorsrc` (bot-maintained) on first render and cache.
// Deferred (not module-load) so non-Next bundlers like Storybook can import
// this module without evaluating Node's `fs`.
const getShuffledContributors = (): Contributor[] => {
if (!shuffledContributors) {
const raw = readFileSync(
join(process.cwd(), ".all-contributorsrc"),
"utf-8"
)
const { contributors: rawContributors } = JSON.parse(
raw
) as AllContributorsRc

shuffledContributors = shuffle(
rawContributors.map(({ login, name, avatar_url, profile }) => ({
login,
name,
avatar_url,
profile,
}))
)
}
return shuffledContributors
}

const Contributors = ({ contributors }: ContributorsProps = {}) => (
<ContributorsView contributors={contributors ?? shuffledContributors} />
<ContributorsView contributors={contributors ?? getShuffledContributors()} />
)

export default Contributors
Loading