The assets directory contains your uncompiled assets such as Stylus or Sass files, images, or fonts.

Images

Inside your vue templates, if you need to link to your assets directory use ~/assets/your_image.png with a slash before assets.

  1. <template>
  2. <img src="~/assets/your_image.png" />
  3. </template>

Inside your css files, if you need to reference your assets directory, use ~assets/your_image.png(without a slash)

  1. background: url('~assets/banner.svg');

When working with dynamic images you will need to use require

  1. <img :src="require(`~/assets/img/${image}.jpg`)" />

assets - 图1

Learn more about webpack Assets

Styles

Nuxt.js lets you define the CSS files/modules/libraries you want to set globally (included in every page). In the nuxt.config you can easily add your styles using the CSS Property.

nuxt.config.js

  1. export default {
  2. css: [
  3. // Load a Node.js module directly (here it's a Sass file)
  4. 'bulma',
  5. // CSS file in the project
  6. '~/assets/css/main.css',
  7. // SCSS file in the project
  8. '~/assets/css/main.scss'
  9. ]
  10. }

Sass

In case you want to use sass make sure that you have installed sass and sass-loader packages.

  1. yarn add -D sass sass-loader@10 fibers
  1. npm install --save-dev sass sass-loader@10 fibers

assets - 图2

Synchronous compilation with sass (2x speed increase) is enabled automatically when fibers is installed.

Nuxt.js will automatically guess the file type by its extension and use the appropriate pre-processor loader for webpack. You will still need to install the required loader if you need to use them.

Fonts

You can use local fonts by adding them to your assets folder. Once they have been added you can then access them through your css using the @font-face.

  1. -| assets
  2. ----| fonts
  3. ------| DMSans-Regular.ttf
  4. ------| DMSans-Bold.ttf

assets/main.css

  1. @font-face {
  2. font-family: 'DM Sans';
  3. font-style: normal;
  4. font-weight: 400;
  5. font-display: swap;
  6. src: url('~assets/fonts/DMSans-Regular.ttf') format('truetype');
  7. }
  8. @font-face {
  9. font-family: 'DM Sans';
  10. font-style: normal;
  11. font-weight: 700;
  12. font-display: swap;
  13. src: url('~assets/fonts/DMSans-Bold.ttf') format('truetype');
  14. }

assets - 图3

CSS files are not automatically loaded. Add them using the CSS config property .

assets - 图4

To add external fonts such as google fonts check out the Meta Tags and SEO chapter

Webpack Assets

By default, Nuxt uses webpack’s vue-loader, file-loader and url-loader to serve your assets. You can also use the static directory for assets that should not run through webpack

Webpack

vue-loader automatically processes your style and template files with css-loader and the Vue template compiler out of the box. In this compilation process, all asset URLs such as <img src="...">, background: url(...) and CSS @import are resolved as module dependencies.

For example, we have this file tree:

  1. -| assets/
  2. ----| image.png
  3. -| pages/
  4. ----| index.vue

If you use url('~assets/image.png') in your CSS, it will be translated into require('~/assets/image.png').

assets - 图5

The ~/ alias won’t be resolved correctly in your CSS files. You must use ~assets (without a slash) in url CSS references, i.e. background: url("~assets/banner.svg")

If you reference that image in your pages/index.vue:

pages/index.vue

  1. <template>
  2. <img src="~/assets/image.png" />
  3. </template>

It will be compiled into:

  1. createElement('img', { attrs: { src: require('~/assets/image.png') } })

Because .png is not a JavaScript file, Nuxt.js configures webpack to use file-loader and url-loader to handle them for you.

The benefits of these loaders are:

file-loader lets you designate where to copy and place the asset file, and how to name it using version hashes for better caching. In production, you will benefit from long-term caching by default!

url-loader allows you to conditionally inline files as base64 data URLs if they are smaller than a given threshold. This can reduce the number of HTTP requests for trivial files. If a file is larger than the threshold, it automatically falls back to file-loader.

For these two loaders, the default configuration is:

  1. // https://github.com/nuxt/nuxt.js/blob/dev/packages/webpack/src/config/base.js#L382-L411
  2. {
  3. test: /\.(png|jpe?g|gif|svg|webp|avif)$/i,
  4. use: [{
  5. loader: 'url-loader',
  6. options: {
  7. esModule: false,
  8. limit: 1000, // 1kB
  9. name: 'img/[name].[contenthash:7].[ext]'
  10. }
  11. }]
  12. },
  13. {
  14. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i,
  15. use: [{
  16. loader: 'url-loader',
  17. options: {
  18. esModule: false,
  19. limit: 1000, // 1kB
  20. name: 'fonts/[name].[contenthash:7].[ext]'
  21. }
  22. }]
  23. },
  24. {
  25. test: /\.(webm|mp4|ogv)$/i,
  26. use: [{
  27. loader: 'file-loader',
  28. options: {
  29. esModule: false,
  30. name: 'videos/[name].[contenthash:7].[ext]'
  31. }
  32. }]
  33. }

Which means that every file below 1 kB will be inlined as base64 data URL. Otherwise, the image/font will be copied in its corresponding folder (inside the .nuxt directory) with a name containing a version hash for better caching.

When launching your application with nuxt, your template in pages/index.vue:

pages/index.vue

  1. <template>
  2. <img src="~/assets/your_image.png" />
  3. </template>

Will be transformed into:

  1. <img src="/_nuxt/img/your_image.0c61159.png" />

If you want to change the loader configurations, please use build.extend.

Aliases

By default the source directory (srcDir) and the root directory (rootDir) are the same. You can use the alias of ~ for the source directory. Instead of writing relative paths like ../assets/your_image.png you can use ~/assets/your_image.png.

Both will achieve the same results.

components/Avatar.vue

  1. <template>
  2. <div>
  3. <img src="../assets/your_image.png" />
  4. <img src="~/assets/your_image.png" />
  5. </div>
  6. </template>

We recommend using the ~ as an alias. @ is still supported but will not work in all cases such as with background images in your css.

You can use the alias of ~~ or @@ for the root directory.

assets - 图6

Tip: On Spanish keyboard you can access ~ with (Option + ñ) on Mac OS, or (Alt gr + 4) on Windows