Nuxt.js is a powerful framework for building Vue.js applications, and it comes with a robust system for managing assets like stylesheets, fonts, and images. In this blog post, we’ll explore how Nuxt handles assets and the best practices for managing them in your Nuxt project.

Public Directory

Nuxt uses two directories to handle assets: public/ and assets/. Let’s start with the public/ directory.

The public/ directory serves as a public server for static assets that are publicly available at a defined URL in your application. Any file in the public/ directory can be accessed directly from your application’s code or a web browser by using the root URL /.

For example, if you have an image file located at public/img/nuxt.png, you can reference it in your component like this:

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

This makes it easy to serve static assets directly from the public/ directory.

Assets Directory

Next, let’s dive into the assets/ directory. Nuxt uses build tools like Vite or Webpack to process assets for performance and caching purposes. While these build tools primarily prioritize JavaScript files, you can also extend them to handle other types of assets such as stylesheets, fonts, and SVG files.

By convention, Nuxt uses the assets/ directory to store these files, but you can use any other name for it if you prefer. When referencing a file located in the assets/ directory in your application’s code, you can use the ~/assets/ path.

Here’s an example of referencing an image file in the assets/ directory:

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

This informs Nuxt that the build tool should process the file if it’s configured to handle that file extension. It allows you to take advantage of optimizations like stylesheet minification and browser cache invalidation.

Global Styles Imports

In Nuxt projects, you might want to globally insert statements in your components’ styles. You can achieve this by using the Vite option in your nuxt.config.js file.

For instance, if you have a Sass partial file (_colors.scss) containing color variables that you want to use globally in your Nuxt pages and components, you can import it like this in your nuxt.config.js:

export default {
  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/assets/_colors.scss" as *;'

This configuration allows you to make global style imports easily accessible throughout your Nuxt project.

Understanding how Nuxt handles assets in the public/ and assets/ directories is crucial for optimizing the performance of your Vue.js applications. By using the appropriate directory and referencing assets correctly, you can efficiently manage static files and styles, making your Nuxt project more efficient and maintainable.

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.