Hugo responsive image srcsets
Serve responsive picture tags with custom render-image layout partial in Hugo static site generator.
How to render picture tag images in Hugo with custom render-image.html
layout.
What is hugo?
Hugo is a static site generator written in Go. One can write content in markdown and render HTML using simple HTML templates.
What are responsive images?
Responsive images are images on a website that differ based on screen size or device properties. One popular method in HTML5 to serve responsive images is the <picture>
tag.
<picture>
<!-- image source for different format types -->
<source srcset="/assets/1.png" type="image/png">
<source srcset="/assets/1.webp" type="image/webp">
<!-- img tag with fallback src -->
<img loading="lazy" src="/assets/1.png" alt="My image">
</picture>
In Hugo we can use a custom layout partial to handle rendering of images to include source sets.
How to implement picture tag
Create a custom layout inside your Hugo theme or layouts folder.
/themes/<your-theme>/layouts/_default/_markup/render-image.html
Inside the partial you can access images as they get processed by Hugo when reading your markdown content.
<p class="markdown-image">
<picture>
{{ $url := safeURL .Destination }}
{{ $image_ext := path.Ext $url }}
<source srcset="{{ $url }}" type='image/{{replace $image_ext "." ""}}'>
{{ if or (eq $image_ext ".png") (eq $image_ext ".jpg") }}
{{ $webpDestination := strings.Replace .Destination $image_ext ".webp" }}
{{ if (fileExists ( printf "/static%s" $webpDestination )) }}
<source srcset="{{$webpDestination | safeURL }}" type="image/webp">
{{ end }}
{{ end }}
<img loading="lazy" src="{{ $url }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />
</picture>
</p>
The above partial will include webp sources if they exist for png images. You can extend this to include more content types or sizes.
Converting images to webp
Use the webp tool to convert png and jpeg images into webp format.
cwebp assets/1.png -q 70 -o assets/1.webp
Related content
Javascript email libraries for MailSlurp
Nodemailer alternatives to send and receieve email in code and tests
Fastest way to start a typescript project
Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.
Fetch error message javascript
Handle response exceptions with Fetch in Javascript
GraphQL API disposable email accounts
How to create real email addresses using GraphQL to send and receive emails in tests and frontend applications.
GraphQL Email API Tutorial
Did you know you can send and receive emails using GraphQL?
Hugo responsive image srcsets
Serve responsive picture tags with custom render-image layout partial in Hugo static site generator.
NodeMailer NPM Tutorial
Send and receive email using NodeMailer in Node JS.
Create custom print classes with Tailwind
Configure tailwind.config.js to create prefixed classes with a custom media query.
Send and receive email in Cypress JS tests with MailSlurp
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
Cypress Email Plugin - Test email accounts in Javascript (an...
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Test email accounts with Jest and Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
Test user sign-up with NodeJS and WebDriver
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email v...
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
Base64 file uploads
How to encode files as Base 64 encoded strings in several languages
Deno Email Apis
Send and receive emails in Deno JS. Use APIs in Deno with MailSlurp.
Email read (opened seen settings)
How to control the seen or read settings for an email using MailSlurp.
GraphQL Email API
Fetch and read emails with GraphQL inboxes using MailSlurp email API.
Testing OTP password link username and password for 2 factor...
Testing OTP password link username and password for 2 factor authentication (2FA)
MailSlurp NodeMailer SMTP Usage
Use SMTP nodemailer with MailSlurp disposable email addresses
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Send emails in NodeJS using SMTP
How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS