nuxt: Unable to serve static files after build

Environment

  • Operating System: Darwin
  • Node Version: v16.14.0
  • Nuxt Version: 3.0.0
  • Nitro Version: 1.0.0
  • Package Manager: npm@8.19.3
  • Builder: vite
  • User Config: modules, build, runtimeConfig
  • Runtime Modules: @nuxtjs/tailwindcss@6.1.3
  • Build Modules: -

Reproduction

Describe the bug

Files which are added to .output/public after the build process are not accessible (already discussed here #8027).

Additional context

No response

Logs

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Thanks for the suggestion. I have went with a different solution by just creating a data folder where I am uploading all the files to and added the following route to serve them:

// /server/routes/file/[name].get.js
import fs from "fs";

export default defineEventHandler(async (event) => {
  const base = "data";
  const filePath = path.join(base, event.context.params.name);

  return sendStream(event, fs.createReadStream(filePath));
});

So I am able to access an image over http://localhost:3000/file/logo.png.

I also encountered a variety of problems, the framework is really not suitable for production environments. For your problem, I solved it with the configuration of nginx:

    location /upload {
        alias /www/xxxx/upload;
    }

This solution didn’t work for me

Hi @fahmifitu, how did you go? ~I got this to work by changing to const base = "./data" and made sure there’s a data folder in the root of the project with some files inside.~

Edit: stopped working after deployment 😢

Same @miclgael I spent hours browsing through nuxt, nitro and unstorage docs and trying multiple solutions with no luck. decided to go completly static build with (ssr: false). simple directory webserver. frontend browser fetch to a json file.

Thanks for the suggestion. I have went with a different solution by just creating a data folder where I am uploading all the files to and added the following route to serve them:

// /server/routes/file/[name].get.js
import fs from "fs";

export default defineEventHandler(async (event) => {
  const base = "data";
  const filePath = path.join(base, event.context.params.name);

  return sendStream(event, fs.createReadStream(filePath));
});

So I am able to access an image over http://localhost:3000/file/logo.png.

This solution didn’t work for me

Is there any way to serve static files in producation without using an external solution? I have an app where users can upload files to the server.

you can use https://github.com/unjs/unstorage/ together with Nitro to make that work ☺️

ah yes, you can’t add content to .output/public after build (and serve the content with your nitro server). We perform a lot of optimisations to make the server efficient, and one of them is only serving content that was present before the build.