Next.js Discord

Discord Forum

check for path if it exist otherwise use default path

Unanswered
Lesser Scaup posted this in #help-forum
Open in Discord
Avatar
Lesser ScaupOP
hello, im new to nextjs and still learning about it, i wm trying to check if directory exist, if if not to go to the default one in the following method:
    const available = ['de', 'en', 'en-us', 'es', 'fr', 'it', 'nl'];
    const images = available.includes(locale)
        ? {
                image1: `/driver-app/${locale}/1.png`,
                image2: `/driver-app/${locale}/2.png`,
                image3: `/driver-app/${locale}/3.png`,
                image4: `/driver-app/${locale}/4.png`,
                image5: `/driver-app/${locale}/5.png`,
                image6: `/driver-app/${locale}/6.png`,
          }
        : {
                image1: '/driver-app/en/1.png',
                image2: '/driver-app/en/2.png',
                image3: '/driver-app/en/3.png',
                image4: '/driver-app/en/4.png',
                image5: '/driver-app/en/5.png',
                image6: '/driver-app/en/6.png',
          };

i'm doing it this way inside the component, but it's efficient since if add some new folder i need to add it in the available array as well, i tried to do a function with fs and fs.fileExists(), but i didn't know how to implement and where to put,
can someone help me with it

0 Replies