Skip to content

Commit

Permalink
Harden SSG for when HVD docs do not exist (#2260)
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenSandwich authored Dec 5, 2023
1 parent 7b1f941 commit 011b4d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/pages/validated-designs/[...slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import {
export async function getStaticPaths() {
const pagePaths = getHvdCategoryGroupsPaths()

if (!pagePaths) {
return {
paths: [],
fallback: false,
}
}

return {
paths: [
...pagePaths.map((path: string[]) => ({
Expand All @@ -38,6 +45,12 @@ export async function getStaticProps(context) {
const slugs = context.params.slug
const props = await getHvdGuidePropsFromSlugs(slugs)

if (!props) {
return {
notFound: true,
}
}

return {
props,
}
Expand Down
24 changes: 21 additions & 3 deletions src/views/validated-designs/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function loadMetadata(path: string): { title: string; description: string } {
}
}

export function getHvdCategoryGroups(): HvdCategoryGroup[] {
export function getHvdCategoryGroups(): HvdCategoryGroup[] | null {
let hvdRepoContents

try {
Expand Down Expand Up @@ -61,7 +61,10 @@ export function getHvdCategoryGroups(): HvdCategoryGroup[] {
// throw e
console.error(e)
}

return null
}

if (!hvdRepoContents) {
return null
}
Expand Down Expand Up @@ -157,8 +160,12 @@ export function getHvdCategoryGroups(): HvdCategoryGroup[] {
return hvdCategoryGroups
}

export function getHvdCategoryGroupsPaths(): string[][] {
export function getHvdCategoryGroupsPaths(): string[][] | null {
const categoryGroups = getHvdCategoryGroups()

if (!categoryGroups) {
return null
}
// [[guide-slug], [guide-slug, page-slug]]
// e.g. [[terraform-operation-guide-adoption], [terraform-operation-guide-adoption, page-slug]]
const paths = []
Expand Down Expand Up @@ -191,9 +198,14 @@ function getMarkdownHeaders(markdown: string) {
}

export async function getHvdGuidePropsFromSlugs(
slugs: string[]
slugs: string[] | null
): Promise<ValidatedDesignsGuideProps> {
const categoryGroups = getHvdCategoryGroups()

if (!categoryGroups) {
return null
}

const [guideSlug, pageSlug] = slugs

const validatedDesignsGuideProps = {
Expand Down Expand Up @@ -246,6 +258,12 @@ export async function getHvdGuidePropsFromSlugs(

export function getHvdLandingProps(): ValidatedDesignsLandingProps | null {
// @TODO — the title and description should be sourced from the content repo
const categoryGroups = getHvdCategoryGroups()

if (!categoryGroups) {
return null
}

return {
title: 'HashiCorp Validated Designs',
description:
Expand Down

1 comment on commit 011b4d5

@vercel
Copy link

@vercel vercel bot commented on 011b4d5 Dec 5, 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.