Images
Developer preview
Goal: show an image that an author selects in an asset parameter. Use the next/image component, so that Next.js optimizes the image.
Prerequisites#
A Next.js app with the SDK. Refer to Next.js App Router SDK.
A component definition with a parameter of the type Asset, for example
image.The
@uniformdev/canvaspackage, at the same version as the SDK:v=20.81.1-alpha.25.sha-f076f9b857 npm install @uniformdev/canvas@$v
The value of an asset parameter#
The type of an asset parameter value is AssetParamValue from @uniformdev/canvas. It is an array of asset items, because an asset parameter can have more than one asset. Each item keeps its data in fields, and each field has a type and a value:
flattenValues from @uniformdev/canvas changes each item into an object with plain values: url, id, title, description, mediaType, width, height, size, focalPoint and custom. Only url is always in the data. The other fields can be undefined.
| Call | Result |
|---|---|
flattenValues(image?.value) | An array of flat assets |
flattenValues(image?.value, { toSingle: true }) | The first flat asset, or undefined for an empty array |
Give flattenValues the parameter value (image?.value), not the parameter object (image). The function reads the fields of each item.
Step 1: Allow the Uniform image host#
next/image loads remote images only from the hosts in images.remotePatterns. The Uniform asset library delivers images from img.uniform.global (US) and img.eu.uniform.global (EU):
next.config.ts
note
An asset can also come from a DAM integration or from a custom URL. Its url then has a different host. Add each of these hosts to remotePatterns. Without the host, next/image gives an error for the image.
Step 2: Make the image component#
components/feature-image.tsx
Register the component in components/resolveComponent.tsx. Refer to Next.js App Router SDK.
Step 3 (optional): Show more than one image#
Without toSingle, flattenValues returns all assets of the parameter:
components/gallery.tsx
Notes#
- Alt text: an asset has no
altfield. The example uses thedescriptionof the asset, then itstitle. To set a different text in each component, add a text parameter for the alt text. For an image that is only decoration, usealt="". - Width and height:
next/imagemust havewidthandheight, orfill. For an image, Uniform gives the size of the original image inwidthandheight. Other assets, and assets from a custom URL or a DAM integration, can have no size. Then the example usesfillin a box with a fixed aspect ratio. - Fallback: an author can add a component and not select an image. Then
flattenValuesreturnsundefined. In Canvas,context.isContextualEditingistrue, so the example shows a placeholder that tells the author to select an image. On the live site, it renders nothing. - The main image of a page: for the largest image at the top of a page, add
preloadtoImage. Next.js 16 replaced thepriorityproperty withpreload. - Image transformations: the Uniform image delivery API can crop and resize images with query parameters.
imageFromfrom@uniformdev/assetsmakes these URLs. Refer to Image Delivery API.