skip to Main Content

I have an image URL, and I’m providing it to the image src attribute on a modal component.
When the modal opens, there is a black card for a few seconds till the image shown.

How can I make it visible faster? I guess I cannot use spinner because it is not a fetch process.

This is my component, the image is just a URL.

const Modal = ({
  closeModal,
  runtime,
  title,
  rating,
  image,
  description,
}: ModalProps) => {
  const {
    movie: { backToListLabel },
  } = dictionary

  const getRating = () => {
    return `${rating}/${TOP_RATING}`
  }

  return (
    <ModalOverlay data-testid="modal">
      <ModalContent>
        <MovieCover
          alt="movie-cover"
          src={image}
        ></MovieCover>
        <MovideDetailsContainer>
          <MovieDetails>
            <Title>{title}</Title>
            <Duration>{runtime}</Duration>
            {rating && (
              <RatingContainer>
                <RatingIcon src={ratingIcon} />
                <Rating>{getRating()}</Rating>
              </RatingContainer>
            )}
            <Description dangerouslySetInnerHTML={{ __html: description }} />
          </MovieDetails>
          <BackToList
            onClick={closeModal}
            data-testid="back-to-list"
          >
            <BackToListIcon src={arrowIcon} />
            <BackToListTitle>{backToListLabel}</BackToListTitle>
          </BackToList>
        </MovideDetailsContainer>
      </ModalContent>
    </ModalOverlay>
  )
}

export default Modal

2

Answers


  1. You can use <link rel="preload" as="image" href="..." />. Have a look at this article on web.dev.

    Login or Signup to reply.
  2. There are several techniques you can use to improve image loading speed in HTML for images located in your project directory.

    1. Optimize the image size: Reduce the file size of your images without sacrificing too much quality. You can use image optimization tools like ImageOptim or TinyPNG to compress the images before using them on your website.

    2. Use responsive images: Implement responsive images using the HTML element or the srcset and sizes attributes. This allows the browser to select the most appropriate image size based on the device’s screen size, helping to reduce unnecessary data transfer.

    3. Lazy loading: Implement lazy loading to defer the loading of images that are not immediately visible on the screen. This technique loads only the images that are visible within the viewport, improving initial page load time. You can use libraries like LazyLoad or implement it manually using JavaScript.

    4. Use image formats optimized for the web: Consider using modern image formats like WebP or JPEG 2000, which provide better compression and smaller file sizes compared to older formats like JPEG or PNG. However, make sure to provide fallbacks for browsers that don’t support these formats.

    5. Specify image dimensions: Always include the width and height attributes in the tag or specify them in CSS. This allows the browser to allocate the necessary space for the image before it finishes loading, preventing layout shifts and improving the overall user experience.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search