nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Ability to read assets from public folder

Open bitbytebit1 opened this issue 3 months ago • 2 comments

Describe the feature

Would love to be able to get access to the assets variable that is created on build

For example:

const assets = {
  "/success.html": {
    "type": "text/html; charset=utf-8",
    "etag": "\"7-i5KKyIzUGAhxNMutZtRIycH0cTo\"",
    "mtime": "2025-10-27T22:40:48.992Z",
    "size": 7,
    "path": "../public/success.html"
  }
};

What I'm dreaming of is being able to do something like this:

const fileContents = await useStorage('public:server').getItemRaw('success.html')
const meta = await useStorage('public:server').getMeta('success.html')
// Do some stuff with file...
setResponseHeader(event, 'content-type', meta.type)
return fileContents

I can of course do something similar with custom server assets but then I need to figure out the content type header on my own, feels like the all data is right there I just can't get access to it. Another option would be to export readAsset for end users. Thoughts?

Additional information

  • [x] Would you be willing to help implement this feature?

bitbytebit1 avatar Oct 27 '25 22:10 bitbytebit1

Seems like I can do this in production, however it doesn't work during in development, feels sketchy though.

import { getPublicAssetMeta, readAsset } from '#nitro-internal-virtual/public-assets';
import assets from '#nitro-internal-virtual/public-assets-data'
export default defineEventHandler(async (event) => {
  setResponseHeader(event, 'content-type', assets['/success.html'].type);
  return readAsset('/success.html');
});

bitbytebit1 avatar Oct 27 '25 23:10 bitbytebit1

Another work around would be to call for the resource using $fetch. I'm cuirous if I call for resources in the public folder using $fetch, does unenv still apply? Or will it hit the network layer?

bitbytebit1 avatar Oct 27 '25 23:10 bitbytebit1