Guide

资源


¥Assets

公共资源

¥Public assets

Nitro 通过 server/public/ 目录处理资源。

¥Nitro handles assets via the server/public/ directory.

server/public/ 目录中的所有资源都将自动提供服务。这意味着你可以直接从浏览器访问它们,而无需任何特殊配置。

¥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

构建 Nitro 应用时,server/public/ 目录将被复制到 .output/public/,并创建包含元数据的清单并将其嵌入到服务器包中。

¥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
  }
}

这使得 Nitro 无需扫描目录即可获取公共资源,并通过缓存标头实现高性能。

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

服务器资源

¥Server assets

server/assets/ 目录中的所有资源都将添加到服务器包中。构建应用后,你可以在 .output/server/chunks/raw/ 目录中找到它们。请注意资源的大小,因为它们将与服务器包打包在一起。

¥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.

它们可以通过 assets:server 挂载点使用 存储层 进行寻址。

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

例如,你可以在 server/assets/data.json 中存储一个 json 文件,并在处理程序中检索它:

¥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

为了从自定义目录添加资源,你需要在 nitro 配置中定义路径。这允许你从 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: './server/my_directory'
  }]
})

例如,你可能想要添加一个包含 HTML 模板的目录。

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

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

然后,你可以使用 assets: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
})