DEV Community

Martin Ratinaud
Martin Ratinaud

Posted on

NextJs on vercel - missing files after build when interpolating file names

While working on my free comparator for staking crypto - the perfect tool to find the best exchange to stake or lend your cryptos and earn the best rewards - I came across a difficult problem to understand.

Setup

Site is in Next / typescript
It is deployed on Vercel
It loads mdx through next-mdx-remote in getStaticProps

Build

Build works fine and resulting static files contains the content of an mdx file I'm loading in the page.

But after revalidation, content is not there anymore.

ERROR: Could not find /var/task/content/static/en/home.mdx

Weirdly enough, the content folder exists during the build, thus creating correctly the static files

But after deploy, this content folder was not here anymore, thus revalidating page with empty content

Reason

This is because nextjs uses @vercel/nft to determine exactly which files (including node_modules) are necessary for the application runtime.

So when you're using

const folder = "toto"
fs.readFileSync(path.join(process.cwd(), folder))
Enter fullscreen mode Exit fullscreen mode

this library can not guess what it should include in the build and thus do not include the files

Solution

You need to specify exactly the path of the file to be included, and not a variable containing that path.

fs.readFileSync(path.join(process.cwd(),'toto'))
Enter fullscreen mode Exit fullscreen mode

Thanks!

Top comments (1)

Collapse
 
mrmegamango profile image
MrMegaMango

yea I had to find a similar way to trick vercel to take my static data so my page wouldn't 500.