@@ -21,42 +21,54 @@ export const getPostSlugs = async (dir: string, filterRegex?: RegExp) => {
21
21
const contentRoot = getContentRoot ( )
22
22
const _dir = join ( contentRoot , dir )
23
23
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
+ }
46
47
47
- if ( filterRegex ?. test ( path ) ) continue
48
+ if ( filterRegex ?. test ( path ) ) continue
48
49
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
51
52
52
- const sanitizedPath = toPosixPath (
53
- path . replace ( contentRoot , "" ) . replace ( "/index.md" , "" )
54
- )
53
+ const sanitizedPath = toPosixPath (
54
+ path . replace ( contentRoot , "" ) . replace ( "/index.md" , "" )
55
+ )
55
56
56
- files . push ( sanitizedPath )
57
- }
57
+ files . push ( sanitizedPath )
58
+ }
58
59
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
+ }
60
72
}
61
73
62
74
export const getTutorialsData = ( locale : string ) : ITutorial [ ] => {
0 commit comments