How to access files uploaded to the public folder in Next.js?

I have a project in Next.js. I have that upload files and share that in public URL to this project.

With npm run dev first I uploaded files to public folder and it worked fine, but when I change to npm run start and upload files, the files upload to public folder but with URL http://mydomain/fileuploaded.jpg it did not show, is rare but it's there.

I searched on the Internet but I didn't find a solution for this problem.


Solution 1:

From Next.js documentation:

Only assets that are in the public directory at build time will be served by Next.js. Files added at runtime won't be available.

You'll have to persist the uploaded files somewhere else if you want to have access to them in the app at run time.


Alternatively, you could setup your own custom server in Next.js, which would give you more control to serve static files/assets.

You can also achieve something similar using API routes instead. See Next.js serving static files that are not included in the build or source code for details.