Fixing Images In Blog Posts

Originally published: 18 August 2024

Basic Astro/Image is working, but no resizing...yet

blog images

As a developer, it’s easy to overlook the small things, like image handling, until they become an issue. Recently, I discovered that the images in my blog posts weren’t displaying correctly—a frustrating problem that I hadn’t even realised was happening!

The Challenge with Astro and Cloudflare Integration

One of the challenges I faced was getting images to work properly with Astro, especially when hosted and integrated with Cloudflare. Astro’s @astrojs/image package is incredibly powerful, but it doesn’t play nicely with Markdown (MDX) out of the box when it comes to image handling.

Astro’s image component is designed for optimisation, but when using MDX, it can be tricky. The primary issue I encountered was that images weren’t resizing as expected, though they were being converted to the WebP format, which is a small win. The more significant problem was that Astro’s image component doesn’t work within MDX without importing the images, which can be a bit of a hassle.

A Simple Fix: Modifying TSconfig and Using Path Aliases

After some experimentation, I found a solution that, while not perfect, does the job. I fixed the issue by adding an alias to the TypeScript configuration file (tsconfig.json). Here’s how I did it:

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "strictNullChecks": true,
    "allowJs": true,
    "noImplicitAny": false,
    "moduleResolution": "node",
    "baseUrl": ".",
    "paths": {
      "@assets/*": ["src/assets/*"],
      "@config": ["src/config.ts"],
      "@components/*": ["src/components/*"],
      "@content/*": ["src/content/*"],
      "@layouts/*": ["src/layouts/*"],
      "@pages/*": ["src/pages/*"],
      "@styles/*": ["src/styles/*"],
      "@utils/*": ["src/utils/*"],
      "@images/*": ["src/images/*"]
    }
  }
}

Then in the MDX change the path to:

![Optimized hero image](@images/test/hero1-min.avif)

This approach isn’t perfect—Astro’s image component still requires images to be imported when using it inside MDX, which is slightly annoying. However, this workaround makes the development process a bit smoother.

Conclusion

Working with images in Astro, particularly when integrating with Cloudflare, has its challenges. However, with a few tweaks to the configuration files and some patience, it’s possible to get everything working nicely. While the solution I’ve implemented is not the most elegant, it’s functional and has resolved the immediate issue of images not displaying in my blog posts.

If you’re also working with Astro and facing similar issues, I hope this post helps you out. Remember, sometimes the smallest changes can make the biggest difference in your development workflow.

Happy coding!

Read Count: 0


⬅️ Previous Post

Change My Professional Website To Astro

Next Post ➡️

Optimising Images on Podia with Cloudflare: A Guide for Better Performance