Nuxt/docs/content/2.guide/2.features/3.assets.md
Clément Ollivier 8424165258
docs(features): complete assets page (#4563)
Co-authored-by: Sébastien Chopin <seb@nuxtjs.com>
Co-authored-by: Damian <48835293+DamianGlowala@users.noreply.github.com>
Co-authored-by: pooya parsa <pyapar@gmail.com>
2022-04-27 18:21:32 +02:00

2.2 KiB

Assets

Nuxt uses two directories to handle assets like stylesheets, fonts or images:

  • The public/ directory content is served at the server root as-is.
  • The assets/ directory contains by convention every asset that you want the build tool (Vite or Webpack) to process.

public/ directory

The public/ directory is used as a public server for static assets publicly available at a defined URL of your application.

You can get a file in the public/ directory from your application's code or from a browser by the root URL /.

Example

For example, referencing an image file in the public/img/ directory, available at the static URL /img/nuxt.png:

<template>
  <img src="/img/nuxt.png" alt="Discover Nuxt 3" />
</template>

assets/ directory

Nuxt uses Vite or Webpack to build and bundle your application. The main function of these build tools is to process JavaScript files, but they can be extended through plugins (for Vite) or loaders (for Webpack) to process other kind of assets, like stylesheets, fonts or SVG. This step transforms the original file mainly for performance or caching purposes (such as stylesheets minification or browser cache invalidation).

By convention, Nuxt uses the assets/ directory to store these files but there is no auto-scan functionality for this directory, and you can use any other name for it.

In your application's code, you can reference a file located in the assets/ directory by using the ~/assets/ path.

Example

For example, referencing an image file that will be processed if a build tool is configured to handle this file extension:

<template>
  <img src="~/assets/img/nuxt.png" alt="Discover Nuxt 3" />
</template>

::alert{type=info icon=💡} Nuxt won't serve files in the assets/ directory at a static URL like /assets/my-file.png. If you need a static URL, use the public/ directory. ::