Asset service
Contents
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 asset 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 fingerprint i.e.
com.company.app
orcom.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" (cache-control: public, max-age=31536000, immutable
) when serving the asset.
If application assets are changed, 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