Process images during the build step in your Nuxt.js app πΈ
Process images during the build step in your Nuxt.js app πΈ
npm install nuxt-responsive-loader
// OR
yarn add nuxt-responsive-loader
nuxt-responsive-loader
to the modules
section of nuxt.config.js
:// file: nuxt.config.js
export default {
// ...
modules: ['nuxt-responsive-loader']
}
assets
directoryimg
elements in your templates like so:<template>
<img :srcset="require('~/assets/nuxt.jpg').srcSet" />
</template>
βββ _nuxt
βββ img
βββ 2b88a85-640.jpg
βββ 1fff45c-750.jpg
βββ 6717911-860.jpg
βββ f9f19bf-970.jpg
βββ c0ceb80-1080.jpg
img
element in your built HTML file:<img
srcset="
/_nuxt/img/2b88a85-640.jpg 640w,
/_nuxt/img/1fff45c-750.jpg 750w,
/_nuxt/img/6717911-860.jpg 860w,
/_nuxt/img/f9f19bf-970.jpg 970w,
/_nuxt/img/c0ceb80-1080.jpg 1080w
"
/>
The following examples each require a different custom configuration of the responsive loader module:
// file: nuxt.config.js
export default {
// ...
// Specify your options as a responsiveLoader object
responsiveLoader: {
name: 'img/[hash:7]-[width].[ext]',
quality: 65 // choose a lower value if you want to reduce filesize further
}
}
<!-- file: index.vue -->
<template>
<img :src="require('~/assets/nuxt.jpg').src" />
</template>
// file: nuxt.config.js
export default {
// ...
// Specify your options as a responsiveLoader object
responsiveLoader: {
name: 'img/[hash:7]-[width].[ext]',
placeholder: true
}
}
<!-- file: index.vue -->
<template>
<img :src="require('~/assets/nuxt.jpg').placeholder" data-src="..." />
</template>
.png
:// file: nuxt.config.js
export default {
// ...
// Specify your options as a responsiveLoader object
responsiveLoader: {
name: 'img/[hash:7]-[width].[ext]',
format: 'png'
}
}
The plugin will work out of the box and will use these settings:
{
name: 'img/[hash:7]-[width].[ext]'
min: 640 // minimum image width generated
max: 1080 // maximum image width generated
steps: 5 // five sizes per image will be generated
placeholder: false // no placeholder will be generated
quality: 65 // images are compressed with medium quality
}
If you want to configure the underlying loader, you can do that as well. (All options available here)
// file: nuxt.config.js
export default {
// ...
// Specify your options as a responsiveLoader object
responsiveLoader: {
name: 'img/hello-world-[width].[ext]',
sizes: [200, 500],
format: 'png',
adapter: require('responsive-loader/sharp'),
placeholder: true
}
}
yarn install
or npm install
npm run dev
MIT