Assets
Configure how public and server assets are served and bundled.
Note
All examples on this page assume the following import in nitro.config.ts:
import { defineConfig } from "nitro";#serveStatic
- Type:
boolean|'node'|'inline' - Default: depends on the deployment preset used.
Serve public/ assets in production.
Note: It is highly recommended that your edge CDN (Nginx, Apache, Cloud) serves the .output/public/ directory instead to enable compression and higher level caching.
export default defineConfig({
serveStatic: "node", // serve static assets using Node.js
});#noPublicDir
- Default:
false
If enabled, disables .output/public directory creation. Skips copying public/ dir and also disables pre-rendering.
export default defineConfig({
noPublicDir: true, // skip public directory output
});#publicAssets
Public asset directories to serve in development and bundle in production.
If a public/ directory is detected, it will be added by default, but you can add more by yourself too!
It's possible to set Cache-Control headers for assets using the maxAge option:
export default defineConfig({
publicAssets: [
{
baseURL: "images",
dir: "public/images",
maxAge: 60 * 60 * 24 * 7, // 7 days
},
],
});The config above generates the following header in the assets under public/images/ folder:
cache-control: public, max-age=604800, immutable
The dir option is where your files live on your file system; the baseURL option is the folder they will be accessible from when served/bundled.
#assetsMtime
- Type:
boolean|Date - Default:
true
Keep file modification times in the bundled public and server asset metadata. Set to false to leave them out, so the asset metadata stays the same between builds of the same source. The serveStatic handler then sends public assets without Last-Modified and revalidates them by their ETag only. Files served from .output/public by a CDN or nitro preview keep the times from disk.
export default defineConfig({
assetsMtime: false, // leave file times out of the build output
});Set it to a Date to use that time for all assets instead, for example the time of the last commit. Public assets then keep a stable Last-Modified header:
export default defineConfig({
assetsMtime: new Date(Number(process.env.SOURCE_DATE_EPOCH) * 1000),
});#compressPublicAssets
- Type:
boolean|{ gzip?: boolean, brotli?: boolean | { quality?: number }, zstd?: boolean } - Default:
false
If enabled, Nitro will generate a pre-compressed (gzip, brotli, and/or zstd) version of supported types of public assets and prerendered routes larger than 1024 bytes into the public directory. Using this option you can support zero overhead asset compression without using a CDN.
Brotli quality (brotli: { quality }) defaults to 10 and accepts 0 to 11. Higher values produce smaller files and take longer to build.
In development, public assets of supported types are compressed on the fly with the enabled gzip and/or brotli encodings instead.
export default defineConfig({
compressPublicAssets: {
gzip: true,
brotli: true, // enable gzip and brotli pre-compression
},
});#serverAssets
Assets can be accessed in server logic and bundled in production.
export default defineConfig({
serverAssets: [
{
baseName: "templates",
dir: "./templates", // bundle templates/ as server assets
},
],
});