Skip to content

Building URLs in JavaScript

In Luma the mage/url module is used prepend an URL path with the current store base URL.

With Hyvä it is much simpler: the window.BASE_URL variable can be used.


For regular requests, use

BASE_URL + "example/path/action"
or the template literal version

The above is the equvalent to what the Luma method does.

A alternative option is to render the base URL with PHP:

'<?= $escaper->escapeUrl($block->getBaseUrl()) ?>example/path/action'

There is no "better" or "worse" way to do this.


If you need to add the store code to the request path, for example for Ajax requests, use

Or, the non-template literal version:
BASE_URL + 'rest/' + CURRENT_STORE_CODE + '/example/path/action'