Assets Handling

Nitro handles assets via the public directory.


Public Assets

All assets in public/ directory will be automatically served.

Server Assets

All assets in server/assets/ directory will be added automatically to the server bundle.

They can be addressed by the key assets:server:path:to:asset using storage layer and useStorage.

Tip !
Assets keys can be written assets/server/my_file_path or assets:server:my_file_path.

Custom Server Assets

In order to add assets from a custom directory, path will need to be defined in the nitro config:

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

They can be addressed by the key assets/my_directory/.

Examples

Example: Retrieving a json data from default assets directory:

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

Example: Retrieving a html file from a custom assets directory:

// nitro.config.tsimport { defineNitroConfig } from 'nitropack'export default defineNitroConfig({  serverAssets: [{    baseName: 'templates',    dir: './server/templates'  }]})
export default defineEventHandler(async () => {  const html = await useStorage().getItem(`assets/templates/success.html`)  return html})