Assets

Public Assets

Nitro handles assets via the public/ directory.

Nitro v3 Alpha docs are a work in progress — expect updates, rough edges, and occasional inaccuracies.

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

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.

Unless using useStorage(), assets won't be included in sever 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:

import { defineHandler } from "nitro/h3";

export default defineHandler(async () => {
  const data = await useStorage("assets:server").get("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.

nitro.config.ts
import { defineNitroConfig } from "nitro/config";

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

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

nitro.config.ts
import { defineNitroConfig } from "nitro/config";

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

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

handlers/success.ts
import { defineHandler } from "nitro/h3";

export default defineHandler(async (event) => {
  const html = await useStorage("assets:templates").get("success.html");

  return html;
});