Skip to content

Commit 6f1f644

Browse files
committed
fix: catch Netlify env generateStaticParams errors
1 parent 245589b commit 6f1f644

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

app/[locale]/[...slug]/page.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,24 @@ export default async function Page({
9898
}
9999

100100
export async function generateStaticParams() {
101-
const slugs = await getPostSlugs("/")
102-
103-
return LOCALES_CODES.flatMap((locale) =>
104-
slugs.map((slug) => ({
105-
slug: slug.split("/").slice(1),
106-
locale,
107-
}))
108-
)
101+
try {
102+
const slugs = await getPostSlugs("/")
103+
104+
return LOCALES_CODES.flatMap((locale) =>
105+
slugs.map((slug) => ({
106+
slug: slug.split("/").slice(1),
107+
locale,
108+
}))
109+
)
110+
} catch (error) {
111+
// If content directory doesn't exist (e.g., in Netlify serverless environment),
112+
// return empty array to allow ISR to handle all routes dynamically
113+
console.warn(
114+
"Content directory not found, enabling full dynamic routing:",
115+
error
116+
)
117+
return []
118+
}
109119
}
110120

111121
export async function generateMetadata({

src/lib/utils/md.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,54 @@ export const getPostSlugs = async (dir: string, filterRegex?: RegExp) => {
2121
const contentRoot = getContentRoot()
2222
const _dir = join(contentRoot, dir)
2323

24-
// Get an array of all files and directories in the passed directory using `fs.readdirSync`
25-
const dirContents = await fsp.readdir(_dir)
26-
27-
const files: string[] = []
28-
29-
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
30-
for (const fileOrDir of dirContents) {
31-
// file = "about", "bridges".... "translations" (<-- skip that one)...
32-
const path = join(_dir, fileOrDir)
33-
34-
const stats = await fsp.stat(path)
35-
if (stats.isDirectory()) {
36-
// Skip nested translations directory
37-
if (fileOrDir === "translations") continue
38-
// If it is a directory, recursively call the `getPostSlugs` function with the
39-
// directory path and the files array
40-
const nestedDir = join(dir, fileOrDir)
41-
42-
const nestedFiles = await getPostSlugs(nestedDir, filterRegex)
43-
files.push(...nestedFiles)
44-
continue
45-
}
24+
try {
25+
// Get an array of all files and directories in the passed directory using `fs.readdirSync`
26+
const dirContents = await fsp.readdir(_dir)
27+
28+
const files: string[] = []
29+
30+
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
31+
for (const fileOrDir of dirContents) {
32+
// file = "about", "bridges".... "translations" (<-- skip that one)...
33+
const path = join(_dir, fileOrDir)
34+
35+
const stats = await fsp.stat(path)
36+
if (stats.isDirectory()) {
37+
// Skip nested translations directory
38+
if (fileOrDir === "translations") continue
39+
// If it is a directory, recursively call the `getPostSlugs` function with the
40+
// directory path and the files array
41+
const nestedDir = join(dir, fileOrDir)
42+
43+
const nestedFiles = await getPostSlugs(nestedDir, filterRegex)
44+
files.push(...nestedFiles)
45+
continue
46+
}
4647

47-
if (filterRegex?.test(path)) continue
48+
if (filterRegex?.test(path)) continue
4849

49-
// If the current file is not a markdown file, skip it
50-
if (extname(path) !== ".md") continue
50+
// If the current file is not a markdown file, skip it
51+
if (extname(path) !== ".md") continue
5152

52-
const sanitizedPath = toPosixPath(
53-
path.replace(contentRoot, "").replace("/index.md", "")
54-
)
53+
const sanitizedPath = toPosixPath(
54+
path.replace(contentRoot, "").replace("/index.md", "")
55+
)
5556

56-
files.push(sanitizedPath)
57-
}
57+
files.push(sanitizedPath)
58+
}
5859

59-
return files
60+
return files
61+
} catch (error) {
62+
// If directory doesn't exist (e.g., in Netlify serverless environment), return empty array
63+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
64+
console.warn(
65+
`Content directory ${_dir} not found, returning empty slug list`
66+
)
67+
return []
68+
}
69+
// Re-throw other errors
70+
throw error
71+
}
6072
}
6173

6274
export const getTutorialsData = (locale: string): ITutorial[] => {

0 commit comments

Comments
 (0)