文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
资产 Assets
Nitro 通过 `public/` 目录处理资产。nitro.config.ts nitro.config.ts js
Public Assets
public/
目录中的所有资产将自动提供。
public/
image.png <-- /image.png
video.mp4 <-- /video.mp4
robots.txt <-- /robots.txt
package.json
nitro.config.ts
使用 Nitro 构建时,它会将 public/
目录复制到 .output/public/
并创建一个包含元数据的清单:
{
"/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 无需扫描目录即可了解公共资产,从而通过缓存标头提供高性能。
Server Assets
assets/
目录中的所有资产都将添加到服务器包中。
它们可以通过使用 useStorage('assets:server')
的 assets:server
挂载点来寻址
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/config'
export default defineNitroConfig({
serverAssets: [{
baseName: 'my_directory',
dir: './server/my_directory'
}]
})
nuxt.config.tsexport default defineNuxtConfig({
nitro: {
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:
import { defineNitroConfig } from 'nitropack/config'
export default defineNitroConfig({
serverAssets: [{
baseName: 'templates',
dir: './templates' // Relative to `srcDir` (`server/` for nuxt)
}]
})
nuxt.config.tsexport default defineNuxtConfig({
nitro: {
serverAssets: [{
baseName: 'templates',
dir: './templates' // Relative to `srcDir` (`server/` for nuxt)
}]
}
})
// routes/success.ts
export default defineEventHandler(async (event) => {
return await useStorage().getItem(`assets/templates/success.html`)
// or
return await useStorage('assets:templates').getItem(`success.html`)
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论