Performance

Optimizing Images in a Headless Blog with Automatic CDN Delivery

A step‑by‑step guide for developers to boost image performance on headless blogs using CMSKite, Next.js, and CDN features.

3 min read

Why Image Optimization Matters for Headless Blogs – headless blog image optimization

In a headless architecture, the front‑end fetches assets directly from services rather than through a traditional page render. Large image files can dominate the load time of a page, hurting the Largest Contentful Paint (LCP) metric and overall Core Web Vitals. Unlike monolithic CMS platforms that often embed image processing into the page generation step, a headless workflow lets you offload that work to a dedicated image delivery network. By serving images from a CDN that sits at the edge, the distance between the user and the asset shrinks, aligning with the decoupled nature of headless sites. This approach also frees your front‑end code from handling heavy file transfers, letting you focus on interactivity and content.

Configure Automatic CDN Delivery with CMSKite and Next.js

Start by adding the CMSKite SDK to your Next.js project:

npm install @cmskite/sdk

Initialize the client in a module that runs on both server and client:

import { createClient } from '@cmskite/sdk';
export const cms = createClient({ apiKey: process.env.CMSKITE_API_KEY });

CMSKite provides a built‑in image CDN endpoint. Once your images are stored, they are automatically available at a sub‑domain that the SDK resolves. To rewrite all image URLs to this CDN, edit next.config.js:

module.exports = {
  async rewrites() {
    return [{
      source: '/images/:path*',
      destination: 'https://cdn.cmskite.com/:path*' // use your CDN domain
    }];
  }
};

After rebuilding, request an image and inspect the response headers with a simple curl command:

curl -I https://cdn.cmskite.com/example.jpg

You should see standard cache‑control headers indicating that the asset is cacheable at the edge. This confirms that the CDN is serving the image directly.

Responsive Images: srcset, picture, and WebP Conversion

CMSKite’s image API lets you request size variants on the fly by adding query parameters to the URL. For example, appending ?w=small or ?w=large returns a resized version. In a React component, generate a set of URLs for the sizes you need and pass them to the srcSet attribute:

const src = cms.imageUrl('my-photo.jpg');
const srcSet = `${src}?w=small 400w, ${src}?w=medium 800w, ${src}?w=large 1200w`;

return (
  <img src={src}?w=medium alt="Sample" srcSet={srcSet} sizes="(max-width: 600px) 100vw, 600px" />
);

For browsers that support WebP, add the format=webp parameter. Wrap the <img> in a <picture> element to provide a WebP source with a fallback to the original format:

<picture>
  <source type="image/webp" srcSet={`${src}?w=medium&format=webp`} />
  <img src={`${src}?w=medium`} alt="Sample" loading="lazy" />
</picture>

Modern browsers will pick the WebP source, while older browsers receive the standard image.

Lazy Loading, Cache‑Control, and Edge Caching Best Practices

Add loading="lazy" to every <img> that is not above the fold. For finer control, use the IntersectionObserver API to trigger image loads only when they near the viewport. Adjust cache‑control headers through the CMSKite dashboard to set a reasonable max‑age and enable stale‑while‑revalidate, ensuring that browsers keep a fresh copy while the CDN can serve a stale version during revalidation.

When you update an image in the CMS, purge the CDN cache for that path via the SDK’s purge method rather than calling a raw API endpoint. This keeps edge caches in sync without manual HTTP requests. If you need on‑the‑fly resizing, combine a lightweight edge function (such as Vercel Edge Middleware) that reads the query parameters and forwards the request to the CMSKite image endpoint. This keeps the original image untouched and lets the edge compute the variant just in time.

Measuring the Gains and Ongoing Optimization

Run Lighthouse or WebPageTest before you make any changes and record the LCP, FID and CLS values. After implementing CDN delivery, responsive srcset, and lazy loading, run the tests again. Compare the two runs to see how the image delivery improvements affect the core metrics. A noticeable reduction in LCP usually signals that the largest image is now arriving faster from the edge.

To keep optimization continuous, add a checklist to your CI/CD pipeline: verify that image URLs point to the CDN, ensure srcSet includes at least three variants, and confirm that loading="lazy" is present on non‑critical images. Automate a Lighthouse CI step to fail builds if performance regresses.

Next steps: Start your free CMSKite trial today and enable automatic image CDN for your headless blog. Follow the steps above to configure the SDK, rewrite URLs, and add responsive markup—then watch your site’s speed improve.

  • headless
  • images
  • cdn
  • nextjs
By CMSKite team