Skip to content

Commit f860711

Browse files
Load redirects from content dir (#689)
* Load redirects from content dir * Use NODE_ENV
1 parent 90f9796 commit f860711

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

app/api/content/[productSlug]/redirects/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export async function GET(request: Request, { params }: { params: GetParams }) {
3939
'redirects.jsonc',
4040
]
4141

42-
const readFileResult = await findFileWithMetadata(filePath, versionMetadata)
42+
const readFileResult = await findFileWithMetadata(filePath, versionMetadata, {
43+
loadFromContentDir: process.env.NODE_ENV === 'development',
44+
})
4345
if (!readFileResult.ok) {
4446
return new Response('Not found', { status: 404 })
4547
}

app/utils/file.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import grayMatter from 'gray-matter'
77
import { parse as jsoncParse } from 'jsonc-parser'
8+
import { readFile } from 'fs/promises'
89

910
import { Err, Ok, Result } from './result'
1011
import type { ProductVersionMetadata } from './contentVersions'
@@ -27,13 +28,22 @@ const headers = process.env.VERCEL_URL
2728
export const findFileWithMetadata = async (
2829
filePath: string[],
2930
versionMetaData: ProductVersionMetadata,
31+
options: {
32+
loadFromContentDir?: boolean
33+
} = { loadFromContentDir: false },
3034
) => {
3135
const newFilePath = ifNeededAddReleaseStageToPath(
3236
filePath,
3337
versionMetaData.releaseStage,
3438
)
3539

3640
try {
41+
if (options.loadFromContentDir) {
42+
const filePathString = joinFilePath(newFilePath)
43+
const fileContent = await readFile(filePathString, 'utf-8')
44+
return Ok(fileContent)
45+
}
46+
3747
const res = await fetch(`${SELF_URL}/${newFilePath.join('/')}`, {
3848
cache: 'no-cache',
3949
headers,
@@ -47,7 +57,9 @@ export const findFileWithMetadata = async (
4757

4858
return Ok(text)
4959
} catch {
50-
return Err(`Failed to read file at path: ${newFilePath.join('/')}`)
60+
return Err(
61+
`Failed to read file at path: ${newFilePath.join('/')}, with options: ${JSON.stringify(options, null, 2)}`,
62+
)
5163
}
5264
}
5365

0 commit comments

Comments
 (0)