I am running into trouble getting the srcset function to operate in a custom module. the images aren't resizing as anticipated. I have them set to responsive and lazy loading in the settings. I have tried it with and without media queries and run into the same issue.
I have asked a couple of people and they've told me they've had a tough time with the resize_image_url function too, so I'm hoping it's something simple that someone can just show me where I have made a mistake.
Below is code and a snip of my field group so you have the relevant names for the fields. For some reason it jumps sizes at 521px but at no other barriers. Apologies, I've only been on the CMS about 8 days and I am trying to pass the best practices course practicum.
thanks for providing the code. As we've chatted in the slack channel, you've solved this yourself 🎉
But a few optimization ideas:
Add some variables on top of the module to add the ability to change it easier without digging through the whole module code (Very benefitial for big&complex modules) and also wrap the css in require_css or require_head statements. Doing so will push the style-tag to the <head>-tag of the page and optimize the rendering.
Your srcset is working, but the CSS max-width and flex-basis settings in your media queries are conflicting with the srcset behavior. The resize_image_url function is behaving unexpectedly due to fixed flex-basis and image max-width settings. This is forcing the image to a maximum of 250px, even when the srcset is trying to load a smaller or larger image. So the browser only sees the max-width and doesn’t respect the srcset breakpoints properly. The width and height attributes in your <img> tag could be interfering with the srcset behavior.
So this is not what is happening. It's at 299px because there is an inline setting in HubL to the explicit width setting of 299px. This is reflected in the inspect tool. You are correct that the resize image url is doing what it is supposed to do, but where it is being overriden is incorrect. It is defaulting to the explicit width setting in the HubL code which is required to be included by the course.
thanks for providing the code. As we've chatted in the slack channel, you've solved this yourself 🎉
But a few optimization ideas:
Add some variables on top of the module to add the ability to change it easier without digging through the whole module code (Very benefitial for big&complex modules) and also wrap the css in require_css or require_head statements. Doing so will push the style-tag to the <head>-tag of the page and optimize the rendering.
This is killer advice @Anton . I appreciate you both taking the time to reply. Those 2 courses are great for being able to build out pretty much any module you would need, but there are so many nuances to structure, syntax, etc that come with experience that I really appreciate both of your help!
This would totally work, but in the course they want you to use the HubL code snippets for width and height attributes. Both yours an Anton's answers are great though.
I know that you are actually affiliated with HubSpot. I only have 3 days left on my trial and the performance test for lighthouse keeps going above and below 90, which it needs to be above when they test it.
Is there any way to get it grading of the practicums expedited so if it does drop below for some reason, I have time to resubmit before being locked out and losing my work (the reasons it has been dropping below and things like third-party scripts from what I have seen, so unsure if I can even do anything about that in a sandbox account)?
don't worry about getting locked out of a dev portal. If you've created it via https://developers.hubspot.com/ you got a so-called app-test account in this one you've - most likely - have created a CMS sandbox in which you're doing the practicum.
If so, simply login into the app-test account and renew the trial. Trials are always for 90 days and none of your process will be lost if you somehow forget to renew it before the 90 days expire.
There are a few things that will stop working, but once you renew the trial everything will work just fine.
Pro tip: If you want to move things between portals, you can do it via Design-Manager. Simply right-click on a theme/folder and select "copy to different account".
If you like to go the CLI route - also possible. Simply authenticate both portals, fetch it from one to local and upload it to the other. 😉
@Antonbro - you are always on point with the code snips, graphics and everything else to make it easy to understand. Thank you so much.
I do have a Dev Sandbox Account, but on my CRM account it keeps telling me I have 3 days left, so I assumed that meant my CMS access would go away because that was how I got it in the first place.
If I'll still have access to my Dev Sandbox either way, that's even better.