Skip to main content

UI & Cookies

Two small helper modules: sdk.ui for talking to the SitePack host environment, and sdk.cookies for first-party cookies.

UI​

The sdk.ui module is primarily useful for apps rendered inside an iframe.

setHeight(height)​

Tell the host page the iframe's new height in pixels, so it can resize the frame to fit your content.

// After rendering, report your content height
sdk.ui.setHeight(document.body.scrollHeight);

notify(message, type?)​

Show a notification in the SitePack host environment.

sdk.ui.notify('Product added to cart.', 'success');
sdk.ui.notify('Something went wrong.', 'error');

type is one of 'success', 'info' (default), 'warning', or 'error'.

Messages are posted to the trusted origin only

setHeight and notify use postMessage targeted at the site's own origin (SITEPACK_DOMAIN) — never the * wildcard — so notification text and dimensions are not leaked to arbitrary embedding frames.

Cookies​

The sdk.cookies module provides simple first-party cookie helpers. Cookies are written with SameSite=Lax and path=/.

set(name, value, days)​

sdk.cookies.set('preferred_view', 'grid', 30); // expires in 30 days

get(name)​

Returns the value, or null if the cookie is not set.

const view = sdk.cookies.get('preferred_view'); // 'grid' | null

What the visitor allowed in the SitePack cookie banner. SitePack's own statistics need no consent; use this before loading anything from a third party (GA4, a pixel).

const consent = sdk.cookies.consent();
// → { decided: true, essential: true, functional: true, analytics: true, marketing: false }
if (consent.analytics) {
loadAnalytics();
}

decided is false while the visitor has not answered the banner; every category is then false. The old single-button bar has no categories, closing it counts as consent for everything.

hasConsent(category)​

sdk.cookies.hasConsent('marketing'); // true | false

onConsentChange(callback)​

Runs the callback with the new consent when the visitor answers the banner (sitepack:cookies:consent). Returns a function that stops listening.

const stop = sdk.cookies.onConsentChange((consent) => {
if (consent.analytics) loadAnalytics();
});
Don't store sensitive data in cookies

These helpers write ordinary, non-HttpOnly cookies readable by any script on the page. Never store tokens, personal data, or anything security-sensitive with them — keep those on the server session.