Resolve Matrix Asset URL

The resolveMatrixAssetByUrl function accepts a URL and resolves the asset data by making a request to the configured Content Management system.

How to use it

  • You must avoid early typeof checks within your component code to ensure reliability.

  • The URL variable is cast as a string initially, and then replaced late in the render process.

  • Session-based auth must be enabled for the Content Management Content API.

The recommend source for Content Management asset URLs is using the context URL utility - Context URL.

// A simple "Page Header" component.
export default {
  async main({}, info) {
    const currentUrl = info.ctx.url; (1)
    const data = await info.fns.resolveMatrixAssetByUrl(currentUrl, [`metadata`, `thumbnail`]); (2)
    return ` (3)
    <div class="banner">
      <img src="${data.thumbnail.url}" alt="${data.thumbnail.alt}" />
      <h1>${data.name}</h1>
      <p>
        ${data.metadata.description}
      </p>
    </div>
    `;
  }
}
1 Get the URL from the context object.
2 Retrieve asset data from the Squiz Content Management system.
3 Print a banner using the returned asset data.