getBaseUrl
Get the base URL of the shop, retrieved from configuration or defaulting to localhost.
Import
import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';
Syntax
getBaseUrl(): string
Parameters
None.
Return Value
Returns a string containing the base URL without trailing slashes.
Examples
Basic Usage
import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';
const baseUrl = getBaseUrl();
// Returns: 'https://myshop.com' or 'http://localhost:3000'
Building Full URLs
import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';
const baseUrl = getBaseUrl();
const productUrl = `${baseUrl}/product/${product.url_key}`;
const categoryUrl = `${baseUrl}/category/${category.url_key}`;
Email Templates
import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';
const emailData = {
shopUrl: getBaseUrl(),
orderLink: `${getBaseUrl()}/account/orders/${orderId}`,
logoUrl: `${getBaseUrl()}/assets/logo.png`
};
Resolution order
The base URL is resolved from the first source that yields a value:
| # | Source | Notes |
|---|---|---|
| 1 | EVERSHOP_HOME_URL environment variable | Takes precedence over every config file. Trimmed; an empty value is treated as unset. |
| 2 | shop.homeUrl config | The usual place to set it for a fixed deployment. |
| 3 | Localhost on PORT (default 3000) | Development fallback. |
EVERSHOP_HOME_URL
EVERSHOP_HOME_URL=https://myshop.com
This is the right knob for container and platform deployments, where the public URL is injected at run time rather than baked into a config file.
A malformed value fails boot
EVERSHOP_HOME_URL is validated at startup. If it is set but is not a parseable absolute URL, or its protocol is not http:/https:, the process throws and exits — every absolute link and email the store generates would otherwise be broken. Leaving it unset (or empty) is fine; the store then falls back to shop.homeUrl.
Config file
{
"shop": {
"homeUrl": "https://myshop.com"
}
}
Notes
- Always removes trailing slashes from the returned URL
EVERSHOP_HOME_URLoverridesshop.homeUrl, not the other way round- Useful for generating absolute URLs in emails, RSS feeds, sitemaps or API responses
See Also
- buildAbsoluteUrl - Build an absolute URL from a route ID
- getConfig - Get configuration values