Skip to main content

PresetOnlyStrategy

PresetOnlyStrategy

An ImageTransformStrategy which only allows transformations to be made using presets which are defined in the available presets.

With this strategy enabled, requests to the asset server must include a preset parameter (or use the default preset)

This is valid: http://localhost:3000/assets/some-asset.jpg?preset=medium

This is invalid: http://localhost:3000/assets/some-asset.jpg?w=200&h=200, and the dimensions will be ignored.

The strategy can be configured to allow only certain quality values and formats, and to optionally allow the focal point to be specified in the URL.

If a preset is not found in the available presets, an error will be thrown.

Example

import { AssetServerPlugin, PresetOnlyStrategy } from '@vendure/core';

// ...

AssetServerPlugin.init({
//...
imageTransformStrategy: new PresetOnlyStrategy({
defaultPreset: 'thumbnail',
permittedQuality: [0, 50, 75, 85, 95],
permittedFormats: ['jpg', 'webp', 'avif'],
allowFocalPoint: true,
}),
});
Signature
class PresetOnlyStrategy implements ImageTransformStrategy {
constructor(options: PresetOnlyStrategyOptions)
getImageTransformParameters({
input,
availablePresets,
}: GetImageTransformParametersArgs) => Promise<ImageTransformParameters> | ImageTransformParameters;
}

constructor

method
(options: PresetOnlyStrategyOptions) => PresetOnlyStrategy

getImageTransformParameters

method

PresetOnlyStrategyOptions

Configuration options for the PresetOnlyStrategy.

Signature
interface PresetOnlyStrategyOptions {
defaultPreset: string;
permittedQuality?: number[];
permittedFormats?: ImageTransformFormat[];
allowFocalPoint?: boolean;
}

defaultPreset

property
string

The name of the default preset to use if no preset is specified in the URL.

permittedQuality

property
number[]
default:
[0, 50, 75, 85, 95]

The permitted quality of the transformed images. If set to 'any', then any quality is permitted. If set to an array of numbers (0-100), then only those quality values are permitted.

permittedFormats

property
ImageTransformFormat[]
default:
['jpg', 'webp', 'avif']

The permitted formats of the transformed images. If set to 'any', then any format is permitted. If set to an array of strings e.g. ['jpg', 'webp'], then only those formats are permitted.

allowFocalPoint

property
boolean
default:
false

Whether to allow the focal point to be specified in the URL.