visit
"config": {
"posts_per_page": 10,
"image_sizes": {
"xxs": {
"width": 30
},
"xs": {
"width": 100
},
"s": {
"width": 300
},
"m": {
"width": 600
},
"l": {
"width": 900
},
"xl": {
"width": 1200
}
}
}
<img class="feature-image"
srcset="{{img_url feature_image size="s"}} 300w,
{{img_url feature_image size="m"}} 600w,
{{img_url feature_image size="l"}} 900w,
{{img_url feature_image size="xl"}} 1200w"
sizes="800px"
src="{{img_url feature_image size="l"}}"
alt="{{title}}"
/>
Next we take a few simple steps to optimize Asset Download Time. That includes adding
preconnect
and preload
headers in default.hbs
:<link rel="preconnect" href="//fonts.gstatic.com/" crossorigin="anonymous">
<link rel="preconnect" href="//cdn.jsdelivr.net/" crossorigin="anonymous">
<link rel="preconnect" href="//widget.appfleet.com/" crossorigin="anonymous">
<link rel="preload" as="style" href="//fonts.googleapis.com/css?family=Red+Hat+Display:400,500,700&display=swap" />
<link rel="preload" as="style" href="//cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" />
Most often than not, users coming from Google or some other source to a specific blog post will navigate to the homepage to check what else we have written. For the same reason, on blog posts we also added
prefetch
and prerender
tags for the main blog page.That way the browser will asynchronously download and cache it, making the next most probable action of the user almost instant:<link rel="prefetch" href="//appfleet.com/blog">
<link rel="prerender" href="//appfleet.com/blog">
Note that even if you don't use DigitalOcean, you are still recommended to use Nginx in-front of the Node.js app of Ghost.This eventually makes the solution pretty simple. We use Nginx to rewrite the HTML, along with enabling a CDN and lazy-loading images at the same time, without any extra JS.For CDN, you may also use the free CDN offered by Google to all AMP projects. Not many people are aware that you can use it as a regular CDN without actually implementing AMP. All you have to do is use this URL in front of your images:
//appfleet-com.cdn.ampproject.org/i/s/appfleet.com/
Replace the domains with your own and change your <img> tags, and you are done. All images are now served through Google's CDN.The best part is that the images are not only served but optimized as well. Additionally, it will even serve a WebP version of the image when possible, further improving the performance of your site.As for lazy loading, you may use the native functionality of modern browsers that looks like this
<img loading="lazy"
. By adding loading="lazy"
to all images, you instruct the browsers to automatically lazy load them once they become visible by the user.And now the code itself to achieve this:server {
listen 80;
server_name NAME;
location ^~ /blog/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host "appfleet.com";
proxy_set_header X-Forwarded-Proto https;
proxy_pass //127.0.0.1:2368;
proxy_redirect off;
#disable compression
proxy_set_header Accept-Encoding "";
#rewrite the html
sub_filter_once off;
sub_filter_types text/html;
sub_filter '<img src="//appfleet.com' '<img loading="lazy" src="//appfleet-com.cdn.ampproject.org/i/s/appfleet.com';
}
}
Next we use the
sub_filter
parameter to rewrite the HTML. Ghost is using absolute paths in images, so we add the beginning as well. And in 1 line enabled both the CDN and lazyloading.Reload the config and you are good to go. Disclaimer - While the above configuration should definitely help optimize Ghost blogs like , non-ghost blogs like might need a different or a blended approach.
Do let me know your thoughts if you have a different approach to optimize yours.