Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/brave-windows-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hashicorp/react-docs-page': minor
---

Create and export new `getStaticGenerationFunctions` types
77 changes: 54 additions & 23 deletions packages/docs-page/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { GetStaticPaths, GetStaticProps, GetStaticPathsResult } from 'next'
import { ContentApiError } from '../content-api'
import FileSystemLoader from './loaders/file-system'
import RemoteContentLoader from './loaders/remote-content'
import FileSystemLoader, { FileSystemLoaderOpts } from './loaders/file-system'
import RemoteContentLoader, {
RemoteContentLoaderOpts,
} from './loaders/remote-content'
import { DataLoader } from './loaders/types'

// We currently export most utilities individually,
Expand All @@ -14,32 +16,61 @@ export { getPathsFromNavData } from './get-paths-from-nav-data'
export { validateNavData } from './validate-nav-data'
export { default as validateFilePaths } from '@hashicorp/react-docs-sidenav/utils/validate-file-paths'

interface BaseOpts {
/**
* Shared config fields for both the `remote` and `fs` strategies.
* - See {@link RemoteOptions} & {@link FsOptions} for more details.
*/
export interface BaseOpts {
fallback?: GetStaticPathsResult['fallback']
revalidate?: number
revalidate?: number | boolean
product: string
scope?: Record<string, $TSFixMe>
}

export function getStaticGenerationFunctions(
opts:
| ({
basePath: string
strategy: 'remote'
} & BaseOpts &
Partial<ConstructorParameters<typeof RemoteContentLoader>[0]>)
| ({
localContentDir: string
navDataFile: string
strategy: 'fs'
/** Optional location of the partials directory
* relative to process.cwd().
* Passed to our resolveIncludes plugin.
* Defaults to "content/partials". */
localPartialsDir?: string
} & BaseOpts &
Partial<ConstructorParameters<typeof FileSystemLoader>[0]>)
): {
/**
* Config object that is passed to {@link getStaticGenerationFunctions}
* when using the `remote` strategy
*/
export type RemoteOptions = {
basePath: string
strategy: 'remote'
} & BaseOpts &
Partial<RemoteContentLoaderOpts>

/**
* Config object that is passed to {@link getStaticGenerationFunctions}
* when using the `fs` strategy
*/
export type FsOptions = {
localContentDir: string
navDataFile: string
strategy: 'fs'
/** Optional location of the partials directory
* relative to process.cwd().
* Passed to our resolveIncludes plugin.
* Defaults to "content/partials". */
localPartialsDir?: string
} & BaseOpts &
Partial<FileSystemLoaderOpts>

/**
*
* @usage
*
* ```typescript
* declare const remoteOpts: RemoteOptions
* declare const fsOpts: FsOptions
*
* declare const useFs: boolean
*
* const { getStaticPaths, getStaticProps } = getStaticGenerationFunctions(
* useFs ? fsOpts : remoteOpts
* )
*
* export { getStaticPaths, getStaticProps }
* ```
*/
export function getStaticGenerationFunctions(opts: RemoteOptions | FsOptions): {
getStaticPaths: GetStaticPaths
getStaticProps: GetStaticProps
} {
Expand Down
5 changes: 4 additions & 1 deletion packages/docs-page/server/loaders/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { getNodeFromPath } from '../get-node-from-path'

import { DataLoader, DataLoaderOpts } from './types'

interface FileSystemLoaderOpts extends DataLoaderOpts {
/**
* Constructor object for {@link FileSystemLoader}
*/
export interface FileSystemLoaderOpts extends DataLoaderOpts {
navDataFile: string
localContentDir: string
mainBranch?: string // = 'main',
Expand Down
5 changes: 4 additions & 1 deletion packages/docs-page/server/loaders/remote-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { DEFAULT_PARAM_ID } from '../consts'
import { DataLoader, DataLoaderOpts } from './types'
import { getPathsFromNavData } from '../get-paths-from-nav-data'

interface RemoteContentLoaderOpts extends DataLoaderOpts {
/**
* Constructor object for {@link RemoteContentLoader}
*/
export interface RemoteContentLoaderOpts extends DataLoaderOpts {
basePath: string
/**
* In most cases, `basePath` should suffice when resolving nav-data, because
Expand Down