resolveStoreIdentifier
Decides which store the current request is about: the explicit value, the preview request, the URL, the server render or the host.
import { resolveStoreIdentifier } from '@salla.sa/twilight-theme-engine/api/store';In plain words
Every API call must say which store it is for. On a merchant's own domain the address answers that. On Salla's preview host and on localhost, many stores share one address, so the request names its store instead.
resolveStoreIdentifier() goes through those possibilities in a fixed order and returns the first answer: a store id, a username or a domain. The API client calls it for you on every request.
Signature
function resolveStoreIdentifier(explicit?: string): string // '' when nothing resolves // First non-empty answer wins: // 1. explicit // 2. the store the request named: ?storeId= or its cookie (preview host and localhost only) // 3. the store username in the first path segment (same hosts) // 4. what the server render published for the browser // 5. the request host, when that host is a storefront // 6. the browser host, when that host is a storefront // 7. VITE_STORE_DOMAIN, then STORE_DOMAIN
Try it live
import { resolveStoreIdentifier } from '@salla.sa/twilight-theme-engine/api/store';
// '' means nothing resolved: every Salla API call would fail with 422.
const storeIdentifier = resolveStoreIdentifier();
Example
import { resolveStoreIdentifier } from '@salla.sa/twilight-theme-engine/api/store';
/** Tags an analytics event with the store the page was rendered for. */
export function storeTag(): string {
return resolveStoreIdentifier() || 'unknown-store';
}
How it behaves
Re-exported from
utils/store-identity.ts. The API client setsstore-identifierfrom the same function on purpose: one resolver, so the store checked is the store sent.Only
preview.salla.designand loopback hosts (localhost,127.0.0.1,[::1],*.localhost) may name a store. Everywhere else the host is the store and?storeId=is ignored, which stops a link from showing a rival store on a merchant's domain.In the browser, step 4 keeps the hydrated page asking about the store the server rendered, even when the address bar no longer names it.
Gotchas
It returns an empty string, not
null, when nothing resolves.store.settings()then refuses to call and returnsnull, and the root loader shows "Store Unavailable".On
localhostand the preview host the first path segment is a store username, solocalhost:5190/cartnames a store called "cart". Start from/or a locale such as/en/….VITE_STORE_DOMAINcomes last: on a real storefront the host answers first, so the variable matters only in local development.