Next.js on vercel server has different process.env.PWD for static routes and dynamic routes

164 Views Asked by At

I'm using the app router and deploying on vercel. I have a directory in the root of my project with the name blog where I put markdown files. locally I reach for them by doing: ./blog/a-blog-post.md which work whether the route is dynamic or static but this only worked for static routes on the server. I tried to see what is the process.env.PWD in both and got: static: /vercel/path0/ dynamic: /var/task/

I tried appending the routes with process.cwd() or an absolute path like /blog/a-blog-post.md but with the same results

how can I reach the filesystem on the server in a consistent way whatever the route I am in?

1

There are 1 best solutions below

0
On BEST ANSWER

As far as I know vercel deploys your code not to a single server but to multiple (on the edge). so using the filesystem is not a good idea.

You may figure out if you are on the Edge using this:

if (typeof EdgeRuntime !== 'string') {
  // dead-code elimination is enabled for the code inside this block
}

https://vercel.com/docs/functions/edge-functions/edge-runtime#check-if-you're-running-on-the-edge-runtime

There is this depricated Filesystem API: https://vercel.com/docs/file-system-api/v2 and a Buildoutput API: https://vercel.com/docs/build-output-api/v3 But overall it is not advised to use the FS in such cases.

If you need to write files somethere, maybe use a service like minio / AWS, or a Database. If you want to communicate over the filesystem, try to use something like Kafka or MQTT.