---
url: https://gudhub.com/docs/ssr-ssg/server/features/image-handler/
title: "ImageHandler.js\n                #"
description: "The ImageHandler class is responsible for managing image processing and caching in a server site render system. It supports detecting image formats, handling ex"
lang: uk
updated: 2026-09-08T15:35:07.263Z
---

# ImageHandler.js [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#imagehandler-js)

## Overview [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#overview)

The `ImageHandler` class is responsible for managing image processing and caching in a server site render system. It supports detecting image formats, handling external image downloads, caching images, and creating optimized versions of images in various sizes and formats (e.g., `.webp`).

---

## Content [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#content)

-   [ImageHandler.js](#imagehandlerjs)
    -   [Overview](#overview)
    -   [Content](#content)
    -   [Methods](#methods)
        -   [`detectImage`](#detectimage)
        -   [`detectWebpImage`](#detectwebpimage)
        -   [`detectExternalImage`](#detectexternalimage)
        -   [`getFromCache`](#getfromcache)
        -   [`generateImages`](#generateimages)
        -   [`prepareSources`](#preparesources)
        -   [`downloadExternalImage`](#downloadexternalimage)

---

## Methods [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#methods)

### `detectImage` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#detectimage)

-   **Purpose**: Determines if a file path corresponds to an image file.
-   **Parameters**:
    -   `path` (string): The file path to check.
-   **Returns**: `true` if the file is an image; otherwise, `false`.
-   **Supported Formats**: `.jpeg`, `.jpg`, `.tif`, `.tiff`, `.png`, `.webp`, `.bmp`.

---

### `detectWebpImage` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#detectwebpimage)

-   **Purpose**: Checks if a file path corresponds to a `.webp` image.
-   **Parameters**:
    -   `path` (string): The file path to check.
-   **Returns**: `true` if the file is a `.webp` image; otherwise, `false`.

---

### `detectExternalImage` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#detectexternalimage)

-   **Purpose**: Determines if a URL points to an external image by extracting the `source` query parameter.
-   **Parameters**:
    -   `url` (string): The URL path.
    -   `urlParams` (string): Query parameters to append to the URL.
-   **Returns**: The value of the `source` query parameter if present; otherwise, `null`.

---

### `getFromCache` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#getfromcache)

-   **Purpose**: Retrieves an image from the cache if it exists, or generates it if not.
-   **Parameters**:
    -   `website` (string): The website identifier.
    -   `url` (string): The image URL path.
    -   `urlParams` (string): Query parameters for the URL.
-   **Returns**:
    -   An object containing:
        -   `type` (`'asset'` or `'error'`).
        -   `path` (string): The path to the cached image or an error message.

---

### `generateImages` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#generateimages)

-   **Purpose**: Handles the generation and caching of images, including external image downloads and `.webp` conversion.
-   **Parameters**:
    -   `website` (string): The website identifier.
    -   `url` (string): The image URL path.
    -   `urlParams` (string): Query parameters for the URL.
    -   `buildFolder` (string, optional): The folder path where the site is being built.
-   **Returns**: A promise resolving to an object containing:
    -   `type` (`'asset'` or `'error'`).
    -   `path` (string): The path to the cached image or an error message.
-   **Key Features**:
    -   Checks for existing cached or `.webp` files.
    -   Creates missing directories if necessary.
    -   Resizes images and generates `.webp` versions.

---

### `prepareSources` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#preparesources)

-   **Purpose**: Creates resized versions of an image (600px and 1200px width) and converts them to `.webp`.
-   **Parameters**:
    -   `path` (string): The file path of the original image.
-   **Returns**: `true` after the resized versions are created.
-   **Behavior**:
    -   Resizes images exceeding 600px and 1200px widths.
    -   Saves resized images with suffixes `-600` and `-1200`.

---

### `downloadExternalImage` [#](https://gudhub.com/docs/ssr-ssg/server/features/image-handler/#downloadexternalimage)

-   **Purpose**: Downloads an external image, caches it, and generates optimized versions.
-   **Parameters**:
    -   `website` (string): The website identifier.
    -   `url` (string): The image URL path.
    -   `urlParams` (string): Query parameters for the URL.
-   **Returns**: The local path to the cached image.
-   **Behavior**:
    -   Constructs the full external URL using the `source` query parameter.
    -   Downloads the image and saves it to the cache.
    -   Creates `.webp` and resized versions of the image.

---
