I am trying to pre-load images via resource hints.
I am writing a complex partial which processes images and then if requested, appends the <link rel=“preload” data to scratch, and then the information needs to be ranged over in the head.
I am assuming this is not possible due to the fact that the body is compiled after the head.
Does anyone have any advice or a work around??
I am also trying to work out how to add page scoped CSS to the head from the body and partials using the same idea.
I have placed the following in the body and it works
Hi, I’m not a Hugo expert, and I’m not sure I understand your use case, but for the images part I’d try to put this logic into the render hook instead of a partial: Configure Markup | Hugo
I don’t know how you are calling / running your partial, but you could do a pre-run in the head, so you can add whatever meta you need. Then run it again in the main section. partialCached might help if you structure your partials well.
I have had a look and I can’t find any information about pre-running a partial. Could you please elaborate on how this would work?
Basically in the body I specify an image, the partial in the body generates different image sizes, and if configured, and <link rel="preload" tag for the head, for above the fold images.
Preloading images seems like a really bad idea, and quite user-hostile. You will force a users browser to download images they may not want or even ever see, eating up bandwidth and cpu, making the user wait longer, and for what?
There’s a reason native lazy-loading has been shipped in all major browsers. Your approach goes in the complete opposite direction. Good UX requires that you respect user settings (low-data?) and preferences (low-energy?), pre-loading images does neiter.
I’d suggest that you’ll achieve better performance, better metrics and better UX by using loading=lazy on your images unless they appear above the fold, and in that case still don’t preload! Images are not render-blocking, by design, so don’t make them render-blocking!
What are your thoughts on loading=“eager” for above the fold? Would this improve load speed if I have set up placeholder padding to prevent reflow? Or is lazy sufficient in this case?
I regards to preload, I was only chasing it down because the next.js image doc says preload is better than eager. next/image | Next.js
What are your thoughts on lazy vs eager vs preload for above the fold?
Is lazy above the fold generally better because the user gets the page + placeholders quickly?
My personal preference is to use loading=auto and leave it to the browser to decide.
Things are a little different with Next/Nuxt/React beacuse the image references may not exist in the html before the page is hydrated, so the browser needs to be prompted/primed to expect iomage that it would otherwise have no way of knowing were going to be included. With static html that is not the case, as soon as the html is parsed the browser will know about those images even if they have not yet been downloaded.
When I know the image will be above the fold, as in a hero, I will set loading=auto in all other cases I would use loading=lazy. You can prevent document reflow, without placeholder images or padding, by simply adding the height and width attributes of the original image to your img element. Modern browsers are smart enough to calculate the instrinsic image ratio from that and set the height of the replaced element accordingly.
It’s probably also worth noting that you can also set aspect ratio on elements with css, though in the case of images I believe it is preferable to set the height/width attributes directly.