blog-template icon indicating copy to clipboard operation
blog-template copied to clipboard

Image In Post Body Cannot Displayed

Open NinjaGeekDev opened this issue 2 years ago • 9 comments

Hi, I've found that each post's hero image is displayed correctly because it's defined in the MDX frontmatter section and is built to the /dist/_astro path during the Astro build stage. Unfortunately, however, the image files referenced in the MDX body are not built to that path, which results in the images in the body not being displayed. This might be a silly question to ask, but I'm a complete beginner, could you please help me out? Thank you!

image

NinjaGeekDev avatar Jan 23 '24 02:01 NinjaGeekDev

+1 Today notice this issue too. With same image path, adding images through tinacms. HeroImage available from _astro folder at the same time

serjs avatar Jan 30 '24 18:01 serjs

I am also facing this exact issue. Therefore, I am trying to change the theme entirely.

FahimFBA avatar Feb 01 '24 10:02 FahimFBA

I read astro docs about image optimization and TinaCMS docs. Looks like there are no way to fix thus out of box. Every image in mdx must have path under src folder. In this template case, there where images mentioned ../../../src/assets/images must be placed instead /src/assets/images inserted by Tina. Correct path handled by Astro during build (image optimisation process).

We can add prebuild step with recursive find all mdx files and using sed regex rule to preprocess all images during build, but I thing it's better doing in JS, not system shell comands for exclude OS, shell differences.

serjs avatar Feb 01 '24 12:02 serjs

Based on @serjs I've discovered a workaround which is easy enough if you're okay with it:

---
myfields
---

import { Image } from 'astro:assets'
import image1 from '/src/assets/images/image1.png'

<Image
	src={image1}
	alt='alttext'
/>

After many attempts, this was the easiest way to work around this problem. May it serve you well.

WDI-Json avatar Feb 03 '24 20:02 WDI-Json

Fixed this by moving from Sharp image server to Sqoosh. link to astro docs

import { defineConfig, squooshImageService } from 'astro/config';

export default defineConfig({
  image: {
    service: squooshImageService(),
  },
});

Brian-Kariu avatar Apr 04 '24 17:04 Brian-Kariu

facing same issue

NongBritee avatar Apr 26 '24 16:04 NongBritee

Use relative paths can solve this problem.

wangsrGit119 avatar Jul 18 '24 00:07 wangsrGit119