Asset service

Contents

Asset service

Applications commonly need to serve static files such as icons, css and javascript to their clients. XP provides a standard and cache optimized approach to serving assets.

Endpoint

The attachment endpoint is located at the root of an application or site’s url space as /_/asset/.

The asset endpoint consists of three elements:

  • Mount point /_/asset/

  • App identifier with optional hash i.e. com.company.app or com.company.app:235293024/

  • Asset path i.e. path/to/asset.css

A complete asset URL pattern: <app-root>//asset/<app-name><:build-id>/<asset-path> i.e. company.com//asset/com.company.app:235293024/path/to/asset.css

Asset files

To make an asset available, simply place it in your projects src/main/resources/assets/ folder. Once the application is deployed, it will be publicly available through the asset endpoint.

If a file is placed in your projects src/main/resources/assets/path/to/asset.ext - the same file will always be available on the contextual url: <webapp-root>/_/asset/<app-name>/path/to/myasset.ext.

Cache headers

To optimize the user experience and offload the XP back-end, the asset service supports so-called "perfect caching". By adding a unique hash to the asset url, the asset service will automatically set "infinite cache headers" when serving the asset.

If the asset is changed (currently by deploying a new app), a new asset URL can be generated. This effectively prevents stale cache issues, and optimizes performance at the same time

assetUrl()

To safely generate a perfect cache asset URL, use the assetUrl function that is part of the Portal Library

Contents