Skip to content

Latest commit

 

History

History
143 lines (117 loc) · 3.72 KB

File metadata and controls

143 lines (117 loc) · 3.72 KB
icon ri:image-2-line

Assets

Public assets

Nitro handles assets via the server/public/ directory.

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

server/
  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 server/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 server/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 server/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 server/assets/ directory.

::code-group

export default defineNitroConfig({
  serverAssets: [{
    baseName: 'my_directory',
    dir: './my_directory' // Relative to `srcDir`
  }]
})
export default defineNuxtConfig({
  nitro: {
    serverAssets: [{
      baseName: 'my_directory',
      dir: './my_directory' // Relative to Nitro `srcDir`
    }]
  }
})

::

You could want to add a directory (server/templates/) with html templates for example.

::code-group

export default defineNitroConfig({
  serverAssets: [{
    baseName: 'templates',
    dir: './templates' // Relative to `srcDir`
  }]
})
export default defineNuxtConfig({
  nitro: {
    serverAssets: [{
      baseName: 'templates',
      dir: './templates' // Relative to Nitro `srcDir`
    }]
  }
})

::

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

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

Large catalogs (embed)

By default each file becomes a lazy Rollup raw: module (embed: true). For directories with many small files you can opt in:

embed Behavior
true (default) One raw: chunk per file (unchanged)
"inline" Single virtual module (fast builds; larger server entry)
false Copy to server/assets/<baseName> and read from disk at runtime (Node/Bun/Deno)
export default defineNitroConfig({
  serverAssets: [{
    baseName: 'i18n',
    dir: './i18n-data',
    embed: 'inline', // or false
  }]
})