Next.js Discord

Discord Forum

Load <Image> size up to maximum

Answered
American Crow posted this in #help-forum
Open in Discord
American CrowOP
 <Image
    width="640"
    height="960"
    sizes="100vw"
    src={frontMatter.image}
    alt={frontMatter.title}
    priority
  />

next.config.mjs
  images: {
    domains: ['localhost', 'images.unsplash.com'],
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
    deviceSizes: [420, 768, 1024, 1200],
  },

I'd like to load image sizes responsively.

When i set device size to e.g. 400px the network tab shows that the 420x630 version of the image is loaded (screenshot1). Which is correct.

Now when i resize my window and make it bigger (from 400px to 1200px) three more network requests appear (screenshot2). Which is also correct according to my deviceSizes.

However all of these 3 request the same max image of 640x960.

So i was wondering if i can tell nextjs a max size or somth? The result should basically be 1 more network request when resizing the window instead of 3.
Answered by Arinji
ok so thats just your sizes prop,
sizes="(max-width: 450px) 420px, 640px"
View full answer

74 Replies

@American Crow stole your forum, its only valid i help you out aye :D
American CrowOP
i dont care but thanks for the help
anyways, you dont really need the imageSizes thingy, you can just use the "sizes" prop for it
@American Crow i dont care but thanks for the help
do you know how the sizes stuff works? or i can show you an easy way to use it
@Arinji anyways, you dont really need the imageSizes thingy, you can just use the "sizes" prop for it
American CrowOP
the imageSizes in the next.config is just the default ones i realize i don't have to put it there but its implicitly there anyways. i just like having it there.
you talking about the sizesprop on <Image> itself?

Yea i think i got the basics of it but i don't know how to adjust it to my specific problem described above
yes im talking abt the sizes prop on the image itself
do you use tailwind?
American CrowOP
yea
ok so imagine you have a div, can you show me the classnames you would put which would match the resizing you want with the img?
American CrowOP
you mean in sm: md: etc?
yea
American CrowOP
not really the default tailwind breakpoints but It's pretty simple in that case: up to a screen-width of 768 load the 420px witdh image above that the 640px image (see screenshots)
wait no i lied
wait
dont type
pssss
dont
lmao
American CrowOP
up to a screen-width of 450px load the 420px image above that load the 640px image
ok so thats just your sizes prop,
sizes="(max-width: 450px) 420px, 640px"
Answer
usually i tell new people to get their sizes prop values form tailwind
since tailwind provides the breakpoint as well :D
American CrowOP
Unfortunately it works now i have to accept your shit as answer
well done thanks 😉
me looking at you typing be like
lol
haha
get out
no stay
your issue came back?
@Arinji ok so thats just your sizes prop, sizes="(max-width: 450px) 420px, 640px"
American CrowOP
Yo, one more thing isn't it kinda smart to always set a specific max-width to the exact width of the google emulator ? So you always on point for lighthouse image sizes?
just a realization maybe?
google mobile lighthouse bot is 412px width so it's smart to optimize for that i think?
hmm, maybe actually
never tried so myself
American CrowOP
yea sometimes lighthouse tells you you can save a few kb. For example if you load a 640px on a 412px screen (which google mobile bot is testing with)
you could always "cheat" it so the 412px screen width gets a perfect 412px or slightly below image
hmm, try it out, would be cool as a #discussions
American CrowOP
Actually works in lighthouse testing. The "optimize image sizes" goes away if close enough to 412px
@Arinji hmm, try it out, would be cool as a <#752647196419031042>
American CrowOP
no thats our secret knowlegdge now 🤫
@Arinji uhh elaborate pls
American CrowOP
one more question.
Since nexjts generates all these sizes below 420px (imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],)
sizes="(max-width: 450px) 420px, 640px"
how would i tell the sizes prob to make use of these sizes below 450px? Right now its kinda hardcoded to "up to 450px use the 420px image"
Do i have to define every breakpoint?
I know thats what i asked originally
make a diff forum :D
American CrowOP
do you know the answer?
maybe
@Arinji maybe
American CrowOP
com'n i will create a new one if you know the answer i dont want to waste time
yea i do
i think :sunglasses_1:
im never sure of my answers
i just yap and hope it works
ping me
@Arinji ping me
American CrowOP
now i think i solved it while typing out the question
sizes="(min-width: 450px) 640px, 100%"
If you would have had a different answer i will still create it?
@Arinji yea :(
American CrowOP
you earned something better than a virtual point on a discord server. you taught me how to do it
just setup breakpoints as specifiied in deviceWidth
lol
since your srcset is a mix of the deviceWidth and imageSizes
:D
American CrowOP
yea it just clicked for me
thanks
np
bro was adamant on me not getting the point fr
American CrowOP
here i was not lying already typed the whole question:
all good xD
joulev gonna yell at us for taking in a forum
lets dip
I see this post is answered. If it isn't, kindly run the /remove-post-answer command, to avoid confusion
American CrowOP
🏃‍♀️
🏃
If you have another extensive query, its best to create another thread!