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.

Read more in Docs > Assets.

#compressPublicAssets

  • Type: boolean | { gzip?: boolean, brotli?: boolean, 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. Default compression levels are used. Using this option you can support zero overhead asset compression without using a CDN.

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
    },
  ],
});
Read more in Docs > Assets#server Assets.

Nitro  builds full-stack servers that deploy anywhere.