Optimize local images
jampack
optimizes local images for faster download on any device and better Core Web Vitals scores.
- Compresses images using better compressors or modern formats.
- Generates responsive image sets for smaller devices.
- Adds image dimensions if missing to avoid CLS issues.
- Sets images to lazy loading (with exceptions)
<img>
tags
<img src="./redpanda.jpg" alt="Red panda">
becomes
<img src="./redpanda.jpg.webp" alt="Red panda"
srcset="./redpanda.jpg.webp 3872w, ./redpanda@3572w.jpg.webp 3572w, ./redpanda@3272w.jpg.webp 3272w, ./redpanda@2972w.jpg.webp 2972w, ./redpanda@2672w.jpg.webp 2672w, ./redpanda@2372w.jpg.webp 2372w, ./redpanda@2072w.jpg.webp 2072w, ./redpanda@1772w.jpg.webp 1772w, ./redpanda@1472w.jpg.webp 1472w, ./redpanda@1172w.jpg.webp 1172w, ./redpanda@872w.jpg.webp 872w"
sizes="100vw"
loading="lazy"
decoding="async"
width="3872"
height="2592">
src
image is compressed
JPEG
images are compressed into losslyWebP
usingsharp
.PNG
images are compressed into near losslessWebP
using the near_lossless option of the sharp library.AVIF
images are compressed usingsharp
.SVG
images are compressed using svgo
srcset
of smaller images are generated
jampack
will generate a set of smaller images by reducing the width of the images by steps of 300px.
- Images above the fold generate progressives
JPEG
as described in the above the fold optimization. PNG
andJPEG
images will generateWebP
responsive images.AVIF
images will generateAVIF
responsive images.
In other words, you can have a single image in your static site and jampack
will create the different smaller versions to serve the most optimized image for smaller devices.
If srcset
is already present in source <img>
then nothing is done and images are just compressed in
PASS 2.
Responsives sizes
You can set sizes
attribute to fine tune image selection in the srcset
.
See sizes
attributes for more details.
sizes="100vw"
will be added by default when srcset
is set.
Lazy loading
jampack
will set all images to load in lazy by default. This gives the browser the opportunity to load more critical data instead.
Images will have new attributes:
loading="lazy"
decoding="async"
Exceptions
Images will not be lazy loaded in the following conditions:
- Image has attribute
loading="eager"
. Explicitly requesting for no lazy loading. - Image is marked above the fold.
Note
Lazy loading images above the fold can cause LCP issues,
we recommend to use the jampack
βs <the-fold>
feature or mark images with loading="eager"
for images above the fold.
Sets width
and height
attributes
jampack
will set image dimensions to avoid CLS issues.
jampack
will also fix images with invalid format for attributes width
or height
.
<picture>
tags
jampack
will enrich <picture>
tags with AVIF
and WebP
sources when they are missing.
<picture>
<img src="./redpanda.jpg" alt="Red panda">
</picture>
will become
<picture>
<source type="image/avif" srcset="./redpanda@1936w.avif 1936w, ./redpanda@1636w.avif 1636w, ./redpanda@1336w.avif 1336w, ./redpanda@1036w.avif 1036w, ./redpanda@736w.avif 736w">
<source type="image/webp" srcset="./redpanda@1936w.webp 1936w, ./redpanda@1636w.webp 1636w, ./redpanda@1336w.webp 1336w, ./redpanda@1036w.webp 1036w, ./redpanda@736w.webp 736w">
<img src="./redpanda.jpg" alt="Red panda" loading="lazy" decoding="async" width="1936" height="1296" srcset="./redpanda.jpg 1936w, ./redpanda@1636w.jpg 1636w, ./redpanda@1336w.jpg 1336w, ./redpanda@1036w.jpg 1036w, ./redpanda@736w.jpg 736w" sizes="100vw">
</picture>
If the original image is lossless (PNG
or WebP lossless
) then:
- The
WebP
image set will be compressed with near_lossless option. - No
AVIF
image set will be created because AVIF lossless is not very good (1) 2.
If the original image is lossly (JPEG
or WebP lossly
) then:
- The
AVIF
image set will be compressed with normal quality settings. - The
WebP
image set will be lossly compressed.
Demo
SOURCE
JAMPACKED
_jampackimg_bass.svg1891img_bass.svg1891img_bass.svg.svg1459img_jam.svg6731img_jam.svg6731img_jam.svg.svg5476img_music.png142036img_music.png142036img_music.png.webp58184img_screenshot.png503173img_screenshot.png503173img_screenshot.png.webp172990img_water.jpg1954704img_water.jpg1954704img_water.jpg.jpg1106200img_water@1112w.jpeg228670img_water@1412w.jpeg348093img_water@1712w.jpeg481193img_water@2012w.jpeg629626img_water@2312w.jpeg782875img_water@2612w.jpeg945321img_water@812w.jpeg128458index.html1491index.html4029picture_music.png142036picture_music.png142036picture_music.png.webp58184picture_redpanda.jpg799292picture_redpanda.jpg799292picture_redpanda.jpg.jpg259873picture_redpanda@1036w.jpeg91298picture_redpanda@1336w.jpeg140580picture_redpanda@1636w.jpeg197649picture_screenshot.png503173picture_screenshot.png503173picture_screenshot.png.webp172990picture_screenshot@1336w.webp172990SOURCE
- img_bass.svg1891
- img_jam.svg6731
- img_music.png142036
- img_screenshot.png503173
- img_water.jpg1954704
- index.html1491
- picture_music.png142036
- picture_redpanda.jpg799292
- picture_screenshot.png503173
JAMPACKED
- _jampack
- img_bass.svg1891
- img_bass.svg.svg1459
- img_jam.svg6731
- img_jam.svg.svg5476
- img_music.png142036
- img_music.png.webp58184
- img_screenshot.png503173
- img_screenshot.png.webp172990
- img_water.jpg1954704
- img_water.jpg.jpg1106200
- img_water@1112w.jpeg228670
- img_water@1412w.jpeg348093
- img_water@1712w.jpeg481193
- img_water@2012w.jpeg629626
- img_water@2312w.jpeg782875
- img_water@2612w.jpeg945321
- img_water@812w.jpeg128458
- index.html4029
- picture_music.png142036
- picture_music.png.webp58184
- picture_redpanda.jpg799292
- picture_redpanda.jpg.jpg259873
- picture_redpanda@1036w.jpeg91298
- picture_redpanda@1336w.jpeg140580
- picture_redpanda@1636w.jpeg197649
- picture_screenshot.png503173
- picture_screenshot.png.webp172990
- picture_screenshot@1336w.webp172990
__ __
|__|____ _____ ___________ ____ | | __
| \__ \ / \\____ \__ \ _/ ___\| |/ /
| |/ __ \| Y Y \ |_> > __ \\ \___| <
/\__| (____ /__|_| / __(____ /\___ >__|_ \
\______| \/ \/| | \/ \/ \/
v0.20.2 |__| by βΉdivβΊRIOTS
Options:
{ onlyoptim: true }
PASS 1 - Optimizing
βΆ index.html
Done: 17.519s
Summary
ββββββββββββββ€βββββββββββββ€βββββββββββ€βββββββββββββ€βββββββββββββ
β Action β Compressed β Original β Compressed β Gain β
ββββββββββββββΌβββββββββββββΌβββββββββββΌβββββββββββββΌβββββββββββββ’
β jpg->jpg β 2 / 2 β 2.63 MB β 1.30 MB β -1.32 MB β
β png->webp β 5 / 5 β 1.37 MB β 508.33 KB β -890.55 KB β
β svg->svg β 2 / 2 β 8.42 KB β 6.77 KB β -1.65 KB β
β svg->embed β 1 / 1 β 1.85 KB β 1.42 KB β -432.00 B β
ββββββββββββββΌβββββββββββββΌβββββββββββΌβββββββββββββΌβββββββββββββ’
β Total β 10 / 10 β 4.00 MB β 1.81 MB β -2.20 MB β
ββββββββββββββ§βββββββββββββ§βββββββββββ§βββββββββββββ§βββββββββββββ
β No issues