DocsDevelopers
Custom code blocks
Add HTML, CSS and JavaScript blocks to the loyalty page, bind member data such as balance and tier, use the thrice object, and recover with safe mode.
Custom code blocks let you put your own HTML, CSS and JavaScript on your loyalty page, next to Thrice's own sections. Use them for content Thrice doesn't have, such as a seasonal banner, a personal greeting with the member's balance, or a link to your own campaign. This article is the technical reference for a developer or agency. For the page builder itself, see Page builder.
Where custom code runs
Custom code runs only on the loyalty page, the page that shows the Loyalty page theme block. It doesn't run in the rewards panel (the floating widget), on the customer account page, on the Thank you page or anywhere else in your theme.
Add a block
- Open Page builder and choose Loyalty page under What to edit.
- Choose Add custom code block.
- Give it a Name, choose its Width and Who sees it, and write the HTML, CSS and JavaScript in the code tabs.
- Check it in the preview, then choose Save draft or Publish.
Drag the block in the Sections list to place it between Thrice's sections. Page CSS & JS at the bottom of the list holds the code for the whole page.
Fields and limits
| Field | Limit |
|---|---|
| Custom blocks on the page | Up to 20 |
| Name | Up to 60 characters |
| Block HTML | Up to 30,000 characters |
| Block CSS | Up to 20,000 characters |
| Block JavaScript | Up to 30,000 characters |
| Page CSS | Up to 30,000 characters |
| Page JavaScript | Up to 30,000 characters |
| Whole layout, all code included | Up to 400,000 characters as stored, where a line break or a double quote counts as two |
Code over a limit can't be saved: the builder shows "Too large to save" and lists what to shorten. Thrice never cuts code to fit.
- Name is only shown in the builder and in console errors, never to customers.
- Width: Page width puts the block in the page's centered column; Full width runs it to the edge of the page.
- Who sees it: Everyone, Members only or Guests only. Guests include signed-in customers who haven't joined.
- Show this section off hides the block without deleting it.
HTML and bindings
Write any markup. Thrice inserts it as it is. <script> tags in the HTML field don't run; put JavaScript in the JavaScript tab.
Add data-thrice-bind="key" to an element to fill it with a value. Values are always written as text, never as HTML. If a value is empty, for example the balance for a guest, the element keeps what you wrote inside it, so use that as the fallback.
| Key | Value |
|---|---|
member.firstName |
The member's first name |
member.balance |
Points balance, formatted |
member.tier |
Current VIP tier name |
member.nextTier |
Name of the next VIP tier |
member.pointsToNextTier |
What's still needed for the next tier: points, or an amount of money if tiers are based on spend |
member.referralUrl |
The member's referral link on your store's own domain, https://your-store.com/?ref=CODE |
program.name |
The program name from Settings |
program.pointsLabel |
Your word for "points", from Languages & text |
Add data-thrice-if="member" or data-thrice-if="guest" to an element to show it only to members or only to guests. Bindings and data-thrice-if are updated again whenever the loyalty data changes, for example after the visitor joins or redeems.
CSS
- Block CSS is scoped to its block automatically: Thrice nests it inside the block's own selector, so
.card { … }only matches inside that block. Rules that can't be nested, such as@keyframesand@font-face, belong in the page CSS. - Page CSS applies to the whole loyalty page, Thrice's own sections included.
- Custom blocks sit in the page itself, not inside the shadow DOM that holds Thrice's sections, so your theme's styles apply to them. These variables from your Store design are available:
--t-primary,--t-bg,--t-surface,--t-text,--t-muted,--t-accentand--t-r(corner radius).
The Custom CSS field under Store design is a separate thing: it styles Thrice's own sections in the rewards panel and on the loyalty page, doesn't reach custom blocks, and is cleaned of markup and scripts. See Store design.
JavaScript
A block's JavaScript is the body of a function that receives two arguments, thrice and block.
| Name | What it is |
|---|---|
block.root |
The block's element |
block.id, block.name |
The block's ID (c_ followed by letters and digits) and its name |
thrice.state |
The loyalty data: program, member (null for guests), rules, rewards, tiers and challenges |
thrice.subscribe(fn) |
Calls fn(state) whenever the loyalty data changes. Returns a function that unsubscribes. |
thrice.open(view) |
Opens the rewards panel. Leave out view for its home screen, or use "rewards", "earn", "tiers" or "activity". |
thrice.refresh() |
Reloads the loyalty data |
thrice.format.number, thrice.format.money, thrice.format.date |
Format values the way the widget does |
thrice.t(key, vars) |
A text from Languages & text in the visitor's language |
thrice.locale |
The visitor's language |
thrice.preview |
true in the page builder's preview |
thrice.version |
1 |
If your code returns a function, Thrice calls it when the block's code is removed. Use it to remove listeners and timers.
Page JavaScript gets the same thrice, with block set to null.
When code runs
- Nothing runs until the visitor's loyalty data has loaded, so
thrice.stateis always set when your code starts. - Each block's JavaScript runs once, when the block is added to the page. Its HTML and CSS are in place and its bindings filled by then.
- Page JavaScript runs once, after the blocks shown at that moment have started.
- A block for a different audience isn't added at all. If a guest joins during their visit, a Members only block is added then and its JavaScript runs then. Page JavaScript doesn't run again.
- Your code doesn't run again when the data changes. Use
thrice.subscribeto react. - An error thrown while a block starts is caught: the other blocks and Thrice's sections keep working, the block's element gets a
data-thrice-errorattribute and the console shows[thrice] block <name> failed. Errors thrown later, in event handlers or timers, aren't caught for you.
Custom blocks are ordinary elements in the page, so document.querySelector finds them.
Everything is public
The code of every visible custom block is sent to every visitor of the loyalty page, including blocks set to Members only. Who sees it decides what is shown, not who can read the code. Never put API keys, passwords, discount codes or anything else secret in a block.
Thrice doesn't clean or change your code. It's published as you wrote it and runs with the same access as your theme's own scripts, so only add code you trust.
Example block
A card that greets members with their balance, tells them how many rewards they can redeem and opens the rewards panel.
HTML:
<div class="perk">
<p data-thrice-if="member">
Hi <span data-thrice-bind="member.firstName">there</span>, you have
<strong data-thrice-bind="member.balance">0</strong>
<span data-thrice-bind="program.pointsLabel">points</span>.
</p>
<p data-thrice-if="member" class="perk-ready"></p>
<p data-thrice-if="guest">
Create an account to join <span data-thrice-bind="program.name">our rewards program</span>.
</p>
<button type="button" class="perk-button">See rewards</button>
</div>
CSS:
.perk {
padding: 24px;
border-radius: var(--t-r);
background: var(--t-surface);
color: var(--t-text);
}
.perk-button {
padding: 10px 18px;
border: 0;
border-radius: var(--t-r);
background: var(--t-primary);
color: #fff;
cursor: pointer;
}
JavaScript:
const ready = block.root.querySelector(".perk-ready");
const button = block.root.querySelector(".perk-button");
function update(state) {
const count = state.member ? state.rewards.filter((reward) => reward.affordable).length : 0;
ready.textContent = count > 0 ? `${count} of your rewards are ready to redeem.` : "";
}
const openRewards = () => thrice.open("rewards");
button.addEventListener("click", openRewards);
update(thrice.state);
const unsubscribe = thrice.subscribe(update);
return () => {
button.removeEventListener("click", openRewards);
unsubscribe();
};
Safe mode
Add ?thrice-safe=1 to the loyalty page address to open it without any custom code. Thrice's own sections still show, with the note "Safe mode: custom code is turned off on this page." Use it to reach a page your code has broken.
Thrice also switches a browser to safe mode by itself. If custom code starts running and the page never finishes it, for example because an endless loop froze the tab, that browser opens the loyalty page in safe mode on its next visit, until you publish different code. Publishing the same code again does not clear it. To run the code again in that browser while you test, add ?thrice-safe=0.
The page builder's preview has its own Safe mode switch, and lists errors from your code under Errors in custom code.
Draft and publish
Changes in the builder stay in a draft until you publish. Customers see the published version only.
- Save draft keeps your work without changing your store.
- Publish makes the draft live, with an optional note of up to 200 characters. If the layout contains JavaScript, the builder reminds you that it will run for every visitor.
- Discard draft throws the draft away. The live layout doesn't change.
- Version history keeps the 50 most recent published versions. Restore as draft loads one into your draft; Publish this version puts it live again straight away and leaves your draft alone.
- Reset to the built-in layout removes your layout, custom code included, from the store. Published versions stay in the history.
Visitors' browsers keep the published code until a newer version is published. A page always runs the code of the version it was published with.
If someone else saved the layout while you were editing, you see "Someone else changed this layout" and need to reload. Viewers can open the builder and the preview but can't change anything. See Page builder for the rest of the builder.
Code an AI assistant writes through MCP goes to the same draft. Publishing it is a separate step. Publishing or rolling back to a layout with custom code needs an explicit acknowledgement in the assistant's call. The assistant is told to show you the change and ask first, but Thrice can't check that it did.