How to Access Local Files in Server Actions in Next.js 14.1 Deployed on Vercel #70125
Unanswered
NindoK
asked this question in
App Router
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Content:
I've been struggling with an issue for quite some time while deploying my Next.js 14.1 app on Vercel. Specifically, I'm encountering problems when trying to access local files inside a server action. After searching through numerous StackOverflow threads and GenAI answers, I haven't found a viable solution.
Problem:
I have a server action that generates a PDF using a
template.pdf
file and several font files (e.g.,.ttf
files). These files are stored in theactions/pdf_data
folder. However, in production, when I attempt to access these files, I get aENOENT
(file not found) error because the directory and files seem to be missing in the production environment.What I've Tried:
I attempted to resolve this issue by adding the following configuration in my
next.config.js
file:However, when I try to read the files during the execution of my server action in production, the directory structure inside the
.next/server
folder looks like this:It appears that my solution doesn't work because, in the Vercel edge environment, only certain files are generated, rather than the full .next bundle that exists at build time.
Potential Solution:
I'm considering importing the necessary files directly within the server action, as this might force the build process to bundle the files correctly. However, I'm not sure if this approach will work or how to implement it effectively.
Has anyone encountered a similar issue or found a reliable solution for accessing local files in server actions on Vercel?
PS: I am aware of the option to use
@vercel/blob
for storing files, but I would prefer not to use it, as it significantly increases the time required to generate the PDF. I'm looking for an alternative solution that avoids this overhead.My Simplified Server Action Code:
PS Solution
Switched from server action to api route and I can access my files accordingly.
As much as my problem is solved I'm still trying to figure out if there's a way to fix this in any other way while using server actions
Beta Was this translation helpful? Give feedback.
All reactions