Guide

Assets


Public assets

Nitro handles assets via the public/ directory.

All assets in public/ directory will be automatically served. This means that you can access them directly from the browser without any special configuration.

public/
  image.png     <-- /image.png
  video.mp4     <-- /video.mp4
  robots.txt    <-- /robots.txt
package.json
nitro.config.ts

Production public assets

When building your Nitro app, the public/ directory will be copied to .output/public/ and a manifest with metadata will be created and embedded in the server bundle.

{
  "/image.png": {
    "type": "image/png",
    "etag": "\"4a0c-6utWq0Kbk5OqDmksYCa9XV8irnM\"",
    "mtime": "2023-03-04T21:39:45.086Z",
    "size": 18956
  },
  "/robots.txt": {
    "type": "text/plain; charset=utf-8",
    "etag": "\"8-hMqyDrA8fJ0R904zgEPs3L55Jls\"",
    "mtime": "2023-03-04T21:39:45.086Z",
    "size": 8
  },
  "/video.mp4": {
    "type": "video/mp4",
    "etag": "\"9b943-4UwfQXKUjPCesGPr6J5j7GzNYGU\"",
    "mtime": "2023-03-04T21:39:45.085Z",
    "size": 637251
  }
}

This allows Nitro to know the public assets without scanning the directory, giving high performance with caching headers.

Server assets

All assets in assets/ directory will be added to the server bundle. After building your application, you can find them in the .output/server/chunks/raw/ directory. Be careful with the size of your assets, as they will be bundled with the server bundle.

They can be addressed by the assets:server mount point using the storage layer.

For example, you could store a json file in assets/data.json and retrieve it in your handler:

export default defineEventHandler(async () => {
  const data = await useStorage('assets:server').getItem(`data.json`)
  return data
})

Custom server assets

In order to add assets from a custom directory, you will need to define a path in your nitro config. This allows you to add assets from a directory outside of the assets/ directory.

export default defineNitroConfig({
  serverAssets: [{
    baseName: 'my_directory',
    dir: './my_directory'
  }]
})

You could want to add a directory with html templates for example.

export default defineNitroConfig({
  serverAssets: [{
    baseName: 'templates',
    dir: './templates'
  }]
})

Then you can use the assets:templates base to retrieve your assets.

handlers/success.ts
export default defineEventHandler(async (event) => {
  const html = await useStorage('assets:templates').getItem(`success.html`)
  return html
})