index.mdx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ---
  2. title: "AssetServerPlugin"
  3. generated: true
  4. ---
  5. <GenerationInfo sourceFile="packages/asset-server-plugin/src/plugin.ts" sourceLine="176" packageName="@vendure/asset-server-plugin" />
  6. The `AssetServerPlugin` serves assets (images and other files) from the local file system, and can also be configured to use
  7. other storage strategies (e.g. [S3AssetStorageStrategy](/reference/core-plugins/asset-server-plugin/s3asset-storage-strategy#s3assetstoragestrategy). It can also perform on-the-fly image transformations
  8. and caches the results for subsequent calls.
  9. ## Installation
  10. `yarn add @vendure/asset-server-plugin`
  11. or
  12. `npm install @vendure/asset-server-plugin`
  13. *Example*
  14. ```ts
  15. import { AssetServerPlugin } from '@vendure/asset-server-plugin';
  16. const config: VendureConfig = {
  17. // Add an instance of the plugin to the plugins array
  18. plugins: [
  19. AssetServerPlugin.init({
  20. route: 'assets',
  21. assetUploadDir: path.join(__dirname, 'assets'),
  22. }),
  23. ],
  24. };
  25. ```
  26. The full configuration is documented at [AssetServerOptions](/reference/core-plugins/asset-server-plugin/asset-server-options)
  27. ## Image transformation
  28. Asset preview images can be transformed (resized & cropped) on the fly by appending query parameters to the url:
  29. `http://localhost:3000/assets/some-asset.jpg?w=500&h=300&mode=resize`
  30. The above URL will return `some-asset.jpg`, resized to fit in the bounds of a 500px x 300px rectangle.
  31. ### Preview mode
  32. The `mode` parameter can be either `crop` or `resize`. See the [ImageTransformMode](/reference/core-plugins/asset-server-plugin/image-transform-mode) docs for details.
  33. ### Focal point
  34. When cropping an image (`mode=crop`), Vendure will attempt to keep the most "interesting" area of the image in the cropped frame. It does this
  35. by finding the area of the image with highest entropy (the busiest area of the image). However, sometimes this does not yield a satisfactory
  36. result - part or all of the main subject may still be cropped out.
  37. This is where specifying the focal point can help. The focal point of the image may be specified by passing the `fpx` and `fpy` query parameters.
  38. These are normalized coordinates (i.e. a number between 0 and 1), so the `fpx=0&fpy=0` corresponds to the top left of the image.
  39. For example, let's say there is a very wide landscape image which we want to crop to be square. The main subject is a house to the far left of the
  40. image. The following query would crop it to a square with the house centered:
  41. `http://localhost:3000/assets/landscape.jpg?w=150&h=150&mode=crop&fpx=0.2&fpy=0.7`
  42. ### Format
  43. Since v1.7.0, the image format can be specified by adding the `format` query parameter:
  44. `http://localhost:3000/assets/some-asset.jpg?format=webp`
  45. This means that, no matter the format of your original asset files, you can use more modern formats in your storefront if the browser
  46. supports them. Supported values for `format` are:
  47. * `jpeg` or `jpg`
  48. * `png`
  49. * `webp`
  50. * `avif`
  51. The `format` parameter can also be combined with presets (see below).
  52. ### Quality
  53. Since v2.2.0, the image quality can be specified by adding the `q` query parameter:
  54. `http://localhost:3000/assets/some-asset.jpg?q=75`
  55. This applies to the `jpg`, `webp` and `avif` formats. The default quality value for `jpg` and `webp` is 80, and for `avif` is 50.
  56. The `q` parameter can also be combined with presets (see below).
  57. ### Transform presets
  58. Presets can be defined which allow a single preset name to be used instead of specifying the width, height and mode. Presets are
  59. configured via the AssetServerOptions [presets property](/reference/core-plugins/asset-server-plugin/asset-server-options/#presets).
  60. For example, defining the following preset:
  61. ```ts
  62. AssetServerPlugin.init({
  63. // ...
  64. presets: [
  65. { name: 'my-preset', width: 85, height: 85, mode: 'crop' },
  66. ],
  67. }),
  68. ```
  69. means that a request to:
  70. `http://localhost:3000/assets/some-asset.jpg?preset=my-preset`
  71. is equivalent to:
  72. `http://localhost:3000/assets/some-asset.jpg?w=85&h=85&mode=crop`
  73. The AssetServerPlugin comes pre-configured with the following presets:
  74. name | width | height | mode
  75. -----|-------|--------|-----
  76. tiny | 50px | 50px | crop
  77. thumb | 150px | 150px | crop
  78. small | 300px | 300px | resize
  79. medium | 500px | 500px | resize
  80. large | 800px | 800px | resize
  81. ### Caching
  82. By default, the AssetServerPlugin will cache every transformed image, so that the transformation only needs to be performed a single time for
  83. a given configuration. Caching can be disabled per-request by setting the `?cache=false` query parameter.
  84. ### Limiting transformations
  85. By default, the AssetServerPlugin will allow any transformation to be performed on an image. However, it is possible to restrict the transformations
  86. which can be performed by using an [ImageTransformStrategy](/reference/core-plugins/asset-server-plugin/image-transform-strategy#imagetransformstrategy). This can be used to limit the transformations to a known set of presets, for example.
  87. This is advisable in order to prevent abuse of the image transformation feature, as it can be computationally expensive.
  88. Since v3.1.0 we ship with a [PresetOnlyStrategy](/reference/core-plugins/asset-server-plugin/preset-only-strategy#presetonlystrategy) which allows only transformations using a known set of presets.
  89. *Example*
  90. ```ts
  91. import { AssetServerPlugin, PresetOnlyStrategy } from '@vendure/core';
  92. // ...
  93. AssetServerPlugin.init({
  94. //...
  95. imageTransformStrategy: new PresetOnlyStrategy({
  96. defaultPreset: 'thumbnail',
  97. permittedQuality: [0, 50, 75, 85, 95],
  98. permittedFormats: ['jpg', 'webp', 'avif'],
  99. allowFocalPoint: false,
  100. }),
  101. });
  102. ```
  103. ```ts title="Signature"
  104. class AssetServerPlugin implements NestModule, OnApplicationBootstrap, OnApplicationShutdown {
  105. init(options: AssetServerOptions) => Type<AssetServerPlugin>;
  106. constructor(options: AssetServerOptions, processContext: ProcessContext, moduleRef: ModuleRef, assetServer: AssetServer)
  107. configure(consumer: MiddlewareConsumer) => ;
  108. }
  109. ```
  110. * Implements: NestModule, OnApplicationBootstrap, OnApplicationShutdown
  111. <div className="members-wrapper">
  112. ### init
  113. <MemberInfo kind="method" type={`(options: <a href='/reference/core-plugins/asset-server-plugin/asset-server-options#assetserveroptions'>AssetServerOptions</a>) => Type<<a href='/reference/core-plugins/asset-server-plugin/#assetserverplugin'>AssetServerPlugin</a>>`} />
  114. Set the plugin options.
  115. ### constructor
  116. <MemberInfo kind="method" type={`(options: <a href='/reference/core-plugins/asset-server-plugin/asset-server-options#assetserveroptions'>AssetServerOptions</a>, processContext: <a href='/reference/typescript-api/common/process-context#processcontext'>ProcessContext</a>, moduleRef: ModuleRef, assetServer: AssetServer) => AssetServerPlugin`} />
  117. ### configure
  118. <MemberInfo kind="method" type={`(consumer: MiddlewareConsumer) => `} />
  119. </div>