
特徵
sharp優化WebP和後備圖像loading="lazy" )<Picture />組件簡化了用法默認情況下, Next-IMG配置為使用:
768px的1斷點所有這些設置和更多設置都可以在您的next.config.js或單個圖像導入中更改。
由Humaans開發和使用。
默認情況下,Next.js或WebPack對優化圖像無濟於事。這意味著使用圖像CDN或根本不優化圖像的自定義配置或腳本,手工處理圖像。 Next-IMG提供了為您的下一個項目添加圖像的替代精簡方法。它結合了next.js插件,自定義的webpack加載程序和一個反應組件,以最佳方式製作圖像的方式幾乎與鍵入<img src='foo.png' />一樣容易。
簡而言之,它需要以下內容:
< Picture src = { require ( './images/jelly.jpg?sizes=375,800' ) } alt = 'Jellyfish' />導入,調整,優化,緩存(持續在git repo中),並輸出以下HTML:
< picture >
< source
type =" image/webp "
srcset ="
/_next/static/images/[email protected] 375w,
/_next/static/images/[email protected] 750w,
/_next/static/images/[email protected] 800w,
/_next/static/images/[email protected] 1600w
"
sizes =" (max-width: 768px) 375px, 800px "
/>
< source
type =" image/jpeg "
srcset ="
/_next/static/images/[email protected] 375w,
/_next/static/images/[email protected] 750w,
/_next/static/images/[email protected] 800w,
/_next/static/images/[email protected] 1600w
"
sizes =" (max-width: 768px) 375px, 800px "
/>
< img
src =" /_next/static/images/[email protected] "
srcset ="
/_next/static/images/[email protected] 375w,
/_next/static/images/[email protected] 750w,
/_next/static/images/[email protected] 800w,
/_next/static/images/[email protected] 1600w
"
width =" 375 "
height =" 250 "
alt =" Jellyfish "
/>
</ picture >查看示例。
安裝軟件包
npm install next-img
將插件添加到您的next.config.js :
const withImg = require ( 'next-img/plugin' )
module . exports = withImg ( {
nextImg : {
breakpoints : [ 768 ] ,
} ,
} )在您的應用程序中,導入圖像並使用<Picture />組件嵌入:
import { Picture } from 'next-img'
import jelly from './images/jelly.jpg?sizes=375,800'
export default ( ) => < Picture src = { jelly } />或內聯:
import { Picture } from 'next-img'
export default ( ) => < Picture src = { require ( './images/jelly.jpg?sizes=375,800' ) } />此特定示例將生成以下圖像:
調整大小的圖像將在開發過程中將項目目錄保存到resources目錄中。這意味著,如果您調整圖像導入參數或插件配置,則可能會生成項目不再使用的額外圖像。在這種情況下,執行next-img命令以刪除所有不必要的圖像並構建任何缺失的圖像:
npx next-img
現在,請訪問resources目錄到您的源控件,以便以後重複使用開發和生產構建。您可以通過設置persistentCache: false在插件配置中關閉此功能,在這種情況下,圖像將僅存儲在.next Directory內部的臨時緩存中。
查看更多用法示例。
默認插件配置選項:
{
// global settings for images, can be overriden per image
breakpoints : [ 768 ] ,
densities : [ '1x' , '2x' ] ,
// output image quality configuration
jpeg : {
quality : 80 ,
webp : {
quality : 90 ,
reductionEffort : 6 ,
} ,
} ,
png : {
quality : 100 ,
webp : {
reductionEffort : 6 ,
lossless : true ,
} ,
} ,
// the directory within Next.js build output
imagesDir : 'images' ,
// the output image name template
imagesName : '[name]-[size]@[density]-[hash].[ext]' ,
// advanced - customise the image public path
imagesPublicPath : null ,
// advanced - customise the image output path
imagesOutputPath : null ,
// persistent cache allows for fast deploy and
// development workflow by avoiding reprocessing
// images that were previously processed
persistentCache : true ,
persistentCacheDir : 'resources' ,
// this directory within .next is used in case persistent cache is turned off
cacheDir : path . join ( 'cache' , 'next-img' )
}請參閱jpeg/png/webp壓縮選項的尖銳文檔。
導入圖像時,您可以使用查詢參數自定義優化:
320px寬的圖像,只需在此處指定320 ,該插件將根據densities配置生成任何必要的較大版本。1x和2x的圖像尺寸,如果您只想產生每個大小的圖像,或者1x,2x,3x等,則指定1x 。如果您想要更多的密度。jpeg圖像的質量配置選項。請注意, jpeg->webp設置需要嵌套在此參數下,例如?jpeg[webp][quality]=95png圖像的質量配置選項。請注意, png->webp設置需要嵌套在此參數下,例如?png[webp][lossless]=false&png[webp][nearLossless]=true示例:
import img1 from './images/img.jpg'
import img2 from './images/img.jpg?sizes=375,900'
import img3 from './images/img.jpg?sizes=375,900&densities=1x'
import img4 from './images/img.jpg?sizes=375,900&densities=1x,2x,3x'
import img5 from './images/img.jpg?sizes=375,900&densities=1x,2x,3x&jpeg[quality]=70&jpeg[webp][quality]=70' next-img帶有一個React組件,使嵌入圖像更容易。
這是此組件訪問的道具:
img標籤。這允許使用諸如alt , loading="lazy"等屬性。 當通過src Prop提供單個圖像時,每個大小將配置為使用HTML sizes attribute屬性每個可用的每個斷點顯示。
例如,使用斷點[375, 768]和?sizes=100,400,800 <Picture>組件將應用以下sizes屬性:
(max-width: 375px) 100px,
(max-width: 768px) 400px,
800px
當通過src Prop提供圖像數組時,將使用HTML media attribute配置每個圖像以顯示每個可用的每個斷點。
例如,使用斷點[375, 768]和src=[img1, img2, img3] <Picture>組件將應用以下media屬性:
< picture >
< source media =" (max-width: 480px) " sizes =" {{img1 width}} " />
< source media =" (max-width: 768px) " sizes =" {{img2 width}} " />
< source sizes =" {{img3 width}} " />
< img ... />
</ picture > 我必須使用<Picture />組件嗎?
圖片組件是可選的。您可以根據需要處理導入的圖像對象。
圖像不能進一步優化嗎?
是的,如果通過ImageOptim或其他工具通過jpg/png ,您可能會獲得〜10%-20%或更多的壓縮。事實是,由於此插件已輸出已經優化的WebP,並且您將向大多數現代瀏覽器提供WebP,因此由於它們是後備圖像,因此消除了擠壓jpg/png的額外文件大小的需求。但是,在某些情況下可能需要自定義壓縮算法,我們可能會增加對將來該插件中任意轉換的支持。
要開發此項目,您需要為<Picture />組件運行一個觀察者:
npm install
npm run watch
您可以將示例作為操場:
cd example
npm install
next
在示例dir中執行下next-img命令:
node ../bin/next-img
路線圖
jpg->webp和png->webp轉換允許其他配置webp/jpg/png輸出?raw查詢支持next build來刪除next-img命令的需求以及對將來可以採取該項目的一些想法:
imagemin優化插件添加到管道中。這樣,用戶可以控制如何更加格拉努阿。?sizes=100vw,50vw,900px轉換為像素,這將允許動態類型的導入依賴於您的設計系統和圖像的相對尺寸,例如,如果CSS表示“ 50VW”,則您無需手動完成該計算。require()它們。