Context URL

This utility is only available for Components at Edge.

The context object exposes the current URL on which a component is rendering.

This approach is useful when you want to implement content decisions that either:

  • Rely on knowing the current location of a site

  • Retrieve additional data about the parent page from Matrix.

Usage

  • 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 recommended method for retrieving additional data using the URL is the resolveMatrixAssetByUrl utility.

For more information read Resolve Matrix Asset 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 Content Management system.
3 Print a banner using the returned asset data.