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

feat: add prebuild script for building sitemaps for builds #2664

Merged
merged 15 commits into from
Jan 15, 2025
Merged
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"generate:tutorial-map": "hc-tools ./scripts/generate-tutorial-map.ts",
"lint": "next-hashicorp lint",
"lint:stylelint": "npx stylelint '**/*.css'",
"prebuild": "hc-tools ./scripts/generate-tutorial-variant-map.ts && hc-tools ./scripts/extract-hvd-content.ts",
"prebuild": "hc-tools ./scripts/generate-tutorial-variant-map.ts && hc-tools ./scripts/extract-hvd-content.ts && hc-tools ./scripts/build-sitemap.ts",
"prepare": "simple-git-hooks",
"prestart": "hc-tools ./scripts/generate-tutorial-variant-map.ts && hc-tools ./scripts/extract-hvd-content.ts",
"prettier:check": "prettier --check .",
"prettier:write": "prettier --write .",
"postbuild": "hc-tools ./scripts/capture-build-metrics.ts dev-portal && next-sitemap && hc-tools ./scripts/upload-source-maps.ts",
"postbuild": "hc-tools ./scripts/capture-build-metrics.ts dev-portal && hc-tools ./scripts/upload-source-maps.ts",
"rewrite-docs-content-links": "hc-tools ./scripts/docs-content-link-rewrites/rewrite-links.ts",
"sitemap": "next-sitemap",
"start:local-preview": "./scripts/content-repo-preview/start.sh",
Expand Down
32 changes: 32 additions & 0 deletions scripts/build-sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'path'
import { allDocsFields, allTutorialsFields } from 'lib/sitemap'
import { unflatten } from 'flat'
import { getHashiConfig } from '../config'

const env = process.env.HASHI_ENV || 'development'
const envConfigPath = path.join(process.cwd(), 'config', `${env}.json`)

const __config = unflatten(getHashiConfig(envConfigPath))

/**
* This script is run as part of the Build sitemap github action that runs on PRs and
* pushes to main. To run this script manually, you can run the following command:
* `npx hc-tools ./scripts/build-sitemap.ts`
*/
async function main() {
try {
await allDocsFields(__config)
} catch (error) {
console.error('Error building docs sitemap: ', error)
process.exit(-1)
}

try {
await allTutorialsFields(__config)
} catch (error) {
console.error('Error building tutorials sitemaps: ', error)
process.exit(-1)
}
}

main()
4 changes: 2 additions & 2 deletions src/lib/__tests__/docs-content-fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('allDocsFields', () => {
json: vi.fn().mockResolvedValue({ result: mockContentAPIDocsResult }),
})

const result = await allDocsFields()
const result = await allDocsFields(__config)

expect(fetch).toHaveBeenCalledWith(
'https://content-api.example.com/api/all-docs-paths'
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('allDocsFields', () => {
json: vi.fn().mockResolvedValue({ result: mockUDRDocsResult }),
})

const result = await allDocsFields()
const result = await allDocsFields(__config)

expect(fetch).toHaveBeenCalledWith(
'https://content-api.example.com/api/all-docs-paths?filterOut=repo1&filterOut=repo2'
Expand Down
30 changes: 18 additions & 12 deletions src/lib/sitemap/docs-content-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import { makeSitemapField } from './helpers'

export async function allDocsFields() {
export async function allDocsFields(config: typeof __config) {
// If there are docs that have been migrated to the unified docs repo, get the paths for those docs
// and merge them with the paths for the docs that haven't been migrated from the content API
if (
process.env.HASHI_ENV === 'unified-docs-sandbox' &&
__config.flags?.unified_docs_migrated_repos.length > 0
config.flags?.unified_docs_migrated_repos.length > 0
) {
const contentAPIFilter = __config.flags.unified_docs_migrated_repos.map(
const contentAPIFilter = config.flags.unified_docs_migrated_repos.map(
(repo) => {
return `filterOut=${repo}`
}
Expand All @@ -23,7 +23,7 @@ export async function allDocsFields() {
)
const { result: contentAPIDocsResult } = await getContentAPIDocsPaths.json()

const UDRFilter = __config.flags.unified_docs_migrated_repos.map((repo) => {
const UDRFilter = config.flags.unified_docs_migrated_repos.map((repo) => {
return `products=${repo}`
})
const UDRFilterString = UDRFilter.join('&')
Expand All @@ -34,10 +34,13 @@ export async function allDocsFields() {

const allDocsData = [...contentAPIDocsResult, ...udrDocsResult]
return allDocsData.map((page: { path: string; created_at: string }) =>
makeSitemapField({
slug: `${page.path}`,
lastmodDate: page.created_at,
})
makeSitemapField(
{
slug: `${page.path}`,
lastmodDate: page.created_at,
},
config
)
)
}

Expand All @@ -48,9 +51,12 @@ export async function allDocsFields() {

return contentAPIDocsResult.map(
(page: { path: string; created_at: string }) =>
makeSitemapField({
slug: `${page.path}`,
lastmodDate: page.created_at,
})
makeSitemapField(
{
slug: `${page.path}`,
lastmodDate: page.created_at,
},
config
)
)
}
12 changes: 5 additions & 7 deletions src/lib/sitemap/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export const determinePriority = (slug: string): SitemapPriority => {
}
}

export const makeSitemapField = ({
slug,
lastmodDate,
priority,
changefreq,
}: SitemapFactoryInput): SitemapElement => {
export const makeSitemapField = (
{ slug, lastmodDate, priority, changefreq }: SitemapFactoryInput,
config: typeof __config
): SitemapElement => {
return {
loc: new URL(slug, __config.dev_dot.canonical_base_url).toString(),
loc: new URL(slug, config.dev_dot.canonical_base_url).toString(),
lastmod: lastmodDate ?? new Date(Date.now()).toISOString(),
priority: priority ?? determinePriority(slug),
changefreq: changefreq ?? 'daily',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/sitemap/tutorials-content-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ async function getCollectionPaths() {
})
}

export async function allTutorialsFields() {
export async function allTutorialsFields(config: typeof __config) {
const landingSlugs = getTutorialLandingPaths()
const collectionSlugs = await getCollectionPaths()
const tutorialSlugs = Object.values(tutorialMap)
return [...landingSlugs, ...collectionSlugs, ...tutorialSlugs].map(
(slug: string) => makeSitemapField({ slug })
(slug: string) => makeSitemapField({ slug }, config)
)
}
12 changes: 10 additions & 2 deletions src/pages/server-sitemap.xml/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
import { getServerSideSitemap } from 'next-sitemap'
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import { allDocsFields, allTutorialsFields } from 'lib/sitemap'
import path from 'path'
import { unflatten } from 'flat'
import { getHashiConfig } from '../../../config'

const env = process.env.HASHI_ENV || 'development'
const envConfigPath = path.join(process.cwd(), 'config', `${env}.json`)

const config = unflatten(getHashiConfig(envConfigPath))

export const getServerSideProps: GetServerSideProps = async (
ctx: GetServerSidePropsContext
) => {
try {
// returns an array of docs content sitemap fields per slug
const docsFields = await allDocsFields()
const docsFields = await allDocsFields(config)
// returns an array of tutorials content sitemap fields per slug
const tutorialsFields = await allTutorialsFields()
const tutorialsFields = await allTutorialsFields(config)

return getServerSideSitemap(ctx, [...docsFields, ...tutorialsFields])
} catch (error) {
Expand Down
Loading